예제 #1
0
        /** Example of how to use ``paropt``.
         *  Optimizes tasks whose names were read from command line.
         */
        public static void Main(string[] argv)
        {
            int n = argv.Length;

            mosek.Task[]    t   = new mosek.Task[n];
            mosek.rescode[] res = new mosek.rescode[n];
            mosek.rescode[] trm = new mosek.rescode[n];

            using (var env = new mosek.Env())
            {
                for (int i = 0; i < n; i++)
                {
                    t[i] = new mosek.Task(env);
                    t[i].readdata(argv[i]);
                    // Each task will be single-threaded
                    t[i].putintparam(mosek.iparam.intpnt_multi_thread, mosek.onoffkey.off);
                }

                paropt(t, res, trm);

                for (int i = 0; i < n; i++)
                {
                    Console.WriteLine("Task  {0}  res {1}   trm {2}   obj_val  {3}  time {4}",
                                      i,
                                      res[i],
                                      trm[i],
                                      t[i].getdouinf(mosek.dinfitem.intpnt_primal_obj),
                                      t[i].getdouinf(mosek.dinfitem.optimizer_time));
                }
            }
        }
예제 #2
0
        /**
         *  Given a continuous task, set up jobs to optimize it
         *  with a list of different solvers.
         *
         *  Returns an index, corresponding to the optimization
         *  task that is returned as winTask. This is the task
         *  with the best possible status of those that finished.
         *  If none task is considered successful returns -1.
         */
        public static int  optimizeconcurrent(mosek.Task task,
                                              int[]                 optimizers,
                                              out mosek.Task winTask,
                                              out mosek.rescode winTrm,
                                              out mosek.rescode winRes)
        {
            var n     = optimizers.Length;
            var tasks = new mosek.Task[n];
            var res   = new mosek.rescode[n];
            var trm   = new mosek.rescode[n];

            // Clone tasks and choose various optimizers
            for (var i = 0; i < n; ++i)
            {
                tasks[i] = new mosek.Task(task);
                tasks[i].putintparam(mosek.iparam.optimizer, optimizers[i]);
            }

            // Solve tasks in parallel
            bool success;
            int  firstOK;

            success = optimize(tasks, res, trm, out firstOK);

            if (success)
            {
                winTask = tasks[firstOK];
                winTrm  = trm[firstOK];
                winRes  = res[firstOK];
                return(firstOK);
            }
            else
            {
                winTask = null;
                winTrm  = 0;
                winRes  = 0;
                return(-1);
            }
        }
예제 #3
0
        /**
         *  Given a mixed-integer task, set up jobs to optimize it
         *  with different values of seed. That will lead to
         *  different execution paths of the optimizer.
         *
         *  Returns an index, corresponding to the optimization
         *  task that is returned as winTask. This is the task
         *  with the best value of the objective function.
         *  If none task is considered successful returns -1.
         *
         *  Typically, the input task would contain a time limit. The two
         *  major scenarios are:
         *  1. Some clone ends before time limit - then it has optimum.
         *  2. All clones reach time limit - pick the one with best objective.
         */
        public static int  optimizeconcurrentMIO(mosek.Task task,
                                                 int[]                 seeds,
                                                 out mosek.Task winTask,
                                                 out mosek.rescode winTrm,
                                                 out mosek.rescode winRes)
        {
            var n     = seeds.Length;
            var tasks = new mosek.Task[n];
            var res   = new mosek.rescode[n];
            var trm   = new mosek.rescode[n];

            // Clone tasks and choose various seeds for the optimizer
            for (var i = 0; i < n; ++i)
            {
                tasks[i] = new mosek.Task(task);
                tasks[i].putintparam(mosek.iparam.mio_seed, seeds[i]);
            }

            // Solve tasks in parallel
            bool success;
            int  firstOK;

            success = optimize(tasks, res, trm, out firstOK);

            if (success)
            {
                // Pick the task that ended with res = ok
                // and contains an integer solution with best objective value
                mosek.objsense sense   = task.getobjsense();
                double         bestObj = (sense == mosek.objsense.minimize) ? 1.0e+10 : -1.0e+10;
                int            bestPos = -1;

                for (var i = 0; i < n; ++i)
                {
                    Console.WriteLine("{0}   {1}   ", i, tasks[i].getprimalobj(mosek.soltype.itg));
                }

                for (var i = 0; i < n; ++i)
                {
                    if ((res[i] == mosek.rescode.ok) &&
                        (tasks[i].getsolsta(mosek.soltype.itg) == mosek.solsta.prim_feas ||
                         tasks[i].getsolsta(mosek.soltype.itg) == mosek.solsta.integer_optimal) &&
                        ((sense == mosek.objsense.minimize) ?
                         (tasks[i].getprimalobj(mosek.soltype.itg) < bestObj) :
                         (tasks[i].getprimalobj(mosek.soltype.itg) > bestObj)))
                    {
                        bestObj = tasks[i].getprimalobj(mosek.soltype.itg);
                        bestPos = i;
                    }
                }

                if (bestPos != -1)
                {
                    winTask = tasks[bestPos];
                    winTrm  = trm[bestPos];
                    winRes  = res[bestPos];
                    return(bestPos);
                }
            }

            winTask = null;
            winTrm  = 0;
            winRes  = 0;
            return(-1);
        }
예제 #4
0
        public static void Main()
        {
            const int numcon = 3;
            const int numvar = 4;

            // Since the value of infinity is ignored, we define it solely
            // for symbolic purposes
            double infinity = 0;

            double[] c    = { 3.0, 1.0, 5.0, 1.0 };
            int[][]  asub =
            {
                new int[] { 0, 1 },
                new int[] { 0,1, 2 },
                new int[] { 0, 1 },
                new int[] { 1, 2 }
            };
            double[][] aval =
            {
                new double[] { 3.0, 2.0 },
                new double[] { 1.0,1.0, 2.0 },
                new double[] { 2.0, 3.0 },
                new double[] { 1.0, 3.0 }
            };

            mosek.boundkey[] bkc = { mosek.boundkey.fx,
                                     mosek.boundkey.lo,
                                     mosek.boundkey.up };

            double[]         blc = { 30.0,
                                     15.0,
                                     -infinity };
            double[]         buc = { 30.0,
                                     +infinity,
                                     25.0 };
            mosek.boundkey[] bkx = { mosek.boundkey.lo,
                                     mosek.boundkey.ra,
                                     mosek.boundkey.lo,
                                     mosek.boundkey.lo };
            double[]         blx = { 0.0,
                                     0.0,
                                     0.0,
                                     0.0 };
            double[]         bux = { +infinity,
                                     10.0,
                                     +infinity,
                                     +infinity };

            try
            {
                // Make mosek environment.
                using (mosek.Env env = new mosek.Env())
                {
                    // Create a task object.
                    using (mosek.Task task = new mosek.Task(env, 0, 0))
                    {
                        // Directs the log task stream to the user specified
                        // method msgclass.streamCB
                        task.set_Stream(mosek.streamtype.log, new msgclass(""));

                        // Append 'numcon' empty constraints.
                        // The constraints will initially have no bounds.
                        task.appendcons(numcon);

                        // Append 'numvar' variables.
                        // The variables will initially be fixed at zero (x=0).
                        task.appendvars(numvar);

                        for (int j = 0; j < numvar; ++j)
                        {
                            // Set the linear term c_j in the objective.
                            task.putcj(j, c[j]);

                            // Set the bounds on variable j.
                            // blx[j] <= x_j <= bux[j]
                            task.putvarbound(j, bkx[j], blx[j], bux[j]);

                            // Input column j of A
                            task.putacol(j,        /* Variable (column) index.*/
                                         asub[j],  /* Row index of non-zeros in column j.*/
                                         aval[j]); /* Non-zero Values of column j. */
                        }

                        // Set the bounds on constraints.
                        // blc[i] <= constraint_i <= buc[i]
                        for (int i = 0; i < numcon; ++i)
                        {
                            task.putconbound(i, bkc[i], blc[i], buc[i]);
                        }

                        // Input the objective sense (minimize/maximize)
                        task.putobjsense(mosek.objsense.maximize);

                        // Solve the problem
                        task.optimize();

                        // Print a summary containing information
                        // about the solution for debugging purposes
                        task.solutionsummary(mosek.streamtype.msg);

                        // Get status information about the solution
                        mosek.solsta solsta;

                        task.getsolsta(mosek.soltype.bas, out solsta);

                        switch (solsta)
                        {
                        case mosek.solsta.optimal:
                            double[] xx = new double[numvar];
                            task.getxx(mosek.soltype.bas, // Request the basic solution.
                                       xx);

                            Console.WriteLine("Optimal primal solution\n");
                            for (int j = 0; j < numvar; ++j)
                            {
                                Console.WriteLine("x[{0}]: {1}", j, xx[j]);
                            }
                            break;

                        case mosek.solsta.dual_infeas_cer:
                        case mosek.solsta.prim_infeas_cer:
                            Console.WriteLine("Primal or dual infeasibility certificate found.\n");
                            break;

                        case mosek.solsta.unknown:
                            Console.WriteLine("Unknown solution status.\n");
                            break;

                        default:
                            Console.WriteLine("Other solution status");
                            break;
                        }
                    }
                }
            }
            catch (mosek.Exception e) {
                mosek.rescode res = e.Code;
                Console.WriteLine("Response code {0}\nMessage       {1}", res, e.Message);
            }
        }