Пример #1
0
        internal static void pp()
        {
            Network net = new Network();
            // number of materials
            int m = 3;
            // limit of each material
            int[] limit = new int[] { 1650, 1400, 1800 };
            // number of products
            int n = 2;
            // profit of each product
            int[] p = new int[] { 5, 4 };
            // amount of materials required to make each product
            int[][] a = new int[][] { new int[] { 15, 10, 9 }, new int[] { 11, 14, 20 } };

            // initialize variables for products
            IntVariable[] x = new IntVariable[n];
            for (int j = 0; j < n; j++)
            {
                x[j] = new IntVariable(net);
                x[j].Ge(0);
            }
            // generate constraits of limiting materials
            for (int i = 0; i < m; i++)
            {
                IntVariable sum = new IntVariable(net, 0);
                for (int j = 0; j < n; j++)
                {
                    sum = sum.Add(x[j].Multiply(a[j][i]));
                }
                sum.Le(limit[i]);
            }
            // total profit
            IntVariable profit = new IntVariable(net, 0);
            for (int j = 0; j < n; j++)
            {
                profit = profit.Add(x[j].Multiply(p[j]));
            }
            // maximize the total profit
            net.Objective = profit;
            // iteratively find a better solution until the optimal solution is found
            Solver solver = new DefaultSolver(net, Solver.Maximize | Solver.Better);
            for (solver.Start(); solver.WaitNext(); solver.Resume())
            {
                Solution solution = solver.Solution;
                Console.WriteLine(solver.GetCount());
                Console.Out.WriteLine("Profit = " + solution.GetIntValue(profit));
                for (int j = 0; j < n; j++)
                {
                    Console.Out.WriteLine("x[" + j + "]=" + solution.GetIntValue(x[j]));
                }
                Console.Out.WriteLine();
            }

            solver.Stop();
            Console.ReadLine();
        }
Пример #2
0
 internal static void runExample(Network net, int opt)
 {
     Console.Out.WriteLine("# Problem");
     //UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
     Console.Out.WriteLine(net);
     Console.Out.WriteLine("# Solutions");
     Solver solver = new DefaultSolver(net, opt);
     for (solver.Start(); solver.WaitNext(); solver.Resume())
     {
         Solution solution = solver.Solution;
         Console.Out.WriteLine(solution);
     }
     solver.Stop();
     long count = solver.GetCount();
     long time = solver.GetElapsedTime();
     Console.Out.WriteLine("Found " + count + " solutions in " + time + " milli seconds");
     Console.Out.WriteLine();
 }