Click here to Skip to main content
15,881,882 members

Solver foundation parameterize

ihsansaidi asked:

Open original thread
i still confused to parameterize solver foundation.
take a look at http://blogs.msdn.com/b/natbr/archive/2009/05/05/creating-parameterized-solver-foundation-models-using-linq-to-sql.aspx[^]

i successfully try that tutorial but i can't use it to my case.
i will review what that web did.
this is normal case :

C#
        void newSample1()
        {
              SolverContext context = SolverContext.GetContext();
              context.ClearModel();
              Model model = context.CreateModel();

              //limit and decision
              Decision sa = new Decision(Domain.RealRange(0, 9000), "SA");
              Decision vz = new Decision(Domain.RealRange(0, 6000), "VZ");
              model.AddDecisions(sa, vz);

              model.AddConstraint("demand1", 0.3 * sa + 0.4 * vz >= 1900);
              model.AddConstraint("demand2", 0.4 * sa + 0.2 * vz >= 1500);
              model.AddConstraint("demand3", 0.2 * sa + 0.3 * vz >= 500);
              
              model.AddGoal("goal", GoalKind.Minimize, 20 * sa + 15 * vz);
              Solution solution = context.Solve(new SimplexDirective());
              Report report = solution.GetReport();
              Console.WriteLine(report);
              Console.ReadLine();
        }


// this result for looping/parameterize code above:


private static void LinqDataBinding()
        {
            SolverContext context = SolverContext.GetContext();
            context.ClearModel();
            Model model = context.CreateModel();
            NortwindDataContext db = new NortwindDataContext();

            Set Product1set = new Set(Domain.Any, "Product1s");
            Set countries = new Set(Domain.Any, "countries");

            Parameter demand = new Parameter(Domain.Real, "demand", Product1set);
            demand.SetBinding(db.Products,  "Demand","ProductId");

            Parameter yield = new Parameter(Domain.Real, "yield", Product1set, countries);
            yield.SetBinding(db.yields,"Yieldc", "ProductId","CountryId");

            Parameter limit = new Parameter(Domain.Real, "limit", countries);
            limit.SetBinding(db.countries,  "limit", "CountryId");
            
            Parameter cost = new Parameter(Domain.Real, "cost", countries);
            cost.SetBinding(db.countrycosts,"cost", "CountryId" );

            model.AddParameters(demand, yield, limit, cost);

            Decision produce = new Decision(Domain.RealNonnegative, "produce", countries);
            model.AddDecision(produce);

            model.AddConstraint("Demand",
            Model.ForEach(Product1set, p => Model.Sum(Model.ForEach(countries, c => yield[p, c] * produce[c])) >= demand[p]));

            model.AddConstraint("ProductionLimit",
            Model.ForEach(countries, c => produce[c] <= limit[c]));

            model.AddGoal("goal", GoalKind.Minimize, Model.Sum(Model.ForEach(countries, c => cost[c] * produce[c])));

            Solution solution = context.Solve(new SimplexDirective());
            Report report = solution.GetReport();
            Console.WriteLine(report);
            Console.ReadLine();

        }

this is my case, i succefully implement normal case..
structure db & data sample :

https://drive.google.com/file/d/0B12-kOJbFzA0QTYxaTZ1TWFGeWs/edit?usp=sharing[^]



https://drive.google.com/file/d/0B12-kOJbFzA0TXZOX2loUktmRHM/edit?usp=sharing[^]
C#
 void sampleExcel()
        {
            SolverContext context = SolverContext.GetContext();
            Model model = context.CreateModel();

            //var djakartaMJO = new Decision(Domain.IntegerNonnegative, "djakartaMJO");
            Decision djakartaMJO = new Decision(Domain.RealNonnegative, "djakartaMJO");
            Decision djakartaUNG = new Decision(Domain.RealNonnegative, "djakartaUNG");
            Decision djakartaCBT = new Decision(Domain.RealNonnegative, "djakartaCBT");
            Decision dbandungMJO = new Decision(Domain.IntegerNonnegative, "dbandungMJO");
            Decision dbandungUNG = new Decision(Domain.IntegerNonnegative, "dbandungUNG");
            Decision dbandungCBT = new Decision(Domain.IntegerNonnegative, "dbandungCBT");
            Decision ddenpasarMJO = new Decision(Domain.IntegerNonnegative, "ddenpasarMJO");
            Decision ddenpasarUNG = new Decision(Domain.IntegerNonnegative, "ddenpasarUNG");
            Decision ddenpasarCBT = new Decision(Domain.IntegerNonnegative, "ddenpasarCBT");
            Decision dlampungMJO = new Decision(Domain.IntegerNonnegative, "dlampungMJO");
            Decision dlampungUNG = new Decision(Domain.IntegerNonnegative, "dlampungUNG");
            Decision dlampungCBT = new Decision(Domain.IntegerNonnegative, "dlampungCBT");
            Decision dmedanMJO = new Decision(Domain.IntegerNonnegative, "dmedanMJO");
            Decision dmedanUNG = new Decision(Domain.IntegerNonnegative, "dmedanUNG");
            Decision dmedanCBT = new Decision(Domain.IntegerNonnegative, "dmedanCBT");

            model.AddDecisions(djakartaMJO, djakartaUNG, djakartaCBT, dbandungMJO, dbandungUNG, dbandungCBT, ddenpasarCBT, ddenpasarMJO, ddenpasarUNG, dlampungCBT, dlampungMJO, dlampungUNG, dmedanCBT, dmedanMJO, dmedanUNG);

            //min cost
            var aa = 6; var ab = 6; var ac = 3;
            var ba = 10; var bb = 2; var bc = 3;
            var ca = 5; var cb = 4; var cc = 7;
            var da = 8; var db = 8; var dc = 7;
            var ea = 9; var eb = 8; var ec = 7;


            model.AddConstraints
                ("DayaTampungColumn",
                djakartaMJO + djakartaUNG + djakartaCBT == 440,
                dbandungMJO + dbandungUNG + dbandungCBT == 360,
                ddenpasarMJO + ddenpasarUNG + ddenpasarCBT == 320,
                dlampungMJO + dlampungUNG + dlampungCBT == 160,
                dmedanMJO + dmedanUNG + dmedanCBT == 400,

                djakartaMJO + dbandungMJO + ddenpasarMJO + dlampungMJO + dmedanMJO <= 620,
                djakartaUNG + dbandungUNG + ddenpasarUNG + dlampungUNG + dmedanUNG <= 520,
                djakartaCBT + dbandungCBT + ddenpasarCBT + dlampungCBT + dmedanCBT <= 560
                );

model.AddGoal
                    ("GOALSPOINT", GoalKind.Minimize,//goal
                    (
                    (aa + 20) * djakartaMJO + (ba + 20) * dbandungMJO + (ca + 20) * ddenpasarMJO + (da + 20) * dlampungMJO + (ea + 20) * dmedanMJO) +
                    (ab + 25) * djakartaUNG + (bb + 25) * dbandungUNG + (cb + 25) * ddenpasarUNG + (db + 25) * dlampungUNG + (eb + 25) * dmedanUNG +
                    (ac + 28) * djakartaCBT + (bc + 28) * dbandungCBT + (cc + 28) * ddenpasarCBT + (dc + 28) * dlampungCBT + (ec + 28) * dmedanCBT
                    );

Solution solution = context.Solve(new SimplexDirective());


Report report = solution.GetReport();
            Console.Write("{0}", report);
            Console.WriteLine();
            Console.ReadLine();</pre>
}



now how did i parameterize it..
i want to know best minimal cost and item to allocated..
still confuse in the constrains and the goal ..

my try still stuck..

 void sampleExcelMin()
        {
            SolverContext context = SolverContext.GetContext();
            Model model = context.CreateModel();
            sifusionEntities mydb = new sifusionEntities();
            

            Set kpbs = new Set(Domain.Any, "KPBs");
            Set kpws = new Set(Domain.Any, "KPWs");
            Set costs1 = new Set(Domain.Any, "COSTs");
            Set costs2 = new Set(Domain.Any, "COSTs");

            ////cost  COST | +  { 6,6,3 ---}
            Parameter cost = new Parameter(Domain.Real, "cost", costs1);
            cost.SetBinding(mydb.COSTs, "COST1","KD_KPB");
           

            //cogm KPB | + { 28, 25, 20 }
            Parameter cogm = new Parameter(Domain.Real, "cogm", costs2);
            cogm.SetBinding(mydb.COSTs, "COGM","KD_KPB");

            //product capacity KPB | <= { 560, 520, 620 }.
            Parameter prodcapacity = new Parameter(Domain.Real, "prodcapacity", kpbs);
            prodcapacity.SetBinding(mydb.KPBs, "PROD_CAPACITY", "KD_KPB");
            
            //dayatampung KPW /demand | == { 440, 360, 320, 400, 160 }.
            Parameter whcapacity = new Parameter(Domain.Real, "whcapacity", kpws);
            whcapacity.SetBinding(mydb.KPWs, "WH_CAPACITY", "KD_KPW");

            model.AddParameters(cost, cogm, prodcapacity, whcapacity);

            //DECISION
            Decision DKPBW = new Decision(Domain.RealNonnegative, "DKPBW",kpbs);
            model.AddDecision(DKPBW);

            
            //kapasitas produksi pabrik kpb
            model.AddConstraint("kpbCapacity",
            //Model.ForEach(kpbs, pb => Model.Sum(Model.Sum(Model.ForEach(kpbs, askpb => DKPBW[askpb, pb] <= prodcapacity[askpb])))));
            Model.Sum(Model.ForEach(kpbs, askpb => DKPBW[askpb] <= prodcapacity[askpb])));
            
            
            //daya tampung kpw 440 360 320  400 160
            model.AddConstraint("kpwLimit",
            Model.ForEach(kpws, askpw => DKPBW[askpw] == whcapacity[askpw]));
            
            
            
            //model.AddConstraint("ProductionLimit",
            //  Model.ForEach(countries, c => produce[c] <= limit[c]));

            ////cost+cogm*dkpw
            //model.AddGoal("goal", GoalKind.Minimize,
            //  Model.Sum(Model.ForEach(kpbs, ac => Model.Sum(Model.ForEach(costs, xx => prmkpbcogm[ac] + prmcost[xx] * DKPW[xx])))));

            //cost+cogm*dkpw
            model.AddGoal("goal", GoalKind.Minimize,
                //Model.Sum(Model.ForEach(costs1, ww => cost[ww] * DKPBW[ww])));
               // Model.Sum(Model.ForEach(costs1, pb => Model.Sum(Model.ForEach(costs1, co => cost[co, pb] * DKPBW[co,pb])))));
               Model.Sum(Model.ForEach(kpbs, ww => cogm[ww] * DKPBW[ww])));
             // Model.Sum(Model.ForEach(countries, c => cost[c] * produce[c])));

            Solution solution = context.Solve();
            Report report = solution.GetReport();
            Console.Write("{0}", report);
            Console.WriteLine(); Console.ReadLine();
}


help..
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900