Search code examples
c#winformssolversimplex-algorithm

solver gets the minimum value set in decision


I am running a code that needs to calculate the appropriate percentage of ingredients to compose an animal food. In order to do it I am using MS solver foundation. I set the model passing the minimum and maximum value for ingredients and nutrients (an ingredient percentage interferes with more or less nutrient). I need to compose the cheapest (Minimize) cost of the animal food. Here's my code.

  private Solution FormularRacao()
    {
        var ingredientesRacao = _formulacaoRacao.CarregarIngredientesNutrientesFormulacao(true);
        var nutrientes = _formulacaoRacao.CarregarIngredientesNutrientesFormulacao(false);
        var formulacao = _formulacaoRacao.CarregarFormulacao();

        SolverContext context = SolverContext.GetContext();
        context.ClearModel();

        Model model = context.CreateModel();

        var objetivo = new SumTermBuilder(ingredientesRacao.Count);
        var totalIngrediente = new SumTermBuilder(ingredientesRacao.Count);

        List<SumTermBuilder> listaTotalNutriente = new List<SumTermBuilder>();

        //Set decisions
        foreach (var item in ingredientesRacao)
        {
            item.Nome = TratarNome(item.Nome);

            Decision d = new Decision(Domain.RealRange(Convert.ToDouble(item.Minimo), Convert.ToDouble(item.Maximo)), "d_" + item.Nome);

            model.AddDecision(d);

            objetivo.Add(Model.Product(d, Convert.ToDouble(item.Custo)));
            totalIngrediente.Add(d);
            listaTotalNutriente.Add(new SumTermBuilder(nutrientes.Count));
        }

        var SomaIngrediente = totalIngrediente.ToTerm();

        //sum of decisions values must be equal 100
        model.AddConstraint("c_totalIngrediente", SomaIngrediente == 100);

        //totalIngrediente;
        model.AddGoal("racao", GoalKind.Minimize, objetivo.ToTerm());

        int indexIngrediente = 0;
        int indexNutriente = 0;

        //each ingredient contributes with nutrients
        //each nutrient has a min and max set            
        foreach (var ingredienteRacao in ingredientesRacao)
        {
            Ingrediente ingrediente = _formulacaoRacao.CarregarIngrediente(ingredienteRacao.Id);
            indexNutriente = 0;
            ingredienteRacao.Nome = TratarNome(ingredienteRacao.Nome);
            Decision d = model.Decisions.First(x => x.Name == "d_" + ingredienteRacao.Nome);

            foreach (var nutriente in nutrientes)
            {
                var valor = new object();

                if (nutriente.AminoacidoDigestivo.Equals("S"))
                {
                    var aminoacidoDigestivo = _formulacaoRacao.CarregarAminoacidoDigestivo(ingredienteRacao.Id);
                    valor = aminoacidoDigestivo.GetType().GetProperty(nutriente.Nome).GetValue(aminoacidoDigestivo, null);
                }
                else
                {
                    nutriente.Nome = TratarNomeNutriente(nutriente.Nome);
                    valor = ingrediente.GetType().GetProperty(nutriente.Nome).GetValue(ingrediente, null);
                }

                var proporcaoNutriente = Convert.ToDouble(valor.ToString()) * d;

                listaTotalNutriente[indexNutriente].Add(proporcaoNutriente / 100);

                if (indexIngrediente == ingredientesRacao.Count - 1) //last iteration
                {
                    var totalNutriente = listaTotalNutriente[indexNutriente].ToTerm();
                    if (nutriente.Minimo == nutriente.Maximo)
                       model.AddConstraint("c_" + nutriente.Nome, totalNutriente == Convert.ToDouble(nutriente.Maximo));                        
                    else
                        model.AddConstraint("c" + nutriente.Nome, Convert.ToDouble(nutriente.Minimo) <= totalNutriente <= Convert.ToDouble(nutriente.Maximo));                        
                }

                indexNutriente++;
            }
            indexIngrediente++;
        }            

        //saves the model
        TextWriter tw = new StreamWriter("Path\file.oml");
        context.SaveModel(FileFormat.OML, tw);
        tw.Close();

        //Imprime o modelo
        TextWriter txt = new StreamWriter("Path\file.txt");
        foreach (var item in model.Constraints.ToList())
        {
            txt.WriteLine(item.Name + ":  " + item.Expression);
            txt.WriteLine();
        }
        txt.Close();

        Solution solution = context.Solve();

        return solution;
    }

I get as a result the minimum value of the decision that it was set, which means there's no solution found. My question is: Why is solver considering the minimum value set in the decision rather than calculating the appropriate value for each decision?


Solution

  • It was my mistake when modeling the model. The code worked fine. But it did helped me to solve the problem by creating a txt file and adding to it all my constraints, so I could double check the constraints I was setting and also creating a model file (oml) and importing it on Excel so I could check all the constraints set to solve the model. The code is updated in my question so if you need you can check how to create the txt and oml files.