public static IList <Tuple <Ore, int> > MiningPlanToEarnSpecifiedGoldAndGems(double hpc, double goldNeeded, double gemsNeeded)
        {
            IList <Ore>   localOreList = BuildFeasibleOreList(hpc);
            SolverContext context      = SolverContext.GetContext();
            Model         model        = context.CreateModel();
            Set           items        = new Set(Domain.Any, "items");
            Decision      mine         = new Decision(Domain.IntegerNonnegative, "mine", items);

            model.AddDecision(mine);
            Parameter value = new Parameter(Domain.RealNonnegative, "value", items);

            value.SetBinding(localOreList, "Value", "Name");
            Parameter clicks = new Parameter(Domain.IntegerNonnegative, "clicks", items);

            clicks.SetBinding(localOreList, "Clicks", "Name");

            model.AddParameters(value, clicks);
            model.AddConstraint("knap_value", Model.Sum(Model.ForEach(items, t => mine[t] * value[t])) >= goldNeeded);
            model.AddConstraint("knap_gems", Model.Sum(Model.ForEach(items, t => mine[t])) >= gemsNeeded);
            model.AddGoal("knap_time", GoalKind.Minimize, Model.Sum(Model.ForEach(items, t => mine[t] * clicks[t])));
            var report = context.CheckModel();

            Console.Write(report);
            Solution solution = context.Solve(new SimplexDirective());
            //Report report = solution.GetReport();

            List <Tuple <Ore, int> > retval = new List <Tuple <Ore, int> >();

            for (int i = 0; i < localOreList.Count; i++)
            {
                int temp = (int)mine.GetDouble(i);
                retval.Add(Tuple.Create(localOreList[i], temp));
            }
            return(retval);
        }
示例#2
0
        static void Main(string[] args)
        {
            var    solver      = SolverContext.GetContext();
            var    model       = solver.CreateModel();
            double pArroz      = 20;
            double pAceite     = 280;
            double pHabichuela = 25;
            double presupuesto = 1500;

            var decisionX = new Decision(Domain.IntegerNonnegative, "Arroz");
            var decisionY = new Decision(Domain.IntegerNonnegative, "Aceite");
            var decisionZ = new Decision(Domain.IntegerNonnegative, "Habichuela");

            model.AddDecision(decisionX);
            model.AddDecision(decisionY);
            model.AddDecision(decisionZ);
            var tGoal = (pArroz * decisionX) + (pAceite * decisionY) + (pHabichuela * decisionZ);

            model.AddGoal("Meta", GoalKind.Maximize, tGoal);
            model.AddConstraint("cantminX", decisionX >= 20);
            model.AddConstraint("cantmaxX", decisionX <= 40);
            model.AddConstraint("cantminY", decisionY >= 1);
            model.AddConstraint("cantmaxY", decisionY <= 3);
            model.AddConstraint("cantminZ", decisionZ >= 1);
            model.AddConstraint("cantmaxZ", decisionZ <= 3);
            model.AddConstraint("Total", tGoal >= 1);
            model.AddConstraint("Solucion", tGoal <= presupuesto);
            solver.CheckModel();

            var    solution = solver.Solve();
            var    qa       = solution.Quality;
            var    gls      = solution.Goals;
            double x        = decisionX.GetDouble();
            double y        = decisionY.GetDouble();
            double z        = decisionZ.GetDouble();
            double total    = (x * pArroz) + (y * pAceite) + (z * pHabichuela);

            Console.WriteLine("Cantidades Máximas: ");
            Console.WriteLine("Arroz: " + x + " - Aceite: " + y + " - Habichuela: " + z);
            Console.WriteLine("Solución: " + total);
            Report r = solution.GetReport();

            using (StreamWriter sw = new StreamWriter("ejemplo1.txt"))
            {
                solver.SaveModel(FileFormat.FreeMPS, sw);
            }
        }
示例#3
0
            public static double GetValue(State s, FoeQTable q)
            {
                var contxt = SolverContext.GetContext();
                var model  = contxt.CreateModel();

                var actDecisions = new List <Decision>();

                foreach (var action in Enum.GetNames(typeof(Action)))
                {
                    var decision = new Decision(Domain.RealNonnegative, action);
                    model.AddDecision(decision);
                    actDecisions.Add(decision);
                }

                var val_Decis = new Decision(Domain.Real, "value");

                model.AddDecision(val_Decis);

                model.AddConstraint("probSumConst", actDecisions[0] + actDecisions[1] + actDecisions[2] + actDecisions[3] + actDecisions[4] == 1.0);

                int cons_cnt = 0;

                foreach (Action Player2 in Enum.GetValues(typeof(Action)))
                {
                    var qConstVals = new List <double>();

                    foreach (Action currentPlayer in Enum.GetValues(typeof(Action)))
                    {
                        qConstVals.Add(q.getQval(s, currentPlayer, Player2));
                    }

                    model.AddConstraint("Const" + cons_cnt, qConstVals[0] * actDecisions[0] + qConstVals[1] * actDecisions[1] + qConstVals[2] * actDecisions[2] + qConstVals[3] * actDecisions[3] + qConstVals[4] * actDecisions[4] >= val_Decis);

                    ++cons_cnt;
                }

                model.AddGoal("MaximizeV", GoalKind.Maximize, val_Decis);

                contxt.Solve(new SimplexDirective());

                contxt.ClearModel();

                return(val_Decis.GetDouble());
            }
示例#4
0
        public string[,] Calculate()
        {
            Solution solution = context.Solve();
            Report   report   = solution.GetReport();

            Console.WriteLine(solution.Quality);

            string[,] decisionsValues = new string[model.Decisions.Count(), 2];

            for (int i = 0; i < model.Decisions.Count(); i++)
            {
                Decision d = model.Decisions.ElementAt(i);
                decisionsValues[i, 0] = d.Name;
                decisionsValues[i, 1] = d.GetDouble().ToString();
            }
            context.ClearModel();



            return(decisionsValues);
        }
        public JsonResult Calcular(string n18nom, string input1, string input2)
        {
            /*
             * https://es.symbolab.com/solver/functions-graphing-calculator
             * ddl package = using Microsoft.SolverFoundation.Services;
             * url que permite operar la formula con datos
             */

            var solver    = SolverContext.GetContext();
            var model     = solver.CreateModel();
            var decisionX = new Decision(Domain.IntegerNonnegative, "X");
            var decisionY = new Decision(Domain.IntegerNonnegative, "Y");

            model.AddDecision(decisionX);
            model.AddDecision(decisionY);

            model.AddConstraints("res1", decisionY >= 100);
            model.AddGoal("formula", GoalKind.Maximize,
                          -decisionX);

            double x = decisionX.GetDouble();
            double y = decisionY.GetDouble();

            //resultado = (Math.Log10((Convert.ToDouble(PSI))/(4.5-1.5)));
            //SolverContext context = SolverContext.GetContext();
            //Model model = context.CreateModel();
            //double n18nomf = Convert.ToDouble(n18nom);
            //Decision D = new Decision(Domain.RealNonnegative, "D");
            //model.AddDecision(D);
            //model.AddConstraints("Restriccion", D >= 10);
            //double z = Convert.ToDouble(input1);
            //double r = Convert.ToDouble(input2);
            //double calculo = Convert.ToDouble(D + 25.4);
            //model.AddGoal("Goal", GoalKind.Maximize,(z  * r) + (7.35 * D));
            //Solution solution = context.Solve(new SimplexDirective());
            //Report report = solution.GetReport();
            //double F = solution.Goals.First().ToDouble();
            return(Json(x, JsonRequestBehavior.AllowGet));
        }
        public void OptFunc(double[,] Forcurve,double[,]injectionVol,double[,]withdrawal)
        {
            double[,] PriceArray =  Forcurve;
                double[,] InjectionVol = injectionVol;
                double[,] Withdrawal = withdrawal;

                //is a combination from the price spread array....
                double[,] monthspread = new double[12, 12];
                double results = 0;
                for (int row = 0; row < PriceArray.GetLength(0); row++)
                {

                    int sprow = row; int i = 1;

                    for (int col = sprow; col < PriceArray.GetLength(0) - 1; col++)
                    {
                        results = Math.Round(((PriceArray[row + i, 1] - con.Widthdrawl) - (PriceArray[row, 1] + con.Injection)), 5);
                        monthspread[row, sprow + 1] = results;

                        sprow++;
                        i++;
                    };

                }

                Term goal;
                Term[,] ty;
                Term[,] tv;

                SolverContext context = SolverContext.GetContext();             // Get context environment
                Model model = context.CreateModel();                            // Create a new model

                #region decision and constraints
                //need 12 decisions for every month remaining in the year......
                Decision I1 = new Decision(Domain.RealNonnegative, "I1");
                Decision W1 = new Decision(Domain.RealNonnegative, "W1");
                Decision I2 = new Decision(Domain.RealNonnegative, "I2");
                Decision I3 = new Decision(Domain.RealNonnegative, "I3");
                Decision I4 = new Decision(Domain.RealNonnegative, "I4");
                Decision I5 = new Decision(Domain.RealNonnegative, "I5");
                Decision I6 = new Decision(Domain.RealNonnegative, "I6");
                Decision I7 = new Decision(Domain.RealNonnegative, "I7");
                Decision I8 = new Decision(Domain.RealNonnegative, "I8");
                Decision I9 = new Decision(Domain.RealNonnegative, "I9");
                Decision I10 = new Decision(Domain.RealNonnegative, "I10");
                Decision I11 = new Decision(Domain.RealNonnegative, "I11");
                Decision I12 = new Decision(Domain.RealNonnegative, "I12");
                Decision W2 = new Decision(Domain.RealNonnegative, "W2");
                Decision W3 = new Decision(Domain.RealNonnegative, "W3");
                Decision W4 = new Decision(Domain.RealNonnegative, "W4");
                Decision W5 = new Decision(Domain.RealNonnegative, "W5");
                Decision W6 = new Decision(Domain.RealNonnegative, "W6");
                Decision W7 = new Decision(Domain.RealNonnegative, "W7");
                Decision W8 = new Decision(Domain.RealNonnegative, "W8");
                Decision W9 = new Decision(Domain.RealNonnegative, "W9");
                Decision W10 = new Decision(Domain.RealNonnegative, "W10");
                Decision W11 = new Decision(Domain.RealNonnegative, "W11");
                Decision W12 = new Decision(Domain.RealNonnegative, "W12");
                model.AddDecisions(I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, W1, W2, W3, W4, W5, W6, W7, W8, W9, W10, W11, W12);                                 // Add these to the model (this is where the outputs will be stored)

                model.AddConstraints("limits",
                    //monthly injection withdrawl constraints
                   W1 + Withdrawal[9,1] <= con.JulExport,//13333333.2,
                   I1 + InjectionVol[9, 1] == con.JanImport,//0
                   W2 + Withdrawal[10, 1] <= con.FebExport,//11999999.88,
                   I2 + InjectionVol[10, 1] == con.FebImport,//0,
                   W3 + Withdrawal[11, 1] <= con.MarExport,//5333333.28,
                   I3 + InjectionVol[11, 1] == con.MarImport,//0,
                   W4 + Withdrawal[0,1] == con.AprExport,//0,
                   I4 + InjectionVol[0, 1] == con.AprImport,//0,
                   W5 + Withdrawal[1, 1] == con.MayExport,//0,
                   I5 + InjectionVol[1, 1]<= con.MayImport,//3000000,
                   W6 + Withdrawal[2, 1] == con.JunExport,//0,
                   I6 + InjectionVol[2, 1] <= con.JunImport,//16800000,
                   W7 + Withdrawal[3, 1] == con.JulExport,//0,
                   I7 + InjectionVol[3, 1] <= con.JulImport,//16800000,
                   W8 + Withdrawal[4, 1] == con.AugExport,//0,
                   I8 + InjectionVol[4, 1] <= con.AugImport,//12600000,
                   W9 + Withdrawal[5, 1] == con.SeptExport,//0,
                   I9 + InjectionVol[5, 1] <= con.SeptImport,//10800000,
                   W10 + Withdrawal[6, 1] <= con.OctExport,//6000000,
                   I10 + InjectionVol[6, 1] == con.OctImport,//0,
                   W11 + Withdrawal[7,1] <= con.NovExport,//6000000,
                   I11 + InjectionVol[7, 1] == con.NovImport,//0,
                   W12 + Withdrawal[8, 1] <= con.DecExport,//17333333,
                   I12 + InjectionVol[8, 1] == con.DecImport,//0,

                   //maximum capacity constraints...
                   I4 -  - W4 <= con.MaxCap,
                   I4 - W4 + I5 - W5 <= con.MaxCap,
                   I4 - W4 + I5 - W5 + I6 - W6 <= con.MaxCap,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 <= con.MaxCap,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 <= con.MaxCap,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 <= con.MaxCap,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 <= con.MaxCap,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 <= con.MaxCap,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 + I1 - W1 <= con.MaxCap,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 + I1 - W1 + I2 - W2 <= con.MaxCap,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 + I1 - W1 + I2 - W2 + I3 - W3 <= con.MaxCap,
                    //minimum capacity constraints
                    //you need to take into account any volumes currently in storage...
                   I4 - W4 >= 0,
                   I4 - W4 + I5 - W5 >= 0,
                   I4 - W4 + I5 - W5 + I6 - W6 >= 0,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 >= 0,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 >= 0,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 >= 0,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 >= 0,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 >= 0,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 >= 0,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 + I1 - W1 >= 0,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 + I1 - W1 + I2 - W2 >= 0,
                   I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 + I1 - W1 + I2 - W2 + I3 - W3 == 0
                 );

                #endregion

                ty = matrix(monthspread);
                tv = new Term[,] { { (I4 - W4), (I5 - W5), (I6 - W6), (I7 - W7), (I8 - W8), (I9 - W9), (I10 - W10), (I11 - W11), (I12 - W12), (I1 - W1), (I2 - W2), (I3 - W3) } };

                //to create the goal we need to find the volumes for each month, if injection greater than
                //withdrawals vol is positive vica versa, then multiply by spread and reverse sign to find profit, which is what we want to maximise
                //goal = matMult(matSubtract(tx, tw), ty)[0, 0];
                goal = matMult(tv, ty)[0, 0];
                model.AddGoal("goal", GoalKind.Minimize, goal);

                // Specifying the IPM solver, as we have a quadratic goal
                Solution solution = context.Solve(new InteriorPointMethodDirective());

                //Profit calculation section, you need to store decisions and profit figures......

                //  DataSet SimulationResults = new DataSet();
                #region Fill DataSet

                DataRow rowinfo = Withtable.NewRow();

                rowinfo[0] = Convert.ToDouble(W4.GetDouble());
                rowinfo[1] = Convert.ToDouble(W5.GetDouble());
                rowinfo[2] = Convert.ToDouble(W6.GetDouble());
                rowinfo[3] = Convert.ToDouble(W7.GetDouble());
                rowinfo[4] = Convert.ToDouble(W8.GetDouble());
                rowinfo[5] = Convert.ToDouble(W9.GetDouble());
                rowinfo[6] = Convert.ToDouble(W10.GetDouble());
                rowinfo[7] = Convert.ToDouble(W11.GetDouble());
                rowinfo[8] = Convert.ToDouble(W12.GetDouble());
                rowinfo[9] = Convert.ToDouble(W1.GetDouble());
                rowinfo[10] = Convert.ToDouble(W2.GetDouble());
                rowinfo[11] = Convert.ToDouble(W3.GetDouble());
                SimulationResults.Tables[1].Rows.Add(rowinfo);

                rowinfo = Imptable.NewRow();
                rowinfo[0] = Convert.ToDouble(I4.GetDouble());
                rowinfo[1] = Convert.ToDouble(I5.GetDouble());
                rowinfo[2] = Convert.ToDouble(I6.GetDouble());
                rowinfo[3] = Convert.ToDouble(I7.GetDouble());
                rowinfo[4] = Convert.ToDouble(I8.GetDouble());
                rowinfo[5] = Convert.ToDouble(I9.GetDouble());
                rowinfo[6] = Convert.ToDouble(I10.GetDouble());
                rowinfo[7] = Convert.ToDouble(I11.GetDouble());
                rowinfo[8] = Convert.ToDouble(I12.GetDouble());
                rowinfo[9] = Convert.ToDouble(I1.GetDouble());
                rowinfo[10] = Convert.ToDouble(I2.GetDouble());
                rowinfo[11] = Convert.ToDouble(I3.GetDouble());
                SimulationResults.Tables[2].Rows.Add(rowinfo);

                rowinfo = Proftable.NewRow();
                rowinfo[0] = (Convert.ToDouble(W4.GetDouble()) - Convert.ToDouble(I4.GetDouble())) * PriceArray[0, 1]/100;
                rowinfo[1] = (Convert.ToDouble(W5.GetDouble()) - Convert.ToDouble(I5.GetDouble())) * PriceArray[1, 1]/100;
                rowinfo[2] = (Convert.ToDouble(W6.GetDouble()) - Convert.ToDouble(I6.GetDouble())) * PriceArray[2, 1]/100;
                rowinfo[3] = (Convert.ToDouble(W7.GetDouble()) - Convert.ToDouble(I7.GetDouble())) * PriceArray[3, 1]/100;
                rowinfo[4] = (Convert.ToDouble(W8.GetDouble()) - Convert.ToDouble(I8.GetDouble())) * PriceArray[4, 1]/100;
                rowinfo[5] = (Convert.ToDouble(W9.GetDouble()) - Convert.ToDouble(I9.GetDouble())) * PriceArray[5, 1]/100;
                rowinfo[6] = (Convert.ToDouble(W10.GetDouble()) - Convert.ToDouble(I10.GetDouble())) * PriceArray[6, 1]/100;
                rowinfo[7] = (Convert.ToDouble(W11.GetDouble()) - Convert.ToDouble(I11.GetDouble())) * PriceArray[7, 1]/100;
                rowinfo[8] = (Convert.ToDouble(W12.GetDouble()) - Convert.ToDouble(I12.GetDouble())) * PriceArray[8, 1]/100;
                rowinfo[9] = (Convert.ToDouble(W1.GetDouble()) - Convert.ToDouble(I1.GetDouble())) * PriceArray[9, 1]/100;
                rowinfo[10] = (Convert.ToDouble(W2.GetDouble()) - Convert.ToDouble(I2.GetDouble())) * PriceArray[10, 1]/100;
                rowinfo[11] = (Convert.ToDouble(W3.GetDouble()) - Convert.ToDouble(I3.GetDouble())) * PriceArray[11, 1]/100;
                rowinfo[12] = ((double)rowinfo[0] + (double)rowinfo[1] + (double)rowinfo[2] + (double)rowinfo[3] + (double)rowinfo[4] + (double)rowinfo[5] + (double)rowinfo[6] + (double)rowinfo[7] + (double)rowinfo[8] + (double)rowinfo[9] + (double)rowinfo[10] + (double)rowinfo[11]);
                SimulationResults.Tables[4].Rows.Add(rowinfo);

                rowinfo = ForCurvetable.NewRow();
                rowinfo[0] = PriceArray[0, 1];
                rowinfo[1] = PriceArray[1, 1];
                rowinfo[2] = PriceArray[2, 1];
                rowinfo[3] = PriceArray[3, 1];
                rowinfo[4] = PriceArray[4, 1];
                rowinfo[5] = PriceArray[5, 1];
                rowinfo[6] = PriceArray[6, 1];
                rowinfo[7] = PriceArray[7, 1];
                rowinfo[8] = PriceArray[8, 1];
                rowinfo[9] = PriceArray[9, 1];
                rowinfo[10] = PriceArray[10, 1];
                rowinfo[11] = PriceArray[11, 1];
                SimulationResults.Tables[0].Rows.Add(rowinfo);

                rowinfo = Positiontable.NewRow();
                rowinfo[0] = (Convert.ToDouble(I4.GetDouble()) - Convert.ToDouble(W4.GetDouble()));
                rowinfo[1] = (double)rowinfo[0] + (Convert.ToDouble(I5.GetDouble()) - Convert.ToDouble(W5.GetDouble()));
                rowinfo[2] = (double)rowinfo[1] + (Convert.ToDouble(I6.GetDouble()) - Convert.ToDouble(W6.GetDouble()));
                rowinfo[3] = (double)rowinfo[2] + (Convert.ToDouble(I7.GetDouble()) - Convert.ToDouble(W7.GetDouble()));
                rowinfo[4] = (double)rowinfo[3] + (Convert.ToDouble(I8.GetDouble()) - Convert.ToDouble(W8.GetDouble()));
                rowinfo[5] = (double)rowinfo[4] + (Convert.ToDouble(I9.GetDouble()) - Convert.ToDouble(W9.GetDouble()));
                rowinfo[6] = (double)rowinfo[5] + (Convert.ToDouble(I10.GetDouble()) - Convert.ToDouble(W10.GetDouble()));
                rowinfo[7] = (double)rowinfo[6] + (Convert.ToDouble(I11.GetDouble()) - Convert.ToDouble(W11.GetDouble()));
                rowinfo[8] = (double)rowinfo[7] + (Convert.ToDouble(I12.GetDouble()) - Convert.ToDouble(W12.GetDouble()));
                rowinfo[9] = (double)rowinfo[8] + (Convert.ToDouble(I1.GetDouble()) - Convert.ToDouble(W1.GetDouble()));
                rowinfo[10] = (double)rowinfo[9] + (Convert.ToDouble(I2.GetDouble()) - Convert.ToDouble(W2.GetDouble()));
                rowinfo[11] = (double)rowinfo[10] + (Convert.ToDouble(I3.GetDouble()) - Convert.ToDouble(W3.GetDouble()));
                SimulationResults.Tables[3].Rows.Add(rowinfo);
                #endregion

              //  System.Diagnostics.Process.GetCurrentProcess().Kill();
                context.ClearModel();
        }
示例#7
0
文件: Form1.cs 项目: Ervand1998/Dz2
        private void Raschot_Click(object sender, EventArgs e)
        {
            if (
                #region ---
                (_predel_B1.Text == "") ||
                (_predel_B2.Text == "") ||
                (_predel_B3.Text == "") ||
                (kol_A1_B1.Text == "") ||
                (kol_A1_B2.Text == "") ||
                (kol_A1_B3.Text == "") ||
                (kol_A2_B1.Text == "") ||
                (kol_A2_B2.Text == "") ||
                (kol_A2_B3.Text == "") ||
                (kol_A3_B1.Text == "") ||
                (kol_A3_B2.Text == "") ||
                (kol_A3_B3.Text == "") ||
                (sto_A1.Text == "") ||
                (sto_A2.Text == "") ||
                (sto_A3.Text == ""))
            #endregion ---
            {
                Grafick.Parent = null;
                MessageBox.Show("Не все поля заполнены!", "Ошибка");
                return;
            }
            else
            {
                Grafick.Parent = tabControl1;

                chart1.Series[0].Points.Clear();
                chart1.Series[1].Points.Clear();
                chart1.Series[2].Points.Clear();

                H._predel_B1 = Double.Parse(_predel_B1.Text);
                H._predel_B2 = Double.Parse(_predel_B2.Text);
                H._predel_B3 = Double.Parse(_predel_B3.Text);
                H.kol_A1_B1  = Double.Parse(kol_A1_B1.Text);
                H.kol_A1_B2  = Double.Parse(kol_A1_B2.Text);
                H.kol_A1_B3  = Double.Parse(kol_A1_B3.Text);
                H.kol_A2_B1  = Double.Parse(kol_A2_B1.Text);
                H.kol_A2_B2  = Double.Parse(kol_A2_B2.Text);
                H.kol_A2_B3  = Double.Parse(kol_A2_B3.Text);
                H.kol_A3_B1  = Double.Parse(kol_A3_B1.Text);
                H.kol_A3_B2  = Double.Parse(kol_A3_B2.Text);
                H.kol_A3_B3  = Double.Parse(kol_A3_B3.Text);
                H.sto_A1     = Double.Parse(sto_A1.Text);
                H.sto_A2     = Double.Parse(sto_A2.Text);
                H.sto_A3     = Double.Parse(sto_A3.Text);

                List <SolverRow> solverList = new List <SolverRow>();
                solverList.Add(new SolverRow {
                    xId = 1, Koef = H.X1
                });
                solverList.Add(new SolverRow {
                    xId = 2, Koef = H.X2
                });
                solverList.Add(new SolverRow {
                    xId = 3, Koef = H.X3
                });

                SolverContext context = SolverContext.GetContext();
                Model         model   = context.CreateModel();
                Set           users   = new Set(Domain.Any, "users");

                Parameter Koef = new Parameter(Domain.Real, "Koef", users);
                Koef.SetBinding(solverList, "Koef", "xId");
                model.AddParameter(Koef);

                Decision choose = new Decision(Domain.RealNonnegative, "choose", users);
                model.AddDecisions(choose);
                model.AddGoal("goal", GoalKind.Minimize, Model.Sum(Model.ForEach(users, xId => choose[xId] * Koef[xId])));

                model.AddConstraint("OgranT1", Model.Sum(Model.ForEach(users, xId => H.kol_A1_B1 * H.X1 + H.kol_A2_B1 * H.X2 + H.kol_A3_B1 * H.X3)) <= H._predel_B1);
                model.AddConstraint("OgranT2", Model.Sum(Model.ForEach(users, xId => H.kol_A1_B2 * H.X1 + H.kol_A2_B2 * H.X2 + H.kol_A3_B2 * H.X3)) <= H._predel_B2);
                model.AddConstraint("OgranT3", Model.Sum(Model.ForEach(users, xId => H.kol_A1_B3 * H.X1 + H.kol_A2_B3 * H.X2 + H.kol_A3_B3 * H.X3)) <= H._predel_B3);

                try
                {
                    Solution solution = context.Solve();
                    Report   report   = solution.GetReport();

                    String reportStr = "";

                    for (int i = 0; i < solverList.Count; i++)
                    {
                        reportStr += "Значение X" + (i + 1).ToString() + ": " + choose.GetDouble(solverList[i].xId) + "\n";
                    }
                    reportStr += "\n" + report.ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("При решении задачи оптимизации возникла исключительная ситуация.");
                }

                double X1 = Math.Round(choose.GetDouble(solverList[0].xId), 3);
                double X2 = Math.Round(choose.GetDouble(solverList[1].xId), 3);
                double X3 = Math.Round(choose.GetDouble(solverList[2].xId), 3);

                this.chart1.Series[0].Points.AddXY("", H.X1);
                this.chart1.Series[1].Points.AddXY("", H.X2);
                this.chart1.Series[2].Points.AddXY("", H.X3);

                dataGridView1.Rows.Add(H.X1, H.X2, H.X3);
            }
        }
示例#8
0
        private void bt_Ras_Click(object sender, EventArgs e)
        {
            if (
                #region ---
                (tb_Cu1.Text == "") ||
                (tb_Cu2.Text == "") ||
                (tb_Sn1.Text == "") ||
                (tb_Sn2.Text == "") ||
                (tb_Zn1.Text == "") ||
                (tb_Zn2.Text == "") ||
                (tb_St1.Text == "") ||
                (tb_St2.Text == ""))
            #endregion ---
            {
                Grafick.Parent = null;
                MessageBox.Show("Не все поля заполнены!", "Ошибка");
                return;
            }
            else
            {
                Grafick.Parent = tabControl1;

                chart1.Series[0].Points.Clear();
                chart1.Series[1].Points.Clear();

                st.Cu1 = Double.Parse(tb_Cu1.Text);
                st.Cu2 = Double.Parse(tb_Cu2.Text);
                st.Sn1 = Double.Parse(tb_Sn1.Text);
                st.Sn2 = Double.Parse(tb_Sn1.Text);
                st.Zn1 = Double.Parse(tb_Zn1.Text);
                st.Zn2 = Double.Parse(tb_Zn2.Text);
                st.St1 = Double.Parse(tb_St1.Text);
                st.St2 = Double.Parse(tb_St2.Text);


                List <SolverRow> solverList = new List <SolverRow>();
                solverList.Add(new SolverRow {
                    xId = 1, Koef = st.Ras_Sp1
                });
                solverList.Add(new SolverRow {
                    xId = 2, Koef = st.Ras_Sp2
                });


                SolverContext context = SolverContext.GetContext();
                Model         model   = context.CreateModel();
                Set           users   = new Set(Domain.Any, "users");

                Parameter Koef = new Parameter(Domain.Real, "Koef", users);
                Koef.SetBinding(solverList, "Koef", "xId");
                model.AddParameter(Koef);

                Decision choose = new Decision(Domain.RealNonnegative, "choose", users);
                model.AddDecisions(choose);
                model.AddGoal("goal", GoalKind.Minimize, Model.Sum(Model.ForEach(users, xId => choose[xId] * Koef[xId])));



                model.AddConstraint("Ogran1", Model.Sum(Model.ForEach(users, xId => (st.Cu1 * st.Ras_Sp1 + st.Cu2 * st.Ras_Sp2) * 0.01)) <= 2);
                model.AddConstraint("Ogran2", Model.Sum(Model.ForEach(users, xId => (st.Sn1 * st.Ras_Sp1 + st.Sn2 * st.Ras_Sp2) * 0.01)) <= 1000);
                model.AddConstraint("Ogran3", Model.Sum(Model.ForEach(users, xId => (st.Zn1 * st.Ras_Sp1 + st.Zn2 * st.Ras_Sp2) * 0.01)) <= 12.8);


                try
                {
                    Solution solution = context.Solve();
                    Report   report   = solution.GetReport();

                    String reportStr = "";

                    for (int i = 0; i < solverList.Count; i++)
                    {
                        reportStr += "Значение X" + (i + 1).ToString() + ": " + choose.GetDouble(solverList[i].xId) + "\n";
                    }
                    reportStr += "\n" + report.ToString();
                }

                catch (Exception ex)
                {
                    MessageBox.Show("При решении задачи оптимизации возникла исключительная ситуация.");
                }

                double Ras_Sp1 = Math.Round(choose.GetDouble(solverList[0].xId), 3);
                double Ras_Sp2 = Math.Round(choose.GetDouble(solverList[1].xId), 3);


                this.chart1.Series[0].Points.AddXY("", st.Ras_Sp1);
                this.chart1.Series[1].Points.AddXY("", st.Ras_Sp2);

                dataGridView1.Rows.Add(st.Ras_Sp1, st.Ras_Sp2, st.Ras_O_St);
            }
        }
示例#9
0
        public double[] SolverSteelMeltingMixture(Variants variants)
        {
            _variants = variants;

            #region --- Решение с использованием Solver Foundation

            SteelMeltingMixtureDatabase _database  = new SteelMeltingMixtureDatabase();
            List <SolverRowModel>       solverList = new List <SolverRowModel>();
            double[] mKoefNerav = new double[_components.Count];

            // заполнить списки
            int countItemComponents = 0;
            foreach (Components itemComponents in _components)
            {
                int countItemElements = 0;
                foreach (Elements itemElements in _elements)
                {
                    var db_ValElement = _database.ComponentsElements
                                        .Where(p => p.ID_Component == itemComponents.ID_Component &&
                                               p.ID_Element == itemElements.ID_Element &&
                                               p.ID_Variant == _variants.ID_Variant &&
                                               p.Owner.ID_User == _users.CurrentUser.ID_User &&
                                               p.IsSolve == true)
                                        .FirstOrDefault();
                    mKoefNerav[countItemElements] = 0.01 * db_ValElement.Val;
                    countItemElements++;
                }

                var db_Components = _database.Components
                                    .Where(p => p.ID_Component == itemComponents.ID_Component &&
                                           p.Owner.ID_User == _users.CurrentUser.ID_User &&
                                           p.IsSolve == true)
                                    .FirstOrDefault();

                switch (_maxElements)
                {
                case 2:

                    solverList.Add(new SolverRowModel
                    {
                        xId        = countItemComponents + 1,
                        Koef       = 0.01 * db_Components.Cost,
                        Min        = db_Components.MinValComponent,
                        Max        = db_Components.MaxValComponent,
                        KoefNerav1 = mKoefNerav[0],
                        KoefNerav2 = mKoefNerav[1]
                    });

                    break;

                case 3:
                    solverList.Add(new SolverRowModel
                    {
                        xId        = countItemComponents + 1,
                        Koef       = 0.01 * db_Components.Cost,
                        Min        = db_Components.MinValComponent,
                        Max        = db_Components.MaxValComponent,
                        KoefNerav1 = mKoefNerav[0],
                        KoefNerav2 = mKoefNerav[1],
                        KoefNerav3 = mKoefNerav[2],
                    });
                    break;

                case 4:
                    solverList.Add(new SolverRowModel
                    {
                        xId        = countItemComponents + 1,
                        Koef       = 0.01 * db_Components.Cost,
                        Min        = db_Components.MinValComponent,
                        Max        = db_Components.MaxValComponent,
                        KoefNerav1 = mKoefNerav[0],
                        KoefNerav2 = mKoefNerav[1],
                        KoefNerav3 = mKoefNerav[2],
                        KoefNerav4 = mKoefNerav[3],
                    });
                    break;

                default:
                    break;
                }
                countItemComponents++;
            }

            SolverContext context = SolverContext.GetContext();
            Model         model   = context.CreateModel();

            Set users = new Set(Domain.Any, "users");

            Parameter k = new Parameter(Domain.Real, "Koef", users);
            k.SetBinding(solverList, "Koef", "xId");

            Parameter min = new Parameter(Domain.Real, "Min", users);
            min.SetBinding(solverList, "Min", "xId");

            Parameter max = new Parameter(Domain.Real, "Max", users);
            max.SetBinding(solverList, "Max", "xId");

            Parameter[] KoefNerav = new Parameter[_maxElements];

            for (int i = 0; i < KoefNerav.Length; i++)
            {
                KoefNerav[i] = new Parameter(Microsoft.SolverFoundation.Services.Domain.Real, "KoefNerav" + (i + 1).ToString(), users);
                KoefNerav[i].SetBinding(solverList, "KoefNerav" + (i + 1).ToString(), "xId");
            }

            Parameter[] KoefNeravRows = new Parameter[3 + _maxElements];
            KoefNeravRows[0] = k;
            KoefNeravRows[1] = min;
            KoefNeravRows[2] = max;
            for (int i = 3; i < KoefNeravRows.Length; i++) // заполняем, начиная с 3 элемента
            {
                KoefNeravRows[i] = KoefNerav[i - 3];
            }

            model.AddParameters(KoefNeravRows);

            Decision choose = new Decision(Domain.RealNonnegative, "choose", users);
            model.AddDecision(choose);

            model.AddGoal("goal", GoalKind.Minimize, Model.Sum(Model.ForEach(users, xId => choose[xId] * k[xId])));

            // Добавить ограничения-неравенства по каждому химическому элементу
            int count = 0;
            foreach (Elements itemElements in _elements)
            {
                var db_MinValElement = _database.Elements
                                       .Where(p => p.ID_Element == itemElements.ID_Element &&
                                              p.Owner.ID_User == _users.CurrentUser.ID_User &&
                                              p.IsSolve == true)
                                       .FirstOrDefault();
                model.AddConstraint("Nerav_Max_" + (count + 1).ToString(), Model.Sum(Model.ForEach(users, xId => KoefNeravRows[count + 3][xId] * choose[xId])) <= db_MinValElement.MaxValElement);
                model.AddConstraint("Nerav_Min_" + (count + 1).ToString(), Model.Sum(Model.ForEach(users, xId => KoefNeravRows[count + 3][xId] * choose[xId])) >= db_MinValElement.MinValElement);
                count++;
            }

            // Добавить прямые ограничения по каждой переменной
            model.AddConstraint("c_choose", Model.ForEach(users, xId => (min[xId] <= choose[xId] <= max[xId])));

            // Добавить ограничение по сумме всех переменных, сумма = 100 %
            model.AddConstraint("c_sum", Model.Sum(Model.ForEach(users, xId => choose[xId])) == 100);

            try
            {
                Solution solution = context.Solve();
                Report   report   = solution.GetReport();

                String reportStr = "";

                for (int i = 0; i < solverList.Count; i++)
                {
                    reportStr += "Значение X" + (i + 1).ToString() + ": " + choose.GetDouble(solverList[i].xId) + "\n";
                }
                reportStr += "\n" + report.ToString();

                //MessageBox.Show(reportStr);
            }
            catch (Exception ex)
            {
                //MessageBox.Show("При решении задачи оптимизации возникла исключительная ситуация");
            }

            #endregion --- Решение с использованием Solver Foundation

            #region --- Формирование массива с результатами расчета

            double[] out_values = new double[_maxComponents + 1]; // для каждого Xi + величина целевой функции

            double SumCost = 0;                                   // целевая функция (суммарная стоимость шихты)
            for (int i = 0; i < out_values.Length - 1; i++)
            {
                out_values[i] = Math.Round(choose.GetDouble(solverList[i].xId), 2);
                SumCost       = SumCost + Math.Round(solverList[i].Koef * choose.GetDouble(solverList[i].xId), 2);
            }
            out_values[_maxComponents] = Math.Round(SumCost, 2); // последний элемент массива - это целевая функция (суммарная стоимость шихты)

            #endregion --- Формирование массива с результатами расчета

            return(out_values);
        }
示例#10
0
        public double[] SolverSteelMeltingMixture(InputDataModel idm)
        {
            _idm = idm;

            #region --- Решение с использованием Solver Foundation

            List <SolverRowModel> solverList = new List <SolverRowModel>();

            //Добавить по X1
            solverList.Add(new SolverRowModel
            {
                xId        = 1,
                Koef       = 0.01 * _idm.CostComponent_1,
                Min        = _idm.ComponentMin_1,
                Max        = _idm.ComponentMax_1,
                KoefNerav1 = 0.01 * _idm.Si_InComponent_1,
                KoefNerav2 = 0.01 * _idm.Mn_InComponent_1,
                KoefNerav3 = 0.01 * _idm.Cr_InComponent_1,
                KoefNerav4 = 0.01 * _idm.Ni_InComponent_1
            });

            // Добавить по X2
            solverList.Add(new SolverRowModel
            {
                xId        = 2,
                Koef       = 0.01 * _idm.CostComponent_2,
                Min        = _idm.ComponentMin_2,
                Max        = _idm.ComponentMax_2,
                KoefNerav1 = 0.01 * _idm.Si_InComponent_2,
                KoefNerav2 = 0.01 * _idm.Mn_InComponent_2,
                KoefNerav3 = 0.01 * _idm.Cr_InComponent_2,
                KoefNerav4 = 0.01 * _idm.Ni_InComponent_2
            });

            // Добавить по X3
            solverList.Add(new SolverRowModel
            {
                xId        = 3,
                Koef       = 0.01 * _idm.CostComponent_3,
                Min        = _idm.ComponentMin_3,
                Max        = _idm.ComponentMax_3,
                KoefNerav1 = 0.01 * _idm.Si_InComponent_3,
                KoefNerav2 = 0.01 * _idm.Mn_InComponent_3,
                KoefNerav3 = 0.01 * _idm.Cr_InComponent_3,
                KoefNerav4 = 0.01 * _idm.Ni_InComponent_3
            });

            // Добавить по X4
            solverList.Add(new SolverRowModel
            {
                xId        = 4,
                Koef       = 0.01 * _idm.CostComponent_4,
                Min        = _idm.ComponentMin_4,
                Max        = _idm.ComponentMax_4,
                KoefNerav1 = 0.01 * _idm.Si_InComponent_4,
                KoefNerav2 = 0.01 * _idm.Mn_InComponent_4,
                KoefNerav3 = 0.01 * _idm.Cr_InComponent_4,
                KoefNerav4 = 0.01 * _idm.Ni_InComponent_4
            });

            // Добавить по X5
            solverList.Add(new SolverRowModel
            {
                xId        = 5,
                Koef       = 0.01 * _idm.CostComponent_5,
                Min        = _idm.ComponentMin_5,
                Max        = _idm.ComponentMax_5,
                KoefNerav1 = 0.01 * _idm.Si_InComponent_5,
                KoefNerav2 = 0.01 * _idm.Mn_InComponent_5,
                KoefNerav3 = 0.01 * _idm.Cr_InComponent_5,
                KoefNerav4 = 0.01 * _idm.Ni_InComponent_5
            });

            // Добавить по X6
            solverList.Add(new SolverRowModel
            {
                xId        = 6,
                Koef       = 0.01 * _idm.CostComponent_6,
                Min        = _idm.ComponentMin_6,
                Max        = _idm.ComponentMax_6,
                KoefNerav1 = 0.01 * _idm.Si_InComponent_6,
                KoefNerav2 = 0.01 * _idm.Mn_InComponent_6,
                KoefNerav3 = 0.01 * _idm.Cr_InComponent_6,
                KoefNerav4 = 0.01 * _idm.Ni_InComponent_6
            });

            // Добавить по X7
            solverList.Add(new SolverRowModel
            {
                xId        = 7,
                Koef       = 0.01 * _idm.CostComponent_7,
                Min        = _idm.ComponentMin_7,
                Max        = _idm.ComponentMax_7,
                KoefNerav1 = 0.01 * _idm.Si_InComponent_7,
                KoefNerav2 = 0.01 * _idm.Mn_InComponent_7,
                KoefNerav3 = 0.01 * _idm.Cr_InComponent_7,
                KoefNerav4 = 0.01 * _idm.Ni_InComponent_7
            });

            SolverContext context = SolverContext.GetContext();
            Model         model   = context.CreateModel();

            Set users = new Set(Domain.Any, "users");

            Parameter k = new Parameter(Domain.Real, "Koef", users);
            k.SetBinding(solverList, "Koef", "xId");

            Parameter min = new Parameter(Domain.Real, "Min", users);
            min.SetBinding(solverList, "Min", "xId");

            Parameter max = new Parameter(Domain.Real, "Max", users);
            max.SetBinding(solverList, "Max", "xId");

            Parameter KoefNerav1 = new Parameter(Domain.Real, "KoefNerav1", users);
            KoefNerav1.SetBinding(solverList, "KoefNerav1", "xId");

            Parameter KoefNerav2 = new Parameter(Domain.Real, "KoefNerav2", users);
            KoefNerav2.SetBinding(solverList, "KoefNerav2", "xId");

            Parameter KoefNerav3 = new Parameter(Domain.Real, "KoefNerav3", users);
            KoefNerav3.SetBinding(solverList, "KoefNerav3", "xId");

            Parameter KoefNerav4 = new Parameter(Domain.Real, "KoefNerav4", users);
            KoefNerav4.SetBinding(solverList, "KoefNerav4", "xId");

            model.AddParameters(k, min, max, KoefNerav1, KoefNerav2, KoefNerav3, KoefNerav4);

            Decision choose = new Decision(Domain.RealNonnegative, "choose", users);
            model.AddDecision(choose);

            model.AddGoal("goal", GoalKind.Minimize, Model.Sum(Model.ForEach(users, xId => choose[xId] * k[xId])));

            // Добавить ограничения-неравенства по [Si]
            model.AddConstraint("Nerav_Si_Max", Model.Sum(Model.ForEach(users, xId => KoefNerav1[xId] * choose[xId])) <= _idm.Si_InShihtaNeed_Max);
            model.AddConstraint("Nerav_Si_Min", Model.Sum(Model.ForEach(users, xId => KoefNerav1[xId] * choose[xId])) >= _idm.Si_InShihtaNeed_Min);

            // Добавить ограничения-неравенства по [Mn]
            model.AddConstraint("Nerav_Mn_Max", Model.Sum(Model.ForEach(users, xId => KoefNerav2[xId] * choose[xId])) <= _idm.Mn_InShihtaNeed_Max);
            model.AddConstraint("Nerav_Mn_Min", Model.Sum(Model.ForEach(users, xId => KoefNerav2[xId] * choose[xId])) >= _idm.Mn_InShihtaNeed_Min);

            // Добавить ограничения-неравенства по [Cr]
            model.AddConstraint("Nerav_Cr_Max", Model.Sum(Model.ForEach(users, xId => KoefNerav3[xId] * choose[xId])) <= _idm.Cr_InShihtaNeed_Max);
            model.AddConstraint("Nerav_Cr_Min", Model.Sum(Model.ForEach(users, xId => KoefNerav3[xId] * choose[xId])) >= _idm.Cr_InShihtaNeed_Min);

            // Добавить ограничения-неравенства по [Ni]
            model.AddConstraint("Nerav_Ni_Max", Model.Sum(Model.ForEach(users, xId => KoefNerav4[xId] * choose[xId])) <= _idm.Ni_InShihtaNeed_Max);
            model.AddConstraint("Nerav_Ni_Min", Model.Sum(Model.ForEach(users, xId => KoefNerav4[xId] * choose[xId])) >= _idm.Ni_InShihtaNeed_Min);

            // Добавить прямые ограничения по каждой переменной
            model.AddConstraint("c_choose", Model.ForEach(users, xId => (min[xId] <= choose[xId] <= max[xId])));

            // Добавить ограничение по сумме всех переменных, сумма = 100 %
            model.AddConstraint("c_sum", Model.Sum(Model.ForEach(users, xId => choose[xId])) == 100);

            try
            {
                Solution solution = context.Solve();
                Report   report   = solution.GetReport();

                String reportStr = "";

                for (int i = 0; i < solverList.Count; i++)
                {
                    reportStr += "Значение X" + (i + 1).ToString() + ": " + choose.GetDouble(solverList[i].xId) + "\n";
                }
                reportStr += "\n" + report.ToString();

                //MessageBox.Show(reportStr);
            }
            catch (Exception ex)
            {
                //MessageBox.Show("При решении задачи оптимизации возникла исключительная ситуация");
            }

            #endregion --- Решение с использованием Solver Foundation

            double[] out_values = new double[10];

            out_values[0] = Math.Round(choose.GetDouble(solverList[0].xId), 2);
            out_values[1] = Math.Round(choose.GetDouble(solverList[1].xId), 2);
            out_values[2] = Math.Round(choose.GetDouble(solverList[2].xId), 2);
            out_values[3] = Math.Round(choose.GetDouble(solverList[3].xId), 2);
            out_values[4] = Math.Round(choose.GetDouble(solverList[4].xId), 2);
            out_values[5] = Math.Round(choose.GetDouble(solverList[5].xId), 2);
            out_values[6] = Math.Round(choose.GetDouble(solverList[6].xId), 2);

            return(out_values);
        }
示例#11
0
        static void Main(string[] args)
        {


            //Define Cost price of fruits
            double costApple = 10;
            double costPear = 15;
            double costBanana = 25;

            //Define Sales price of fruits
            double priceApple = 30;
            double pricePear = 30;
            double priceBanana = 60;

            //Define weight of fruits
            double weightApple = 50;
            double weightPear = 20;
            double weightBanana = 80;

            //Maximal budget
            double budget = 200;

            //Maximal weight
            double weightCapacity = 500;


            //Initiate/Create Solver
            var solver = SolverContext.GetContext();
            var model = solver.CreateModel();

            //Define variable which should be solved - result
            var decisionApple = new Decision(Domain.IntegerNonnegative, "AmountApples");
            var decisionPear = new Decision(Domain.IntegerNonnegative, "AmountPears");
            var decisionBanana = new Decision(Domain.IntegerRange(1, 100), "AmountBananas");

            //Add variables to the model
            model.AddDecision(decisionApple);
            model.AddDecision(decisionPear);
            model.AddDecision(decisionBanana);

            //Define what should be optimized - we want maximal profit
            model.AddGoal("GoalCost", GoalKind.Maximize,
                (priceApple - costApple) * decisionApple +
                (pricePear - costPear) * decisionPear +
                (priceBanana - costBanana) * decisionBanana
            );

            //Test to optimize weight capacity
            /*model.AddGoal("GoalWeight", GoalKind.Maximize,
                weightApple * decisionApple +
                weightPear * decisionPear +
                weightBanana * decisionBanana
            );*/

            //Add constraints
            //Maximum budget constraint
            model.AddConstraint("Budget",
                costApple * decisionApple +
                costPear * decisionPear +
                costBanana * decisionBanana
                <= budget
            );

            //Maximum weight capacity constraint
            model.AddConstraint("Weight",
                weightApple * decisionApple +
                weightPear * decisionPear +
                weightBanana * decisionBanana
                <= weightCapacity
            );

            var solution = solver.Solve();

            double totalweight = decisionApple.GetDouble() * weightApple + decisionPear.GetDouble() * weightPear + decisionBanana.GetDouble() * weightBanana;
            double totalCost = decisionApple.GetDouble() * costApple + decisionPear.GetDouble() * costPear + decisionBanana.GetDouble() * costBanana;
            double totalProfit = decisionApple.GetDouble() * (priceApple - costApple) + decisionPear.GetDouble() * (pricePear - costPear) + decisionBanana.GetDouble() * (priceBanana - costBanana);

            Console.WriteLine("Constraint Satisfaction Problem - Microsoft Solver Foundation");
            Console.WriteLine("");

            Console.WriteLine("Fruit retailer");
            Console.WriteLine("");

            Console.WriteLine("Apple: cost {0}, price {1}, weight {2}", costApple, priceApple, weightApple);
            Console.WriteLine("Pear:  cost {0}, price {1}, weight {2}", costPear, pricePear, weightPear);
            Console.WriteLine("Apple: cost {0}, price {1}, weight {2}", costBanana, priceBanana, weightBanana);
            Console.WriteLine("");

            Console.WriteLine("Maximal budget: {0}", budget);
            Console.WriteLine("Maximal weight capacity: {0}", weightCapacity);
            Console.WriteLine("");

            Console.WriteLine("******************************************************");
            Console.WriteLine("");

            Console.WriteLine("Solve the problem");
            Console.WriteLine("Load truck with fruit to MAXIMIZE profit");
            Console.WriteLine("");

            Console.WriteLine("Apple = " + decisionApple.GetDouble());
            Console.WriteLine("Pear = " + decisionPear.GetDouble());
            Console.WriteLine("Banana = " + decisionBanana.GetDouble());
            Console.WriteLine("");

            Console.WriteLine("Total Weight = {0} ", totalweight);
            Console.WriteLine("Total Cost = {0} ", totalCost);
            Console.WriteLine("Total Profit = {0} ", totalProfit);
            Console.WriteLine("");

            Console.WriteLine("*****************************************************");
            Console.WriteLine("");
            Console.WriteLine("Solution quality: " + solution.Quality);

            Console.ReadLine();


        } // Main
        public void OptFunc(double[,] Forcurve, double[,] injectionVol, double[,] withdrawal)
        {
            double[,] PriceArray   = Forcurve;
            double[,] InjectionVol = injectionVol;
            double[,] Withdrawal   = withdrawal;

            //is a combination from the price spread array....
            double[,] monthspread = new double[12, 12];
            double results = 0;

            for (int row = 0; row < PriceArray.GetLength(0); row++)
            {
                int sprow = row; int i = 1;

                for (int col = sprow; col < PriceArray.GetLength(0) - 1; col++)
                {
                    results = Math.Round(((PriceArray[row + i, 1] - con.Widthdrawl) - (PriceArray[row, 1] + con.Injection)), 5);
                    monthspread[row, sprow + 1] = results;

                    sprow++;
                    i++;
                }
                ;
            }

            Term goal;

            Term[,] ty;
            Term[,] tv;


            SolverContext context = SolverContext.GetContext();                 // Get context environment
            Model         model   = context.CreateModel();                      // Create a new model

            #region decision and constraints
            //need 12 decisions for every month remaining in the year......
            Decision I1  = new Decision(Domain.RealNonnegative, "I1");
            Decision W1  = new Decision(Domain.RealNonnegative, "W1");
            Decision I2  = new Decision(Domain.RealNonnegative, "I2");
            Decision I3  = new Decision(Domain.RealNonnegative, "I3");
            Decision I4  = new Decision(Domain.RealNonnegative, "I4");
            Decision I5  = new Decision(Domain.RealNonnegative, "I5");
            Decision I6  = new Decision(Domain.RealNonnegative, "I6");
            Decision I7  = new Decision(Domain.RealNonnegative, "I7");
            Decision I8  = new Decision(Domain.RealNonnegative, "I8");
            Decision I9  = new Decision(Domain.RealNonnegative, "I9");
            Decision I10 = new Decision(Domain.RealNonnegative, "I10");
            Decision I11 = new Decision(Domain.RealNonnegative, "I11");
            Decision I12 = new Decision(Domain.RealNonnegative, "I12");
            Decision W2  = new Decision(Domain.RealNonnegative, "W2");
            Decision W3  = new Decision(Domain.RealNonnegative, "W3");
            Decision W4  = new Decision(Domain.RealNonnegative, "W4");
            Decision W5  = new Decision(Domain.RealNonnegative, "W5");
            Decision W6  = new Decision(Domain.RealNonnegative, "W6");
            Decision W7  = new Decision(Domain.RealNonnegative, "W7");
            Decision W8  = new Decision(Domain.RealNonnegative, "W8");
            Decision W9  = new Decision(Domain.RealNonnegative, "W9");
            Decision W10 = new Decision(Domain.RealNonnegative, "W10");
            Decision W11 = new Decision(Domain.RealNonnegative, "W11");
            Decision W12 = new Decision(Domain.RealNonnegative, "W12");
            model.AddDecisions(I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, W1, W2, W3, W4, W5, W6, W7, W8, W9, W10, W11, W12);                                     // Add these to the model (this is where the outputs will be stored)

            model.AddConstraints("limits",
                                 //monthly injection withdrawl constraints
                                 W1 + Withdrawal[9, 1] <= con.JulExport,    //13333333.2,
                                 I1 + InjectionVol[9, 1] == con.JanImport,  //0
                                 W2 + Withdrawal[10, 1] <= con.FebExport,   //11999999.88,
                                 I2 + InjectionVol[10, 1] == con.FebImport, //0,
                                 W3 + Withdrawal[11, 1] <= con.MarExport,   //5333333.28,
                                 I3 + InjectionVol[11, 1] == con.MarImport, //0,
                                 W4 + Withdrawal[0, 1] == con.AprExport,    //0,
                                 I4 + InjectionVol[0, 1] == con.AprImport,  //0,
                                 W5 + Withdrawal[1, 1] == con.MayExport,    //0,
                                 I5 + InjectionVol[1, 1] <= con.MayImport,  //3000000,
                                 W6 + Withdrawal[2, 1] == con.JunExport,    //0,
                                 I6 + InjectionVol[2, 1] <= con.JunImport,  //16800000,
                                 W7 + Withdrawal[3, 1] == con.JulExport,    //0,
                                 I7 + InjectionVol[3, 1] <= con.JulImport,  //16800000,
                                 W8 + Withdrawal[4, 1] == con.AugExport,    //0,
                                 I8 + InjectionVol[4, 1] <= con.AugImport,  //12600000,
                                 W9 + Withdrawal[5, 1] == con.SeptExport,   //0,
                                 I9 + InjectionVol[5, 1] <= con.SeptImport, //10800000,
                                 W10 + Withdrawal[6, 1] <= con.OctExport,   //6000000,
                                 I10 + InjectionVol[6, 1] == con.OctImport, //0,
                                 W11 + Withdrawal[7, 1] <= con.NovExport,   //6000000,
                                 I11 + InjectionVol[7, 1] == con.NovImport, //0,
                                 W12 + Withdrawal[8, 1] <= con.DecExport,   //17333333,
                                 I12 + InjectionVol[8, 1] == con.DecImport, //0,

                                                                            //maximum capacity constraints...
                                 I4 -  -W4 <= con.MaxCap,
                                 I4 - W4 + I5 - W5 <= con.MaxCap,
                                 I4 - W4 + I5 - W5 + I6 - W6 <= con.MaxCap,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 <= con.MaxCap,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 <= con.MaxCap,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 <= con.MaxCap,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 <= con.MaxCap,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 <= con.MaxCap,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 + I1 - W1 <= con.MaxCap,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 + I1 - W1 + I2 - W2 <= con.MaxCap,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 + I1 - W1 + I2 - W2 + I3 - W3 <= con.MaxCap,
                                 //minimum capacity constraints
                                 //you need to take into account any volumes currently in storage...
                                 I4 - W4 >= 0,
                                 I4 - W4 + I5 - W5 >= 0,
                                 I4 - W4 + I5 - W5 + I6 - W6 >= 0,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 >= 0,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 >= 0,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 >= 0,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 >= 0,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 >= 0,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 >= 0,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 + I1 - W1 >= 0,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 + I1 - W1 + I2 - W2 >= 0,
                                 I4 - W4 + I5 - W5 + I6 - W6 + I7 - W7 + I8 - W8 + I9 - W9 + I10 - W10 + I11 - W11 + I12 - W12 + I1 - W1 + I2 - W2 + I3 - W3 == 0
                                 );

            #endregion

            ty = matrix(monthspread);
            tv = new Term[, ] {
                { (I4 - W4), (I5 - W5), (I6 - W6), (I7 - W7), (I8 - W8), (I9 - W9), (I10 - W10), (I11 - W11), (I12 - W12), (I1 - W1), (I2 - W2), (I3 - W3) }
            };

            //to create the goal we need to find the volumes for each month, if injection greater than
            //withdrawals vol is positive vica versa, then multiply by spread and reverse sign to find profit, which is what we want to maximise
            //goal = matMult(matSubtract(tx, tw), ty)[0, 0];
            goal = matMult(tv, ty)[0, 0];
            model.AddGoal("goal", GoalKind.Minimize, goal);

            // Specifying the IPM solver, as we have a quadratic goal
            Solution solution = context.Solve(new InteriorPointMethodDirective());


            //Profit calculation section, you need to store decisions and profit figures......

            //  DataSet SimulationResults = new DataSet();
            #region Fill DataSet

            DataRow rowinfo = Withtable.NewRow();

            rowinfo[0]  = Convert.ToDouble(W4.GetDouble());
            rowinfo[1]  = Convert.ToDouble(W5.GetDouble());
            rowinfo[2]  = Convert.ToDouble(W6.GetDouble());
            rowinfo[3]  = Convert.ToDouble(W7.GetDouble());
            rowinfo[4]  = Convert.ToDouble(W8.GetDouble());
            rowinfo[5]  = Convert.ToDouble(W9.GetDouble());
            rowinfo[6]  = Convert.ToDouble(W10.GetDouble());
            rowinfo[7]  = Convert.ToDouble(W11.GetDouble());
            rowinfo[8]  = Convert.ToDouble(W12.GetDouble());
            rowinfo[9]  = Convert.ToDouble(W1.GetDouble());
            rowinfo[10] = Convert.ToDouble(W2.GetDouble());
            rowinfo[11] = Convert.ToDouble(W3.GetDouble());
            SimulationResults.Tables[1].Rows.Add(rowinfo);

            rowinfo     = Imptable.NewRow();
            rowinfo[0]  = Convert.ToDouble(I4.GetDouble());
            rowinfo[1]  = Convert.ToDouble(I5.GetDouble());
            rowinfo[2]  = Convert.ToDouble(I6.GetDouble());
            rowinfo[3]  = Convert.ToDouble(I7.GetDouble());
            rowinfo[4]  = Convert.ToDouble(I8.GetDouble());
            rowinfo[5]  = Convert.ToDouble(I9.GetDouble());
            rowinfo[6]  = Convert.ToDouble(I10.GetDouble());
            rowinfo[7]  = Convert.ToDouble(I11.GetDouble());
            rowinfo[8]  = Convert.ToDouble(I12.GetDouble());
            rowinfo[9]  = Convert.ToDouble(I1.GetDouble());
            rowinfo[10] = Convert.ToDouble(I2.GetDouble());
            rowinfo[11] = Convert.ToDouble(I3.GetDouble());
            SimulationResults.Tables[2].Rows.Add(rowinfo);

            rowinfo     = Proftable.NewRow();
            rowinfo[0]  = (Convert.ToDouble(W4.GetDouble()) - Convert.ToDouble(I4.GetDouble())) * PriceArray[0, 1] / 100;
            rowinfo[1]  = (Convert.ToDouble(W5.GetDouble()) - Convert.ToDouble(I5.GetDouble())) * PriceArray[1, 1] / 100;
            rowinfo[2]  = (Convert.ToDouble(W6.GetDouble()) - Convert.ToDouble(I6.GetDouble())) * PriceArray[2, 1] / 100;
            rowinfo[3]  = (Convert.ToDouble(W7.GetDouble()) - Convert.ToDouble(I7.GetDouble())) * PriceArray[3, 1] / 100;
            rowinfo[4]  = (Convert.ToDouble(W8.GetDouble()) - Convert.ToDouble(I8.GetDouble())) * PriceArray[4, 1] / 100;
            rowinfo[5]  = (Convert.ToDouble(W9.GetDouble()) - Convert.ToDouble(I9.GetDouble())) * PriceArray[5, 1] / 100;
            rowinfo[6]  = (Convert.ToDouble(W10.GetDouble()) - Convert.ToDouble(I10.GetDouble())) * PriceArray[6, 1] / 100;
            rowinfo[7]  = (Convert.ToDouble(W11.GetDouble()) - Convert.ToDouble(I11.GetDouble())) * PriceArray[7, 1] / 100;
            rowinfo[8]  = (Convert.ToDouble(W12.GetDouble()) - Convert.ToDouble(I12.GetDouble())) * PriceArray[8, 1] / 100;
            rowinfo[9]  = (Convert.ToDouble(W1.GetDouble()) - Convert.ToDouble(I1.GetDouble())) * PriceArray[9, 1] / 100;
            rowinfo[10] = (Convert.ToDouble(W2.GetDouble()) - Convert.ToDouble(I2.GetDouble())) * PriceArray[10, 1] / 100;
            rowinfo[11] = (Convert.ToDouble(W3.GetDouble()) - Convert.ToDouble(I3.GetDouble())) * PriceArray[11, 1] / 100;
            rowinfo[12] = ((double)rowinfo[0] + (double)rowinfo[1] + (double)rowinfo[2] + (double)rowinfo[3] + (double)rowinfo[4] + (double)rowinfo[5] + (double)rowinfo[6] + (double)rowinfo[7] + (double)rowinfo[8] + (double)rowinfo[9] + (double)rowinfo[10] + (double)rowinfo[11]);
            SimulationResults.Tables[4].Rows.Add(rowinfo);

            rowinfo     = ForCurvetable.NewRow();
            rowinfo[0]  = PriceArray[0, 1];
            rowinfo[1]  = PriceArray[1, 1];
            rowinfo[2]  = PriceArray[2, 1];
            rowinfo[3]  = PriceArray[3, 1];
            rowinfo[4]  = PriceArray[4, 1];
            rowinfo[5]  = PriceArray[5, 1];
            rowinfo[6]  = PriceArray[6, 1];
            rowinfo[7]  = PriceArray[7, 1];
            rowinfo[8]  = PriceArray[8, 1];
            rowinfo[9]  = PriceArray[9, 1];
            rowinfo[10] = PriceArray[10, 1];
            rowinfo[11] = PriceArray[11, 1];
            SimulationResults.Tables[0].Rows.Add(rowinfo);

            rowinfo     = Positiontable.NewRow();
            rowinfo[0]  = (Convert.ToDouble(I4.GetDouble()) - Convert.ToDouble(W4.GetDouble()));
            rowinfo[1]  = (double)rowinfo[0] + (Convert.ToDouble(I5.GetDouble()) - Convert.ToDouble(W5.GetDouble()));
            rowinfo[2]  = (double)rowinfo[1] + (Convert.ToDouble(I6.GetDouble()) - Convert.ToDouble(W6.GetDouble()));
            rowinfo[3]  = (double)rowinfo[2] + (Convert.ToDouble(I7.GetDouble()) - Convert.ToDouble(W7.GetDouble()));
            rowinfo[4]  = (double)rowinfo[3] + (Convert.ToDouble(I8.GetDouble()) - Convert.ToDouble(W8.GetDouble()));
            rowinfo[5]  = (double)rowinfo[4] + (Convert.ToDouble(I9.GetDouble()) - Convert.ToDouble(W9.GetDouble()));
            rowinfo[6]  = (double)rowinfo[5] + (Convert.ToDouble(I10.GetDouble()) - Convert.ToDouble(W10.GetDouble()));
            rowinfo[7]  = (double)rowinfo[6] + (Convert.ToDouble(I11.GetDouble()) - Convert.ToDouble(W11.GetDouble()));
            rowinfo[8]  = (double)rowinfo[7] + (Convert.ToDouble(I12.GetDouble()) - Convert.ToDouble(W12.GetDouble()));
            rowinfo[9]  = (double)rowinfo[8] + (Convert.ToDouble(I1.GetDouble()) - Convert.ToDouble(W1.GetDouble()));
            rowinfo[10] = (double)rowinfo[9] + (Convert.ToDouble(I2.GetDouble()) - Convert.ToDouble(W2.GetDouble()));
            rowinfo[11] = (double)rowinfo[10] + (Convert.ToDouble(I3.GetDouble()) - Convert.ToDouble(W3.GetDouble()));
            SimulationResults.Tables[3].Rows.Add(rowinfo);
            #endregion

            //  System.Diagnostics.Process.GetCurrentProcess().Kill();
            context.ClearModel();
        }
示例#13
0
        private void button1_Click(object sender, EventArgs e)
        {
            Stenka_Model sm = new Stenka_Model();

            if (Tb_l_ogn.Text == "" || Tb_l_1.Text == "" || Tb_l_2.Text == "" || Tb_C1.Text == "" || Tb_C2.Text == "" || Tb_x0.Text == "" || Tb_trab.Text == "" || Tb_tokr.Text == "" || Tb_tnp.Text == "" || Tb_anar.Text == "" || Tb_arab.Text == "")
            {
                MessageBox.Show("Не все поля заполнены!", "Ошибка");
                return;
            }
            else
            {
                tp_Graph.Parent = Tab_cont;
                chart1.Series[0].Points.Clear();
                chart1.Series[1].Points.Clear();
                sm.l_ogn = Double.Parse(Tb_l_ogn.Text);
                sm.l_1   = Double.Parse(Tb_l_1.Text);
                sm.l_2   = Double.Parse(Tb_l_2.Text);
                sm.c1    = Double.Parse(Tb_C1.Text);
                sm.c2    = Double.Parse(Tb_C2.Text);
                sm.x0    = Double.Parse(Tb_x0.Text);
                sm.t_rab = Double.Parse(Tb_trab.Text);
                sm.t_okr = Double.Parse(Tb_tokr.Text);
                sm.t_np  = Double.Parse(Tb_tnp.Text);
                sm.a_nar = Double.Parse(Tb_anar.Text);
                sm.a_rab = Double.Parse(Tb_arab.Text);
                List <SolverRow> solverList = new List <SolverRow>();
                solverList.Add(new SolverRow {
                    xId = 1, Koef_C = sm.c1
                });
                solverList.Add(new SolverRow {
                    xId = 2, Koef_C = sm.c2
                });
                SolverContext cntxt = SolverContext.GetContext();
                cntxt.ClearModel();
                Model model = cntxt.CreateModel();
                Set   users = new Set(Domain.Any, "users");

                //Parameter l_ogn = new Parameter(Domain.Real, "l_ogn", users);
                //l_ogn.SetBinding ()
                Parameter Koef_C = new Parameter(Domain.Real, "Koef_C", users);
                Koef_C.SetBinding(solverList, "Koef_C", "xId");
                model.AddParameter(Koef_C);

                Decision choose = new Decision(Domain.RealNonnegative, "choose", users);
                model.AddDecisions(choose);
                model.AddGoal("goal", GoalKind.Minimize, Model.Sum(Model.ForEach(users, xId => choose[xId] * Koef_C[xId])));

                //model.AddConstraint("c_choose", Model.ForEach(users, xId => (min[xId] <= choose[xId] <= max[xId])));

                model.AddConstraint("X1", choose[1] >= 0);
                model.AddConstraint("X2", choose[2] >= 0);
                model.AddConstraint("X_Sum", Model.Sum(Model.ForEach(users, xId => choose[xId])) <= sm.x0 / 1000d);
                model.AddConstraint("Temp", sm.t_okr <= sm.t_np);
                model.AddConstraint("K_sigma", (sm.t_np - (sm.t_okr + ((sm.t_rab - sm.t_okr) / (((1 / sm.a_rab) + (choose[1] / sm.l_1) + (choose[2] / sm.l_2) + (1 / sm.a_nar)) * sm.a_nar)))) >= 0);

                try
                {
                    Solution solution = cntxt.Solve();
                    Report   report   = solution.GetReport();

                    //String reportStr = "";

                    //for (int i = 0; i < solverList.Count; i++)
                    //{
                    //    reportStr += "Значение X" + (i + 1).ToString() + ": " + choose.GetDouble(solverList[i].xId) + "\n";
                    //}
                    // reportStr += "\n" + report.ToString();

                    //MessageBox.Show(reportStr);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("При решении задачи оптимизации возникла исключительная ситуация.");
                }
                double x1 = Math.Round(choose.GetDouble(solverList[0].xId), 3);
                double x2 = Math.Round(choose.GetDouble(solverList[1].xId), 3);
                this.chart1.Series[0].Points.AddXY("Толщина стенок, м", x1);
                this.chart1.Series[1].Points.AddXY("Толщина стенок, м", x2);
                if (dataGridView1.RowCount == 1)
                {
                    dataGridView1[0, 0].Value = x1;
                    dataGridView1[1, 0].Value = x2;
                }
                else
                {
                    dataGridView1.Rows.Add(x1, x2);
                }
            }
        }
示例#14
0
        private void RunSimplex(double avgViews, double totalStorage, IOrderedEnumerable<KeyValuePair<Event, double>> events)
        {
            var memcacheCost = .25;
            var memcacheCoefficient = .3;
            var discCost = .025;
            var discCoefficient = .1;
            var tapeCost = 0;
            var tapeCoefficient = 0;
            var memcacheValueCoefficient = .3;
            var discValueCoefficient = .1;
            var tapeValueCoefficient = 0;

            SolverContext context = SolverContext.GetContext();
            Model model = context.CreateModel();

            Decision memcache = new Decision(Domain.RealNonnegative, "gigs_memcache_storage");
            Decision disc = new Decision(Domain.RealNonnegative, "gigs_disc_storage");
            Decision tape = new Decision(Domain.RealNonnegative, "gigs_tape_storage");
            model.AddDecisions(memcache, disc, tape);

            model.AddConstraint("cost", (memcacheCost + memcacheCoefficient * avgViews) * memcache + (discCost + discCoefficient * avgViews) * disc + (tapeCost + tapeCoefficient * avgViews) * tape <= 16106127360);
            model.AddConstraint("total_storage", totalStorage == memcache + disc + tape);

            model.AddGoal("value", GoalKind.Maximize, memcacheValueCoefficient * memcache + discValueCoefficient * disc + tapeValueCoefficient * tape);

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

            Log.InfoFormat("Results of linear optimization: Memcache={0} Disk={1} Tape={2}", memcache.GetDouble(), disc.GetDouble(), tape.GetDouble());
            AllocateMedia(events, memcache.GetDouble(), disc.GetDouble(), tape.GetDouble());
        }
示例#15
0
        public ZeroCouponCurve BuildZeroCouponCurve()
        {
            // 1. Linear interpolate the market curve
            double gap = 1.0 / (double)CouponFrequency;
            SortedDictionary <double, double> interpMarketCurve = new SortedDictionary <double, double>();
            List <double> xs = marketCurve.Keys.ToList <double>();
            List <double> ys = marketCurve.Values.ToList <double>();

            for (double x = gap; x <= 50; x += gap)
            {
                interpMarketCurve.Add(x, LinearInterpolation(x, xs, ys));
            }

            // 2. Find discount factors with a bootstrap method
            List <double> discountFactors = new List <double>();
            List <double> couponRates     = interpMarketCurve.Values.Select(x => x / (double)CouponFrequency).ToList <double>();

            for (int i = 0; i < interpMarketCurve.Count; i++)
            {
                double couponRate           = couponRates[i];
                Func <Decision, Term> error = (df) =>
                {
                    Term guess = 0;
                    for (int j = 0; j < i; j++)
                    {
                        guess += couponRate * discountFactors[j];
                    }
                    guess += (1 + couponRate) * df;

                    return((guess - 1) * (guess - 1));
                };

                // Create solver context and model
                SolverContext context = SolverContext.GetContext();
                Model         model   = context.CreateModel();

                Decision df = new Decision(Domain.RealNonnegative, "df");
                df.SetInitialValue(1.0);
                model.AddDecision(df);

                model.AddGoal("error", GoalKind.Minimize, error(df));

                Solution solution = context.Solve();

                discountFactors.Add(df.GetDouble());
                context.ClearModel();
            }

            // 3. Calculate a zero coupon curve by discount factors
            SortedDictionary <double, double> zeroCouponCurve = new SortedDictionary <double, double>();
            List <double> yearFracs = interpMarketCurve.Keys.ToList <double>();

            for (int i = 0; i < interpMarketCurve.Count; i++)
            {
                double discountFactor = discountFactors[i];
                double yearFrac       = yearFracs[i];
                double zeroCouponRate = Math.Pow(1 / discountFactor, 1 / yearFrac) - 1;
                zeroCouponCurve.Add(yearFrac, zeroCouponRate);
            }

            return(new ZeroCouponCurve(zeroCouponCurve));
        }
示例#16
0
        private void btnCalc_Click(object sender, EventArgs e)
        {
            if (
                #region ---
                (txtRashod_PG_base_DP_1.Text == "") ||
                (txtRashod_PG_base_DPmin_1.Text == "") ||
                (txtRashod_PG_base_DPmax_1.Text == "") ||
                (txtRashod_K_base_DP_1.Text == "") ||
                (txtRashod_K_base_DP_E_1.Text == "") ||
                (txtProizvoditelnost_chug_base_DP_1.Text == "") ||
                (txtTeor_t_base_DP_1.Text == "") ||
                (txtTeor_t_base_DPmin_1.Text == "") ||
                (txtTeor_t_base_DPmax_1.Text == "") ||
                (txtProiz_chug_iz_PG_DP_1.Text == "") ||
                (txtProiz_chug_uvel_K_DP_1.Text == "") ||
                (txtIz_t_uvel_pg_DP_1.Text == "") ||

                (txtRashod_PG_base_DP_2.Text == "") ||
                (txtRashod_PG_base_DPmin_2.Text == "") ||
                (txtRashod_PG_base_DPmax_2.Text == "") ||
                (txtRashod_K_base_DP_2.Text == "") ||
                (txtRashod_K_base_DP_E_2.Text == "") ||
                (txtProizvoditelnost_chug_base_DP_2.Text == "") ||
                (txtTeor_t_base_DP_2.Text == "") ||
                (txtTeor_t_base_DPmin_2.Text == "") ||
                (txtTeor_t_base_DPmax_2.Text == "") ||
                (txtProiz_chug_iz_PG_DP_2.Text == "") ||
                (txtProiz_chug_uvel_K_DP_2.Text == "") ||
                (txtIz_t_uvel_pg_DP_2.Text == "") ||

                (txtRashod_PG_base_DP_3.Text == "") ||
                (txtRashod_PG_base_DPmin_3.Text == "") ||
                (txtRashod_PG_base_DPmax_3.Text == "") ||
                (txtRashod_K_base_DP_3.Text == "") ||
                (txtRashod_K_base_DP_E_3.Text == "") ||
                (txtProizvoditelnost_chug_base_DP_3.Text == "") ||
                (txtTeor_t_base_DP_3.Text == "") ||
                (txtTeor_t_base_DPmin_3.Text == "") ||
                (txtTeor_t_base_DPmax_3.Text == "") ||
                (txtProiz_chug_iz_PG_DP_3.Text == "") ||
                (txtProiz_chug_uvel_K_DP_3.Text == "") ||
                (txtIz_t_uvel_pg_DP_3.Text == "") ||

                (txtRashod_PG_base_DP_4.Text == "") ||
                (txtRashod_PG_base_DPmin_4.Text == "") ||
                (txtRashod_PG_base_DPmax_4.Text == "") ||
                (txtRashod_K_base_DP_4.Text == "") ||
                (txtRashod_K_base_DP_E_4.Text == "") ||
                (txtProizvoditelnost_chug_base_DP_4.Text == "") ||
                (txtTeor_t_base_DP_4.Text == "") ||
                (txtTeor_t_base_DPmin_4.Text == "") ||
                (txtTeor_t_base_DPmax_4.Text == "") ||
                (txtProiz_chug_iz_PG_DP_4.Text == "") ||
                (txtProiz_chug_uvel_K_DP_4.Text == "") ||
                (txtIz_t_uvel_pg_DP_4.Text == "") ||

                (txtRashod_PG_base_DP_5.Text == "") ||
                (txtRashod_PG_base_DPmin_5.Text == "") ||
                (txtRashod_PG_base_DPmax_5.Text == "") ||
                (txtRashod_K_base_DP_5.Text == "") ||
                (txtRashod_K_base_DP_E_5.Text == "") ||
                (txtProizvoditelnost_chug_base_DP_5.Text == "") ||
                (txtTeor_t_base_DP_5.Text == "") ||
                (txtTeor_t_base_DPmin_5.Text == "") ||
                (txtTeor_t_base_DPmax_5.Text == "") ||
                (txtProiz_chug_iz_PG_DP_5.Text == "") ||
                (txtProiz_chug_uvel_K_DP_5.Text == "") ||
                (txtIz_t_uvel_pg_DP_5.Text == "") ||

                (txtRashod_PG_base_DP_6.Text == "") ||
                (txtRashod_PG_base_DPmin_6.Text == "") ||
                (txtRashod_PG_base_DPmax_6.Text == "") ||
                (txtRashod_K_base_DP_6.Text == "") ||
                (txtRashod_K_base_DP_E_6.Text == "") ||
                (txtProizvoditelnost_chug_base_DP_6.Text == "") ||
                (txtTeor_t_base_DP_6.Text == "") ||
                (txtTeor_t_base_DPmin_6.Text == "") ||
                (txtTeor_t_base_DPmax_6.Text == "") ||
                (txtProiz_chug_iz_PG_DP_6.Text == "") ||
                (txtProiz_chug_uvel_K_DP_6.Text == "") ||
                (txtIz_t_uvel_pg_DP_6.Text == "") ||

                (txtRashod_PG_base_DP_7.Text == "") ||
                (txtRashod_PG_base_DPmin_7.Text == "") ||
                (txtRashod_PG_base_DPmax_7.Text == "") ||
                (txtRashod_K_base_DP_7.Text == "") ||
                (txtRashod_K_base_DP_E_7.Text == "") ||
                (txtProizvoditelnost_chug_base_DP_7.Text == "") ||
                (txtTeor_t_base_DP_7.Text == "") ||
                (txtTeor_t_base_DPmin_7.Text == "") ||
                (txtTeor_t_base_DPmax_7.Text == "") ||
                (txtProiz_chug_iz_PG_DP_7.Text == "") ||
                (txtProiz_chug_uvel_K_DP_7.Text == "") ||
                (txtIz_t_uvel_pg_DP_7.Text == "") ||

                (txtRashod_PG_base_DP_8.Text == "") ||
                (txtRashod_PG_base_DPmin_8.Text == "") ||
                (txtRashod_PG_base_DPmax_8.Text == "") ||
                (txtRashod_K_base_DP_8.Text == "") ||
                (txtRashod_K_base_DP_E_8.Text == "") ||
                (txtProizvoditelnost_chug_base_DP_8.Text == "") ||
                (txtTeor_t_base_DP_8.Text == "") ||
                (txtTeor_t_base_DPmin_8.Text == "") ||
                (txtTeor_t_base_DPmax_8.Text == "") ||
                (txtProiz_chug_iz_PG_DP_8.Text == "") ||
                (txtProiz_chug_uvel_K_DP_8.Text == "") ||
                (txtIz_t_uvel_pg_DP_8.Text == ""))
            #endregion ---
            {
                tPGraph.Parent = null;
                MessageBox.Show("Не все поля заполнены!", "Ошибка");
                return;
            }
            //очищаем
            chart1.Series[0].Points.Clear();
            chart1.Series[1].Points.Clear();

            //при нажатии на кнопку, открывается вкладка
            tPGraph.Parent = tab;

            List <ModelDP> modelDPs = new List <ModelDP>();
            #region -- Загрузка первоначальных значений

            // ДП-1
            _mdp.Rashod_PG_base_DP_1    = Double.Parse(txtRashod_PG_base_DP_1.Text);
            _mdp.Rashod_PG_base_DPmin_1 = Double.Parse(txtRashod_PG_base_DPmin_1.Text);
            _mdp.Rashod_PG_base_DPmax_1 = Double.Parse(txtRashod_PG_base_DPmax_1.Text);
            _mdp.Rashod_K_base_DP_1     = Double.Parse(txtRashod_K_base_DP_1.Text);


            _mdp.Rashod_K_base_DP_E_1            = Double.Parse(txtRashod_K_base_DP_E_1.Text);
            _mdp.Proizvoditelnost_chug_base_DP_1 = Double.Parse(txtProizvoditelnost_chug_base_DP_1.Text);
            _mdp.Teor_t_base_DP_1    = Double.Parse(txtTeor_t_base_DP_1.Text);
            _mdp.Teor_t_base_DPmin_1 = Double.Parse(txtTeor_t_base_DPmin_1.Text);
            _mdp.Teor_t_base_DPmax_1 = Double.Parse(txtTeor_t_base_DPmax_1.Text);

            _mdp.Proiz_chug_iz_PG_DP_1  = Double.Parse(txtProiz_chug_iz_PG_DP_1.Text);
            _mdp.Proiz_chug_uvel_K_DP_1 = Double.Parse(txtProiz_chug_uvel_K_DP_1.Text);
            _mdp.Iz_t_uvel_pg_DP_1      = Double.Parse(txtIz_t_uvel_pg_DP_1.Text);



            // ДП-2
            _mdp.Rashod_PG_base_DP_2    = Double.Parse(txtRashod_PG_base_DP_2.Text);
            _mdp.Rashod_PG_base_DPmin_2 = Double.Parse(txtRashod_PG_base_DPmin_2.Text);
            _mdp.Rashod_PG_base_DPmax_2 = Double.Parse(txtRashod_PG_base_DPmax_2.Text);
            _mdp.Rashod_K_base_DP_2     = Double.Parse(txtRashod_K_base_DP_2.Text);


            _mdp.Rashod_K_base_DP_E_2            = Double.Parse(txtRashod_K_base_DP_E_2.Text);
            _mdp.Proizvoditelnost_chug_base_DP_2 = Double.Parse(txtProizvoditelnost_chug_base_DP_2.Text);
            _mdp.Teor_t_base_DP_2    = Double.Parse(txtTeor_t_base_DP_2.Text);
            _mdp.Teor_t_base_DPmin_2 = Double.Parse(txtTeor_t_base_DPmin_2.Text);
            _mdp.Teor_t_base_DPmax_2 = Double.Parse(txtTeor_t_base_DPmax_2.Text);

            _mdp.Proiz_chug_iz_PG_DP_2  = Double.Parse(txtProiz_chug_iz_PG_DP_2.Text);
            _mdp.Proiz_chug_uvel_K_DP_2 = Double.Parse(txtProiz_chug_uvel_K_DP_2.Text);
            _mdp.Iz_t_uvel_pg_DP_2      = Double.Parse(txtIz_t_uvel_pg_DP_2.Text);


            // ДП-3
            _mdp.Rashod_PG_base_DP_3    = Double.Parse(txtRashod_PG_base_DP_3.Text);
            _mdp.Rashod_PG_base_DPmin_3 = Double.Parse(txtRashod_PG_base_DPmin_3.Text);
            _mdp.Rashod_PG_base_DPmax_3 = Double.Parse(txtRashod_PG_base_DPmax_3.Text);
            _mdp.Rashod_K_base_DP_3     = Double.Parse(txtRashod_K_base_DP_3.Text);


            _mdp.Rashod_K_base_DP_E_3            = Double.Parse(txtRashod_K_base_DP_E_3.Text);
            _mdp.Proizvoditelnost_chug_base_DP_3 = Double.Parse(txtProizvoditelnost_chug_base_DP_3.Text);
            _mdp.Teor_t_base_DP_3    = Double.Parse(txtTeor_t_base_DP_3.Text);
            _mdp.Teor_t_base_DPmin_3 = Double.Parse(txtTeor_t_base_DPmin_3.Text);
            _mdp.Teor_t_base_DPmax_3 = Double.Parse(txtTeor_t_base_DPmax_3.Text);

            _mdp.Proiz_chug_iz_PG_DP_3  = Double.Parse(txtProiz_chug_iz_PG_DP_3.Text);
            _mdp.Proiz_chug_uvel_K_DP_3 = Double.Parse(txtProiz_chug_uvel_K_DP_3.Text);
            _mdp.Iz_t_uvel_pg_DP_3      = Double.Parse(txtIz_t_uvel_pg_DP_3.Text);

            // ДП-4
            _mdp.Rashod_PG_base_DP_4    = Double.Parse(txtRashod_PG_base_DP_4.Text);
            _mdp.Rashod_PG_base_DPmin_4 = Double.Parse(txtRashod_PG_base_DPmin_4.Text);
            _mdp.Rashod_PG_base_DPmax_4 = Double.Parse(txtRashod_PG_base_DPmax_4.Text);
            _mdp.Rashod_K_base_DP_4     = Double.Parse(txtRashod_K_base_DP_4.Text);


            _mdp.Rashod_K_base_DP_E_4            = Double.Parse(txtRashod_K_base_DP_E_4.Text);
            _mdp.Proizvoditelnost_chug_base_DP_4 = Double.Parse(txtProizvoditelnost_chug_base_DP_4.Text);
            _mdp.Teor_t_base_DP_4    = Double.Parse(txtTeor_t_base_DP_4.Text);
            _mdp.Teor_t_base_DPmin_4 = Double.Parse(txtTeor_t_base_DPmin_4.Text);
            _mdp.Teor_t_base_DPmax_4 = Double.Parse(txtTeor_t_base_DPmax_4.Text);

            _mdp.Proiz_chug_iz_PG_DP_4  = Double.Parse(txtProiz_chug_iz_PG_DP_4.Text);
            _mdp.Proiz_chug_uvel_K_DP_4 = Double.Parse(txtProiz_chug_uvel_K_DP_4.Text);
            _mdp.Iz_t_uvel_pg_DP_4      = Double.Parse(txtIz_t_uvel_pg_DP_4.Text);

            // ДП-5
            _mdp.Rashod_PG_base_DP_5    = Double.Parse(txtRashod_PG_base_DP_5.Text);
            _mdp.Rashod_PG_base_DPmin_5 = Double.Parse(txtRashod_PG_base_DPmin_5.Text);
            _mdp.Rashod_PG_base_DPmax_5 = Double.Parse(txtRashod_PG_base_DPmax_5.Text);
            _mdp.Rashod_K_base_DP_5     = Double.Parse(txtRashod_K_base_DP_5.Text);


            _mdp.Rashod_K_base_DP_E_5            = Double.Parse(txtRashod_K_base_DP_E_5.Text);
            _mdp.Proizvoditelnost_chug_base_DP_5 = Double.Parse(txtProizvoditelnost_chug_base_DP_5.Text);
            _mdp.Teor_t_base_DP_5    = Double.Parse(txtTeor_t_base_DP_5.Text);
            _mdp.Teor_t_base_DPmin_5 = Double.Parse(txtTeor_t_base_DPmin_5.Text);
            _mdp.Teor_t_base_DPmax_5 = Double.Parse(txtTeor_t_base_DPmax_5.Text);

            _mdp.Proiz_chug_iz_PG_DP_5  = Double.Parse(txtProiz_chug_iz_PG_DP_5.Text);
            _mdp.Proiz_chug_uvel_K_DP_5 = Double.Parse(txtProiz_chug_uvel_K_DP_5.Text);
            _mdp.Iz_t_uvel_pg_DP_5      = Double.Parse(txtIz_t_uvel_pg_DP_5.Text);
            // ДП-6
            _mdp.Rashod_PG_base_DP_6    = Double.Parse(txtRashod_PG_base_DP_6.Text);
            _mdp.Rashod_PG_base_DPmin_6 = Double.Parse(txtRashod_PG_base_DPmin_6.Text);
            _mdp.Rashod_PG_base_DPmax_6 = Double.Parse(txtRashod_PG_base_DPmax_6.Text);
            _mdp.Rashod_K_base_DP_6     = Double.Parse(txtRashod_K_base_DP_6.Text);


            _mdp.Rashod_K_base_DP_E_6            = Double.Parse(txtRashod_K_base_DP_E_6.Text);
            _mdp.Proizvoditelnost_chug_base_DP_6 = Double.Parse(txtProizvoditelnost_chug_base_DP_6.Text);
            _mdp.Teor_t_base_DP_6    = Double.Parse(txtTeor_t_base_DP_6.Text);
            _mdp.Teor_t_base_DPmin_6 = Double.Parse(txtTeor_t_base_DPmin_6.Text);
            _mdp.Teor_t_base_DPmax_6 = Double.Parse(txtTeor_t_base_DPmax_6.Text);

            _mdp.Proiz_chug_iz_PG_DP_6  = Double.Parse(txtProiz_chug_iz_PG_DP_6.Text);
            _mdp.Proiz_chug_uvel_K_DP_6 = Double.Parse(txtProiz_chug_uvel_K_DP_6.Text);
            _mdp.Iz_t_uvel_pg_DP_6      = Double.Parse(txtIz_t_uvel_pg_DP_6.Text);
            // ДП-7
            _mdp.Rashod_PG_base_DP_7    = Double.Parse(txtRashod_PG_base_DP_7.Text);
            _mdp.Rashod_PG_base_DPmin_7 = Double.Parse(txtRashod_PG_base_DPmin_7.Text);
            _mdp.Rashod_PG_base_DPmax_7 = Double.Parse(txtRashod_PG_base_DPmax_7.Text);
            _mdp.Rashod_K_base_DP_7     = Double.Parse(txtRashod_K_base_DP_7.Text);


            _mdp.Rashod_K_base_DP_E_7            = Double.Parse(txtRashod_K_base_DP_E_7.Text);
            _mdp.Proizvoditelnost_chug_base_DP_7 = Double.Parse(txtProizvoditelnost_chug_base_DP_7.Text);
            _mdp.Teor_t_base_DP_7    = Double.Parse(txtTeor_t_base_DP_7.Text);
            _mdp.Teor_t_base_DPmin_7 = Double.Parse(txtTeor_t_base_DPmin_7.Text);
            _mdp.Teor_t_base_DPmax_7 = Double.Parse(txtTeor_t_base_DPmax_7.Text);

            _mdp.Proiz_chug_iz_PG_DP_7  = Double.Parse(txtProiz_chug_iz_PG_DP_7.Text);
            _mdp.Proiz_chug_uvel_K_DP_7 = Double.Parse(txtProiz_chug_uvel_K_DP_7.Text);
            _mdp.Iz_t_uvel_pg_DP_7      = Double.Parse(txtIz_t_uvel_pg_DP_7.Text);
            // ДП-8
            _mdp.Rashod_PG_base_DP_8    = Double.Parse(txtRashod_PG_base_DP_8.Text);
            _mdp.Rashod_PG_base_DPmin_8 = Double.Parse(txtRashod_PG_base_DPmin_8.Text);
            _mdp.Rashod_PG_base_DPmax_8 = Double.Parse(txtRashod_PG_base_DPmax_8.Text);
            _mdp.Rashod_K_base_DP_8     = Double.Parse(txtRashod_K_base_DP_8.Text);


            _mdp.Rashod_K_base_DP_E_8            = Double.Parse(txtRashod_K_base_DP_E_8.Text);
            _mdp.Proizvoditelnost_chug_base_DP_8 = Double.Parse(txtProizvoditelnost_chug_base_DP_8.Text);
            _mdp.Teor_t_base_DP_8    = Double.Parse(txtTeor_t_base_DP_8.Text);
            _mdp.Teor_t_base_DPmin_8 = Double.Parse(txtTeor_t_base_DPmin_8.Text);
            _mdp.Teor_t_base_DPmax_8 = Double.Parse(txtTeor_t_base_DPmax_8.Text);

            _mdp.Proiz_chug_iz_PG_DP_8  = Double.Parse(txtProiz_chug_iz_PG_DP_8.Text);
            _mdp.Proiz_chug_uvel_K_DP_8 = Double.Parse(txtProiz_chug_uvel_K_DP_8.Text);
            _mdp.Iz_t_uvel_pg_DP_8      = Double.Parse(txtIz_t_uvel_pg_DP_8.Text);

            //параметры
            _mdp.Stoimoct_k       = Double.Parse(txtStoimoct_k.Text);
            _mdp.Stoimoct_pg      = Double.Parse(txtStoimoct_pg.Text);
            _mdp.Rezerf_rashod_pg = Double.Parse(txtRezerf_rashod_pg.Text);
            _mdp.Zapas_k          = Double.Parse(txtZapas_k.Text);
            _mdp.Treb_proiz_chug  = Double.Parse(txtTreb_proiz_chug.Text);

            #endregion -- Загрузка первоначальных значений
            #region ---
            //Для ДП1
            modelDPs.Add(new ModelDP {
                xId                  = 1,
                Koef                 = (_mdp.Rashod_K_base_DP_E_1 * _mdp.Stoimoct_k - _mdp.Stoimoct_pg),
                KoefNerav1           = (-0.001 * _mdp.Rashod_K_base_DP_E_1),
                KoefNerav11          = (_mdp.Rashod_K_base_DP_1 + 0.001 * _mdp.Rashod_PG_base_DP_1 * _mdp.Rashod_K_base_DP_E_1),
                KoefNerav2           = _mdp.Proiz_chug_iz_PG_DP_1 - _mdp.Rashod_K_base_DP_E_1 * _mdp.Proiz_chug_uvel_K_DP_1,
                KoefNerav22          = -_mdp.Rashod_PG_base_DP_1 * _mdp.Proiz_chug_iz_PG_DP_1 + _mdp.Rashod_K_base_DP_E_1 * _mdp.Rashod_PG_base_DP_1 * _mdp.Proiz_chug_uvel_K_DP_1 + _mdp.Proizvoditelnost_chug_base_DP_1,
                KoefNerav3           = (_mdp.Teor_t_base_DPmin_1 + _mdp.Rashod_PG_base_DP_1 * _mdp.Iz_t_uvel_pg_DP_1 - _mdp.Teor_t_base_DP_1) / _mdp.Iz_t_uvel_pg_DP_1,
                KoefNerav33          = (_mdp.Teor_t_base_DPmax_1 + _mdp.Rashod_PG_base_DP_1 * _mdp.Iz_t_uvel_pg_DP_1 - _mdp.Teor_t_base_DP_1) / _mdp.Iz_t_uvel_pg_DP_1,
                Rashod_PG_base_DPmin = _mdp.Rashod_PG_base_DPmin_1,
                Rashod_PG_base_DPmax = _mdp.Rashod_PG_base_DPmax_1,
            });
            //Для ДП2
            modelDPs.Add(new ModelDP
            {
                xId                  = 2,
                Koef                 = (_mdp.Rashod_K_base_DP_E_2 * _mdp.Stoimoct_k - _mdp.Stoimoct_pg),
                KoefNerav1           = (-0.001 * _mdp.Rashod_K_base_DP_E_2),
                KoefNerav11          = (_mdp.Rashod_K_base_DP_2 + 0.001 * _mdp.Rashod_PG_base_DP_2 * _mdp.Rashod_K_base_DP_E_2),
                KoefNerav2           = _mdp.Proiz_chug_iz_PG_DP_2 - _mdp.Rashod_K_base_DP_E_2 * _mdp.Proiz_chug_uvel_K_DP_2,
                KoefNerav22          = -_mdp.Rashod_PG_base_DP_2 * _mdp.Proiz_chug_iz_PG_DP_2 + _mdp.Rashod_K_base_DP_E_2 * _mdp.Rashod_PG_base_DP_2 * _mdp.Proiz_chug_uvel_K_DP_2 + _mdp.Proizvoditelnost_chug_base_DP_2,
                KoefNerav3           = (_mdp.Teor_t_base_DPmin_2 + _mdp.Rashod_PG_base_DP_2 * _mdp.Iz_t_uvel_pg_DP_2 - _mdp.Teor_t_base_DP_2) / _mdp.Iz_t_uvel_pg_DP_2,
                KoefNerav33          = (_mdp.Teor_t_base_DPmax_2 + _mdp.Rashod_PG_base_DP_2 * _mdp.Iz_t_uvel_pg_DP_2 - _mdp.Teor_t_base_DP_2) / _mdp.Iz_t_uvel_pg_DP_2,
                Rashod_PG_base_DPmin = _mdp.Rashod_PG_base_DPmin_2,
                Rashod_PG_base_DPmax = _mdp.Rashod_PG_base_DPmax_2
            });

            //Для ДП3
            modelDPs.Add(new ModelDP
            {
                xId                  = 3,
                Koef                 = (_mdp.Rashod_K_base_DP_E_3 * _mdp.Stoimoct_k - _mdp.Stoimoct_pg),
                KoefNerav1           = (-0.001 * _mdp.Rashod_K_base_DP_E_3),
                KoefNerav11          = (_mdp.Rashod_K_base_DP_3 + 0.001 * _mdp.Rashod_PG_base_DP_3 * _mdp.Rashod_K_base_DP_E_3),
                KoefNerav2           = _mdp.Proiz_chug_iz_PG_DP_3 - _mdp.Rashod_K_base_DP_E_3 * _mdp.Proiz_chug_uvel_K_DP_3,
                KoefNerav22          = -_mdp.Rashod_PG_base_DP_3 * _mdp.Proiz_chug_iz_PG_DP_3 + _mdp.Rashod_K_base_DP_E_3 * _mdp.Rashod_PG_base_DP_3 * _mdp.Proiz_chug_uvel_K_DP_3 + _mdp.Proizvoditelnost_chug_base_DP_3,
                KoefNerav3           = (_mdp.Teor_t_base_DPmin_3 + _mdp.Rashod_PG_base_DP_3 * _mdp.Iz_t_uvel_pg_DP_3 - _mdp.Teor_t_base_DP_3) / _mdp.Iz_t_uvel_pg_DP_3,
                KoefNerav33          = (_mdp.Teor_t_base_DPmax_3 + _mdp.Rashod_PG_base_DP_3 * _mdp.Iz_t_uvel_pg_DP_3 - _mdp.Teor_t_base_DP_3) / _mdp.Iz_t_uvel_pg_DP_3,
                Rashod_PG_base_DPmin = _mdp.Rashod_PG_base_DPmin_3,
                Rashod_PG_base_DPmax = _mdp.Rashod_PG_base_DPmax_3
            });

            //Для ДП4
            modelDPs.Add(new ModelDP
            {
                xId                  = 4,
                Koef                 = (_mdp.Rashod_K_base_DP_E_4 * _mdp.Stoimoct_k - _mdp.Stoimoct_pg),
                KoefNerav1           = (-0.001 * _mdp.Rashod_K_base_DP_E_4),
                KoefNerav11          = (_mdp.Rashod_K_base_DP_4 + 0.001 * _mdp.Rashod_PG_base_DP_4 * _mdp.Rashod_K_base_DP_E_4),
                KoefNerav2           = _mdp.Proiz_chug_iz_PG_DP_4 - _mdp.Rashod_K_base_DP_E_4 * _mdp.Proiz_chug_uvel_K_DP_4,
                KoefNerav22          = -_mdp.Rashod_PG_base_DP_4 * _mdp.Proiz_chug_iz_PG_DP_4 + _mdp.Rashod_K_base_DP_E_4 * _mdp.Rashod_PG_base_DP_4 * _mdp.Proiz_chug_uvel_K_DP_4 + _mdp.Proizvoditelnost_chug_base_DP_4,
                KoefNerav3           = (_mdp.Teor_t_base_DPmin_4 + _mdp.Rashod_PG_base_DP_4 * _mdp.Iz_t_uvel_pg_DP_4 - _mdp.Teor_t_base_DP_4) / _mdp.Iz_t_uvel_pg_DP_4,
                KoefNerav33          = (_mdp.Teor_t_base_DPmax_4 + _mdp.Rashod_PG_base_DP_4 * _mdp.Iz_t_uvel_pg_DP_4 - _mdp.Teor_t_base_DP_4) / _mdp.Iz_t_uvel_pg_DP_4,
                Rashod_PG_base_DPmin = _mdp.Rashod_PG_base_DPmin_4,
                Rashod_PG_base_DPmax = _mdp.Rashod_PG_base_DPmax_4
            });

            //Для ДП5
            modelDPs.Add(new ModelDP
            {
                xId                  = 5,
                Koef                 = (_mdp.Rashod_K_base_DP_E_5 * _mdp.Stoimoct_k - _mdp.Stoimoct_pg),
                KoefNerav1           = (-0.001 * _mdp.Rashod_K_base_DP_E_5),
                KoefNerav11          = (_mdp.Rashod_K_base_DP_5 + 0.001 * _mdp.Rashod_PG_base_DP_5 * _mdp.Rashod_K_base_DP_E_5),
                KoefNerav2           = _mdp.Proiz_chug_iz_PG_DP_5 - _mdp.Rashod_K_base_DP_E_5 * _mdp.Proiz_chug_uvel_K_DP_5,
                KoefNerav22          = -_mdp.Rashod_PG_base_DP_5 * _mdp.Proiz_chug_iz_PG_DP_5 + _mdp.Rashod_K_base_DP_E_5 * _mdp.Rashod_PG_base_DP_5 * _mdp.Proiz_chug_uvel_K_DP_5 + _mdp.Proizvoditelnost_chug_base_DP_5,
                KoefNerav3           = (_mdp.Teor_t_base_DPmin_5 + _mdp.Rashod_PG_base_DP_5 * _mdp.Iz_t_uvel_pg_DP_5 - _mdp.Teor_t_base_DP_5) / _mdp.Iz_t_uvel_pg_DP_5,
                KoefNerav33          = (_mdp.Teor_t_base_DPmax_5 + _mdp.Rashod_PG_base_DP_5 * _mdp.Iz_t_uvel_pg_DP_5 - _mdp.Teor_t_base_DP_5) / _mdp.Iz_t_uvel_pg_DP_5,
                Rashod_PG_base_DPmin = _mdp.Rashod_PG_base_DPmin_5,
                Rashod_PG_base_DPmax = _mdp.Rashod_PG_base_DPmax_5
            });

            //Для ДП6
            modelDPs.Add(new ModelDP
            {
                xId                  = 6,
                Koef                 = (_mdp.Rashod_K_base_DP_E_6 * _mdp.Stoimoct_k - _mdp.Stoimoct_pg),
                KoefNerav1           = (-0.001 * _mdp.Rashod_K_base_DP_E_6),
                KoefNerav11          = (_mdp.Rashod_K_base_DP_6 + 0.001 * _mdp.Rashod_PG_base_DP_6 * _mdp.Rashod_K_base_DP_E_6),
                KoefNerav2           = _mdp.Proiz_chug_iz_PG_DP_6 - _mdp.Rashod_K_base_DP_E_6 * _mdp.Proiz_chug_uvel_K_DP_6,
                KoefNerav22          = -_mdp.Rashod_PG_base_DP_6 * _mdp.Proiz_chug_iz_PG_DP_6 + _mdp.Rashod_K_base_DP_E_6 * _mdp.Rashod_PG_base_DP_6 * _mdp.Proiz_chug_uvel_K_DP_6 + _mdp.Proizvoditelnost_chug_base_DP_6,
                KoefNerav3           = (_mdp.Teor_t_base_DPmin_6 + _mdp.Rashod_PG_base_DP_6 * _mdp.Iz_t_uvel_pg_DP_6 - _mdp.Teor_t_base_DP_6) / _mdp.Iz_t_uvel_pg_DP_6,
                KoefNerav33          = (_mdp.Teor_t_base_DPmax_6 + _mdp.Rashod_PG_base_DP_6 * _mdp.Iz_t_uvel_pg_DP_6 - _mdp.Teor_t_base_DP_6) / _mdp.Iz_t_uvel_pg_DP_6,
                Rashod_PG_base_DPmin = _mdp.Rashod_PG_base_DPmin_6,
                Rashod_PG_base_DPmax = _mdp.Rashod_PG_base_DPmax_6
            });

            //Для ДП7
            modelDPs.Add(new ModelDP
            {
                xId                  = 7,
                Koef                 = (_mdp.Rashod_K_base_DP_E_7 * _mdp.Stoimoct_k - _mdp.Stoimoct_pg),
                KoefNerav1           = (-0.001 * _mdp.Rashod_K_base_DP_E_7),
                KoefNerav11          = (_mdp.Rashod_K_base_DP_7 + 0.001 * _mdp.Rashod_PG_base_DP_7 * _mdp.Rashod_K_base_DP_E_7),
                KoefNerav2           = _mdp.Proiz_chug_iz_PG_DP_7 - _mdp.Rashod_K_base_DP_E_7 * _mdp.Proiz_chug_uvel_K_DP_7,
                KoefNerav22          = -_mdp.Rashod_PG_base_DP_7 * _mdp.Proiz_chug_iz_PG_DP_7 + _mdp.Rashod_K_base_DP_E_7 * _mdp.Rashod_PG_base_DP_7 * _mdp.Proiz_chug_uvel_K_DP_7 + _mdp.Proizvoditelnost_chug_base_DP_7,
                KoefNerav3           = (_mdp.Teor_t_base_DPmin_7 + _mdp.Rashod_PG_base_DP_7 * _mdp.Iz_t_uvel_pg_DP_7 - _mdp.Teor_t_base_DP_7) / _mdp.Iz_t_uvel_pg_DP_7,
                KoefNerav33          = (_mdp.Teor_t_base_DPmax_7 + _mdp.Rashod_PG_base_DP_7 * _mdp.Iz_t_uvel_pg_DP_7 - _mdp.Teor_t_base_DP_7) / _mdp.Iz_t_uvel_pg_DP_7,
                Rashod_PG_base_DPmin = _mdp.Rashod_PG_base_DPmin_7,
                Rashod_PG_base_DPmax = _mdp.Rashod_PG_base_DPmax_7
            });

            //Для ДП8
            modelDPs.Add(new ModelDP
            {
                xId                  = 8,
                Koef                 = (_mdp.Rashod_K_base_DP_E_8 * _mdp.Stoimoct_k - _mdp.Stoimoct_pg),
                KoefNerav1           = (-0.001 * _mdp.Rashod_K_base_DP_E_8),
                KoefNerav11          = (_mdp.Rashod_K_base_DP_8 + 0.001 * _mdp.Rashod_PG_base_DP_8 * _mdp.Rashod_K_base_DP_E_8),
                KoefNerav2           = _mdp.Proiz_chug_iz_PG_DP_8 - _mdp.Rashod_K_base_DP_E_8 * _mdp.Proiz_chug_uvel_K_DP_8,
                KoefNerav22          = -_mdp.Rashod_PG_base_DP_8 * _mdp.Proiz_chug_iz_PG_DP_8 + _mdp.Rashod_K_base_DP_E_8 * _mdp.Rashod_PG_base_DP_8 * _mdp.Proiz_chug_uvel_K_DP_8 + _mdp.Proizvoditelnost_chug_base_DP_8,
                KoefNerav3           = (_mdp.Teor_t_base_DPmin_8 + _mdp.Rashod_PG_base_DP_8 * _mdp.Iz_t_uvel_pg_DP_8 - _mdp.Teor_t_base_DP_8) / _mdp.Iz_t_uvel_pg_DP_8,
                KoefNerav33          = (_mdp.Teor_t_base_DPmax_8 + _mdp.Rashod_PG_base_DP_8 * _mdp.Iz_t_uvel_pg_DP_8 - _mdp.Teor_t_base_DP_8) / _mdp.Iz_t_uvel_pg_DP_8,
                Rashod_PG_base_DPmin = _mdp.Rashod_PG_base_DPmin_8,
                Rashod_PG_base_DPmax = _mdp.Rashod_PG_base_DPmax_8
            });


            #endregion ---

            SolverContext context = SolverContext.GetContext();
            context.ClearModel();
            Model model = context.CreateModel();
            Set   users = new Set(Domain.Any, "users");

            Parameter Koef                 = new Parameter(Domain.Real, "Koef", users);
            Parameter KoefNerav1           = new Parameter(Domain.Real, "KoefNerav1", users);
            Parameter KoefNerav11          = new Parameter(Domain.Real, "KoefNerav11", users);
            Parameter KoefNerav2           = new Parameter(Domain.Real, "KoefNerav2", users);
            Parameter KoefNerav22          = new Parameter(Domain.Real, "KoefNerav22", users);
            Parameter KoefNerav3           = new Parameter(Domain.Real, "KoefNerav3", users);
            Parameter KoefNerav33          = new Parameter(Domain.Real, "KoefNerav33", users);
            Parameter Rashod_PG_base_DPmin = new Parameter(Domain.Real, "Rashod_PG_base_DPmin", users);
            Parameter Rashod_PG_base_DPmax = new Parameter(Domain.Real, "Rashod_PG_base_DPmax", users);

            Koef.SetBinding(modelDPs, "Koef", "xId");
            KoefNerav1.SetBinding(modelDPs, "KoefNerav1", "xId");
            KoefNerav11.SetBinding(modelDPs, "KoefNerav11", "xId");
            KoefNerav2.SetBinding(modelDPs, "KoefNerav2", "xId");
            KoefNerav22.SetBinding(modelDPs, "KoefNerav22", "xId");
            KoefNerav3.SetBinding(modelDPs, "KoefNerav3", "xId");
            KoefNerav33.SetBinding(modelDPs, "KoefNerav33", "xId");
            Rashod_PG_base_DPmin.SetBinding(modelDPs, "Rashod_PG_base_DPmin", "xId");
            Rashod_PG_base_DPmax.SetBinding(modelDPs, "Rashod_PG_base_DPmax", "xId");

            model.AddParameters(
                Koef,
                KoefNerav1,
                KoefNerav11,
                KoefNerav2,
                KoefNerav22,
                KoefNerav3,
                KoefNerav33,
                Rashod_PG_base_DPmin,
                Rashod_PG_base_DPmax
                );

            Decision choose = new Decision(Domain.RealNonnegative, "choose", users);
            model.AddDecision(choose);

            model.AddGoal("Z", GoalKind.Maximize, Model.Sum(Model.ForEach(users, xId => choose[xId] * Koef[xId])));

            model.AddConstraint("Z1", Model.ForEach(users, xId => (Rashod_PG_base_DPmin[xId] <= choose[xId] <= Rashod_PG_base_DPmax[xId])));
            model.AddConstraint("Z2", Model.Sum(Model.ForEach(users, xId => choose[xId])) <= _mdp.Rezerf_rashod_pg);
            model.AddConstraint("Nerav1", Model.Sum(Model.ForEach(users, xId => choose[xId] * KoefNerav1[xId] + KoefNerav11[xId])) <= _mdp.Zapas_k);
            model.AddConstraint("Nerav2", Model.Sum(Model.ForEach(users, xId => choose[xId] * KoefNerav2[xId] + KoefNerav22[xId])) >= _mdp.Treb_proiz_chug);
            model.AddConstraint("Nerav3", Model.ForEach(users, xId => KoefNerav33[xId] <= choose[xId] <= KoefNerav3[xId]));

            try
            {
                Solution solution = context.Solve();
                Report   report   = solution.GetReport();

                String reportStr = "";

                for (int i = 0; i < modelDPs.Count; i++)
                {
                    reportStr += "Значение X" + (i + 1).ToString() + ": " + choose.GetDouble(modelDPs[i].xId) + "\n";
                }
                reportStr += "\n" + report.ToString();

                //MessageBox.Show(reportStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show("При решении задачи оптимизации возникла исключительная ситуация.");
            }

            _mdp.DP1 = Math.Round(choose.GetDouble(modelDPs[0].xId), 2);
            _mdp.DP2 = Math.Round(choose.GetDouble(modelDPs[1].xId), 2);
            _mdp.DP3 = Math.Round(choose.GetDouble(modelDPs[2].xId), 2);
            _mdp.DP4 = Math.Round(choose.GetDouble(modelDPs[3].xId), 2);
            _mdp.DP5 = Math.Round(choose.GetDouble(modelDPs[4].xId), 2);
            _mdp.DP6 = Math.Round(choose.GetDouble(modelDPs[5].xId), 2);
            _mdp.DP7 = Math.Round(choose.GetDouble(modelDPs[6].xId), 2);
            _mdp.DP8 = Math.Round(choose.GetDouble(modelDPs[7].xId), 2);

            txtDP1.Text = _mdp.DP1.ToString();
            txtDP2.Text = _mdp.DP2.ToString();
            txtDP3.Text = _mdp.DP3.ToString();
            txtDP4.Text = _mdp.DP4.ToString();
            txtDP5.Text = _mdp.DP5.ToString();
            txtDP6.Text = _mdp.DP6.ToString();
            txtDP7.Text = _mdp.DP7.ToString();
            txtDP8.Text = _mdp.DP8.ToString();

            double z = 0;
            for (int i = 0; i < 8; i++)
            {
                double a = choose.GetDouble(modelDPs[i].xId);
                double b = modelDPs[i].Koef;
                z += a * b;
            }
            txtZ.Text = z.ToString();

            //график
            this.chart1.Series[0].Points.AddXY("ДП1", txtRashod_PG_base_DP_1.Text);
            this.chart1.Series[0].Points.AddXY("ДП2", txtRashod_PG_base_DP_2.Text);
            this.chart1.Series[0].Points.AddXY("ДП3", txtRashod_PG_base_DP_3.Text);
            this.chart1.Series[0].Points.AddXY("ДП4", txtRashod_PG_base_DP_4.Text);
            this.chart1.Series[0].Points.AddXY("ДП5", txtRashod_PG_base_DP_5.Text);
            this.chart1.Series[0].Points.AddXY("ДП6", txtRashod_PG_base_DP_6.Text);
            this.chart1.Series[0].Points.AddXY("ДП7", txtRashod_PG_base_DP_7.Text);
            this.chart1.Series[0].Points.AddXY("ДП8", txtRashod_PG_base_DP_8.Text);

            this.chart1.Series[1].Points.AddXY("ДП1", choose.GetDouble(modelDPs[0].xId));
            this.chart1.Series[1].Points.AddXY("ДП2", choose.GetDouble(modelDPs[1].xId));
            this.chart1.Series[1].Points.AddXY("ДП3", choose.GetDouble(modelDPs[2].xId));
            this.chart1.Series[1].Points.AddXY("ДП4", choose.GetDouble(modelDPs[3].xId));
            this.chart1.Series[1].Points.AddXY("ДП5", choose.GetDouble(modelDPs[4].xId));
            this.chart1.Series[1].Points.AddXY("ДП6", choose.GetDouble(modelDPs[5].xId));
            this.chart1.Series[1].Points.AddXY("ДП7", choose.GetDouble(modelDPs[6].xId));
            this.chart1.Series[1].Points.AddXY("ДП8", choose.GetDouble(modelDPs[7].xId));
        }