コード例 #1
0
    public static void Main(String[] args)
    {
        mosek.Env
            env = null;
        mosek.Task
            task = null;
        mosek.Task[]
        task_list = new mosek.Task[] { null };

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

            task_list[0] = new mosek.Task(env, 0, 0);
            task_list[0].set_Stream(mosek.streamtype.log, new msgclass("intrpnt: "));

            task.readdata(args[0]);

            // Assign different parameter values to each task.
            // In this case different optimizers.
            task.putintparam(mosek.iparam.optimizer,
                             mosek.optimizertype.primal_simplex);

            task_list[0].putintparam(mosek.iparam.optimizer,
                                     mosek.optimizertype.intpnt);


            // Optimize task and task_list[0] in parallel.
            // The problem data i.e. C, A, etc.
            // is copied from task to task_list[0].
            task.optimizeconcurrent(task_list);

            task.solutionsummary(mosek.streamtype.log);
        }
        catch (mosek.Exception e)
        {
            Console.WriteLine(e.Code);
            Console.WriteLine(e);
        }

        if (task != null)
        {
            task.Dispose();
        }
        if (env != null)
        {
            env.Dispose();
        }
    }
コード例 #2
0
ファイル: concurrent2.cs プロジェクト: edljk/Mosek.jl
  public static void Main (String[] args)
    {
      mosek.Env
        env = null;
      mosek.Task
        task = null;
      mosek.Task[]
        task_list = new mosek.Task[] { null };
            
      try
        {
          // Create mosek environment. 
          env  = new mosek.Env ();
          // Create a task object linked with the environment env.
          task = new mosek.Task (env, 0,0);
          // Directs the log task stream to the user specified
          // method task_msg_obj.streamCB
          task.set_Stream (mosek.streamtype.log, new msgclass ("simplex: "));

          task_list[0] = new mosek.Task (env, 0,0);
          task_list[0].set_Stream (mosek.streamtype.log, new msgclass ("intrpnt: "));

          task.readdata(args[0]);

          // Assign different parameter values to each task. 
          // In this case different optimizers. 
          task.putintparam(mosek.iparam.optimizer,
                           mosek.optimizertype.primal_simplex);
  
          task_list[0].putintparam(mosek.iparam.optimizer,
                                   mosek.optimizertype.intpnt);
  

          // Optimize task and task_list[0] in parallel.
          // The problem data i.e. C, A, etc. 
          // is copied from task to task_list[0].
          task.optimizeconcurrent(task_list);
          
          task.solutionsummary(mosek.streamtype.log);
        }
      catch (mosek.Exception e)
      {
        Console.WriteLine (e.Code);
        Console.WriteLine (e);
      }
          
      if (task != null) task.Dispose ();
      if (env  != null)  env.Dispose ();
          
    }
コード例 #3
0
    public static void Main(String[] args)
    {
        mosek.Env
            env = null;
        mosek.Task
            task = null;

        try
        {
            // Create mosek environment.
            env = new mosek.Env();
            // Direct the env log stream to the user specified
            // method env_msg_obj.streamCB
            env.set_Stream(mosek.streamtype.log, new msgclass("[env]"));
            // Initialize the environment.
            env.init();
            // Create a task object linked with the environment env.
            task = new mosek.Task(env, 0, 0);
            // Directs the log task stream to the user specified
            // method task_msg_obj.streamCB
            task.set_Stream(mosek.streamtype.log, new msgclass("[task]"));

            task.readdata(args[0]);
            task.putintparam(mosek.iparam.optimizer,
                             mosek.Val.optimizer_concurrent);
            task.putintparam(mosek.iparam.concurrent_num_optimizers,
                             2);

            task.optimize();

            task.solutionsummary(mosek.streamtype.msg);
        }
        catch (mosek.Exception e)
        {
            Console.WriteLine(e.Code);
            Console.WriteLine(e);
        }

        if (task != null)
        {
            task.Dispose();
        }
        if (env != null)
        {
            env.Dispose();
        }
    }
コード例 #4
0
        public static void Main()
        {
            const int numcon = 2;
            const int numvar = 2;

            // Since the value infinity is never used, we define
            // 'infinity' symbolic purposes only
            double infinity = 0;


            mosek.boundkey[] bkc = { mosek.boundkey.up,
                                     mosek.boundkey.lo };
            double[]         blc = { -infinity,
                                     -4.0 };
            double[]         buc = { 250.0,
                                     infinity };

            mosek.boundkey[] bkx = { mosek.boundkey.lo,
                                     mosek.boundkey.lo };
            double[]         blx = { 0.0,
                                     0.0 };
            double[]         bux = { infinity,
                                     infinity };

            double[]   c    = { 1.0, 0.64 };
            int[][]    asub = { new int[]  { 0, 1 }, new int[] { 0, 1 } };
            double[][] aval = { new double[] { 50.0, 3.0 }, new double[] { 31.0, -2.0 } };

            double[] xx = new double[numvar];

            mosek.Env  env  = null;
            mosek.Task task = null;

            try
            {
                // Make mosek environment.
                env = new mosek.Env();
                // Create a task object linked with the environment env.
                task = new mosek.Task(env, numcon, numvar);
                // Directs the log task stream to the user specified
                // method task_msg_obj.streamCB
                MsgClass task_msg_obj = new MsgClass();
                task.set_Stream(mosek.streamtype.log, task_msg_obj);

                /* Give MOSEK an estimate of the size of the input data.
                 *   This is done to increase the speed of inputting data.
                 *   However, it is optional. */
                /* 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);

                /* Optionally add a constant term to the objective. */
                task.putcfix(0.0);

                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.
                 *     for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
                for (int i = 0; i < numcon; ++i)
                {
                    task.putconbound(i, bkc[i], blc[i], buc[i]);
                }

                /* Specify integer variables. */
                for (int j = 0; j < numvar; ++j)
                {
                    task.putvartype(j, mosek.variabletype.type_int);
                }
                task.putobjsense(mosek.objsense.maximize);

                /* Set max solution time */
                task.putdouparam(mosek.dparam.mio_max_time, 60.0);

                task.optimize();


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

                mosek.solsta solsta;
                /* Get status information about the solution */
                task.getsolsta(mosek.soltype.itg, out solsta);
                task.getxx(mosek.soltype.itg, // Integer solution.
                           xx);

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

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

                case mosek.solsta.unknown:
                    mosek.prosta prosta;
                    task.getprosta(mosek.soltype.itg, out prosta);
                    switch (prosta)
                    {
                    case mosek.prosta.prim_infeas_or_unbounded:
                        Console.WriteLine("Problem status Infeasible or unbounded");
                        break;

                    case mosek.prosta.prim_infeas:
                        Console.WriteLine("Problem status Infeasible.");
                        break;

                    case mosek.prosta.unknown:
                        Console.WriteLine("Problem status unknown.");
                        break;

                    default:
                        Console.WriteLine("Other problem status.");
                        break;
                    }
                    break;

                default:
                    Console.WriteLine("Other solution status");
                    break;
                }
            }
            catch (mosek.Exception e)
            {
                Console.WriteLine(e.Code);
                Console.WriteLine(e);
                throw;
            }
            finally
            {
                if (task != null)
                {
                    task.Dispose();
                }
                if (env != null)
                {
                    env.Dispose();
                }
            }
        }
コード例 #5
0
ファイル: milo1.cs プロジェクト: edljk/Mosek.jl
  public static void Main ()
  {
    const int numcon = 2;
    const int numvar = 2;

    // Since the value infinity is never used, we define
    // 'infinity' symbolic purposes only
    double infinity = 0;

        
    mosek.boundkey[] bkc = { mosek.boundkey.up,
                             mosek.boundkey.lo };
    double[] blc = { -infinity,
                     -4.0 };
    double[] buc = { 250.0,
                     infinity }; 

    mosek.boundkey[] bkx = { mosek.boundkey.lo,
                             mosek.boundkey.lo  };
    double[] blx = { 0.0,
                     0.0 };
    double[] bux = { infinity,
                     infinity };

    double[] c   = {1.0, 0.64 };
    int[][] asub    = { new int[]  {0,   1},  new int[] {0,    1}   };
    double[][] aval = { new double[] {50.0, 3.0},new double[] {31.0, -2.0} };
        
    double[] xx  = new double[numvar];

    mosek.Env env = null;
    mosek.Task task = null;
    
    try
      {
      // Make mosek environment. 
      env  = new mosek.Env ();
      // Create a task object linked with the environment env.
      task = new mosek.Task (env, numcon,numvar);
      // Directs the log task stream to the user specified
      // method task_msg_obj.streamCB
      MsgClass task_msg_obj = new MsgClass ();
      task.set_Stream (mosek.streamtype.log,task_msg_obj);
      
      /* Give MOSEK an estimate of the size of the input data. 
           This is done to increase the speed of inputting data. 
           However, it is optional. */
      /* 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);

      /* Optionally add a constant term to the objective. */
      task.putcfix(0.0);

      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.
             for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
      for(int i=0; i<numcon; ++i)
        task.putconbound(i,bkc[i],blc[i],buc[i]);
            
      /* Specify integer variables. */
      for(int j=0; j<numvar; ++j)
        task.putvartype(j,mosek.variabletype.type_int);
      task.putobjsense(mosek.objsense.maximize);
           
      task.optimize();
             
      // Print a summary containing information
      //   about the solution for debugging purposes
      task.solutionsummary(mosek.streamtype.msg);
      
      mosek.solsta solsta;
      /* Get status information about the solution */
      task.getsolsta(mosek.soltype.itg, out solsta);    
      task.getxx(mosek.soltype.itg, // Integer solution.     
                 xx);
      
      switch(solsta)
      {
      case mosek.solsta.optimal:
      case mosek.solsta.near_optimal:      
        Console.WriteLine ("Optimal primal solution\n");
        for(int j = 0; j < numvar; ++j)
          Console.WriteLine ("x[{0}]:",xx[j]);
        break;
      case mosek.solsta.dual_infeas_cer:
      case mosek.solsta.prim_infeas_cer:
      case mosek.solsta.near_dual_infeas_cer:
      case mosek.solsta.near_prim_infeas_cer:  
        Console.WriteLine("Primal or dual infeasibility.\n");
        break;
      case mosek.solsta.unknown:
        mosek.prosta prosta;
        task.getprosta(mosek.soltype.itg,out prosta);
        switch(prosta)
        {
        case mosek.prosta.prim_infeas_or_unbounded:
          Console.WriteLine("Problem status Infeasible or unbounded");
          break;
        case mosek.prosta.prim_infeas:
          Console.WriteLine("Problem status Infeasible.");
          break;
        case mosek.prosta.unknown:
          Console.WriteLine("Problem status unknown.");
          break;
        default:
          Console.WriteLine("Other problem status.");
          break;
        }
        break;
      default:
        Console.WriteLine("Other solution status");
        break;
      }
    }
    catch (mosek.Exception e)
    {
      Console.WriteLine (e.Code);
      Console.WriteLine (e);
      throw;
    }
    finally
      {
      if (task != null) task.Dispose ();
      if (env  != null)  env.Dispose ();
    }
  }      
コード例 #6
0
ファイル: solvelinear.cs プロジェクト: smahdie1/active-delays
    public static void Main()
    {
        const int NUMCON = 2;
        const int NUMVAR = 2;


        int numvar = 2;
        int numcon = 2; /* we must have numvar == numcon */

        double[][]
        aval = new double[NUMVAR][];

        aval[0] = new double[] { -1.0 };
        aval[1] = new double[] { 1.0, 1.0 };


        int[][]
        asub = new int[NUMVAR][];

        asub[0] = new int[] { 1 };
        asub[1] = new int[] { 0, 1 };

        int [] ptrb = { 0, 1 };
        int [] ptre = { 1, 3 };

        int[]    bsub  = new int[numvar];
        double[] b     = new double[numvar];
        int[]    basis = new int[numvar];

        mosek.Task
            task = null;
        mosek.Env
            env = null;


        try
        {
            // Make mosek environment.
            env = new mosek.Env();
            // Direct the env log stream to the user specified
            // method env_msg_obj.streamCB
            env.set_Stream(mosek.streamtype.log, new msgclass("[env]"));
            // Initialize the environment.
            env.init();
            // Create a task object linked with the environment env.
            task = new mosek.Task(env, NUMCON, NUMVAR);
            // Directs the log task stream to the user specified
            // method task_msg_obj.streamCB
            task.set_Stream(mosek.streamtype.log, new msgclass("[task]"));


            /* Put A matrix and factor A.
             * Call this function only once for a given task. */

            put_a(
                task,
                aval,
                asub,
                ptrb,
                ptre,
                numvar,
                basis
                );

            /* now solve rhs */
            b[0]    = 1;
            b[1]    = -2;
            bsub[0] = 0;
            bsub[1] = 1;
            int nz = 2;

            task.solvewithbasis(0, ref nz, bsub, b);
            Console.WriteLine("\nSolution to Bx = b:\n\n");

            /* Print solution and show correspondents
             * to original variables in the problem */
            for (int i = 0; i < nz; ++i)
            {
                if (basis[bsub[i]] < numcon)
                {
                    Console.WriteLine("This should never happen\n");
                }
                else
                {
                    Console.WriteLine("x{0} = {1}\n", basis[bsub[i]] - numcon, b[bsub[i]]);
                }
            }

            b[0]    = 7;
            bsub[0] = 0;
            nz      = 1;

            task.solvewithbasis(0, ref nz, bsub, b);

            Console.WriteLine("\nSolution to Bx = b:\n\n");

            /* Print solution and show correspondents
             * to original variables in the problem */
            for (int i = 0; i < nz; ++i)
            {
                if (basis[bsub[i]] < numcon)
                {
                    Console.WriteLine("This should never happen\n");
                }
                else
                {
                    Console.WriteLine("x{0} = {1}\n", basis[bsub[i]] - numcon, b[bsub[i]]);
                }
            }
        }
        catch (mosek.Exception e)
        {
            Console.WriteLine(e.Code);
            Console.WriteLine(e);
        }

        if (task != null)
        {
            task.Dispose();
        }
        if (env != null)
        {
            env.Dispose();
        }
    }
コード例 #7
0
ファイル: qo1.cs プロジェクト: edljk/Mosek.jl
  public static void Main ()
  {
    // Since the value infinity is never used, we define
    // 'infinity' symbolic purposes only
    const double infinity = 0;
    const int numcon = 1;   /* Number of constraints.             */
    const int numvar = 3;   /* Number of variables.               */

    double[] c = {0.0,-1.0,0.0};
    
    mosek.boundkey[]  bkc   = {mosek.boundkey.lo};
    double[] blc = {1.0};
    double[] buc = {infinity};
    
    mosek.boundkey[]  bkx   = {mosek.boundkey.lo,
                               mosek.boundkey.lo,
                               mosek.boundkey.lo};        
    double[] blx  = {0.0,
                     0.0,
                     0.0};
    double[] bux  = {+infinity,
                     +infinity,
                     +infinity};
     
    int[][]    asub  = { new int[] {0},   new int[] {0},   new int[] {0}};
    double[][] aval  = { new double[] {1.0}, new double[] {1.0}, new double[] {1.0}};
        
    mosek.Task 
      task = null;
    mosek.Env
      env = null;
    double[] xx  = new double[numvar];
    try
    {     
      // Make mosek environment. 
      env  = new mosek.Env ();
      // Create a task object linked with the environment env.
      task = new mosek.Task (env, 0,0);
      // Directs the log task stream to the user specified
      // method task_msg_obj.streamCB
      task.set_Stream (mosek.streamtype.log, new msgclass (""));

      /* Give MOSEK an estimate of the size of the input data. 
           This is done to increase the speed of inputting data. 
           However, it is optional. */
      /* 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.
             for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
      for(int i=0; i<numcon; ++i)
        task.putconbound(i,bkc[i],blc[i],buc[i]);

      /*
       * The lower triangular part of the Q
       * matrix in the objective is specified.
       */

      int[]    qsubi = {0,   1,    2,   2  };
      int[]    qsubj = {0,   1,    0,   2  };
      double[] qval =  {2.0, 0.2, -1.0, 2.0};
            
      /* Input the Q for the objective. */

      task.putobjsense(mosek.objsense.minimize);

      task.putqobj(qsubi,qsubj,qval);

      task.optimize();

      // Print a summary containing information
      //   about the solution for debugging purposes
      task.solutionsummary(mosek.streamtype.msg);
      
      mosek.solsta solsta;
      /* Get status information about the solution */
      task.getsolsta(mosek.soltype.itr, out solsta);    
      switch(solsta)
        {
        case mosek.solsta.optimal:
        case mosek.solsta.near_optimal:      
      task.getxx(mosek.soltype.itr, // Interior point solution.     
                 xx);
                
          Console.WriteLine ("Optimal primal solution\n");
          for(int j = 0; j < numvar; ++j)
            Console.WriteLine ("x[{0}]:",xx[j]);
          break;
        case mosek.solsta.dual_infeas_cer:
        case mosek.solsta.prim_infeas_cer:
        case mosek.solsta.near_dual_infeas_cer:
              case mosek.solsta.near_prim_infeas_cer:  
          Console.WriteLine("Primal or dual infeasibility.\n");
          break;
        case mosek.solsta.unknown:
          Console.WriteLine("Unknown solution status.\n");
          break;
        default:
          Console.WriteLine("Other solution status");
          break;
        } 
    }
    catch (mosek.Exception e)
      {
        Console.WriteLine (e);
        throw;
      }
    finally
      {
        if (task != null) task.Dispose ();
        if (env  != null)  env.Dispose ();
      }
  } /* Main */
コード例 #8
0
    public static void Main(String[] args)
    {
        const double infinity = 0.0;

        mosek.Env
            env = null;
        mosek.Task
            task = null;
        mosek.Task
            task_relaxprimal = null;

        double[] wlc = { 1.0, 1.0, 1.0, 1.0 };
        double[] wuc = { 1.0, 1.0, 1.0, 1.0 };
        double[] wlx = { 1.0, 1.0 };
        double[] wux = { 1.0, 1.0 };
        double   sum_violation;

        try
        {
            // Make mosek environment.
            env = new mosek.Env();
            // Direct the env log stream to the user specified
            // method env_msg_obj.streamCB
            env.set_Stream(mosek.streamtype.log, new msgclass("[env]"));
            // Initialize the environment.
            env.init();

            // Create a task object linked with the environment env.
            task = new mosek.Task(env, 0, 0);
            // Directs the log task stream to the user specified
            // method task_msg_obj.streamCB
            task.set_Stream(mosek.streamtype.log, new msgclass("[task]"));


            /* read file from current dir */
            task.readdata(args[0]);
            task.putintparam(mosek.iparam.feasrepair_optimize,
                             mosek.Val.feasrepair_optimize_penalty);
            Console.WriteLine("Start relax primal");
            task.relaxprimal(out task_relaxprimal,
                             wlc,
                             wuc,
                             wlx,
                             wux);
            Console.WriteLine("End relax primal");

            task_relaxprimal.getprimalobj(mosek.soltype.bas, out sum_violation);
            Console.WriteLine("Minimized sum of violations = {0}", sum_violation);

            /* modified bound returned in wlc,wuc,wlx,wux */

            for (int i = 0; i < 4; ++i)
            {
                if (wlc[i] == -infinity)
                {
                    Console.WriteLine("lbc[{0}] = -inf, ", i);
                }
                else
                {
                    Console.WriteLine("lbc[{0}] = {1}, ", i, wlc[i]);
                }

                if (wuc[i] == infinity)
                {
                    Console.WriteLine("ubc[{0}] = inf\n", i);
                }
                else
                {
                    Console.WriteLine("ubc[{0}] = {1}\n", i, wuc[i]);
                }
            }

            for (int i = 0; i < 2; ++i)
            {
                if (wlx[i] == -infinity)
                {
                    Console.WriteLine("lbx[{0}] = -inf, ", i);
                }
                else
                {
                    Console.WriteLine("lbx[{0}] = {1}, ", i, wlx[i]);
                }

                if (wux[i] == infinity)
                {
                    Console.WriteLine("ubx[{0}] = inf\n", i);
                }
                else
                {
                    Console.WriteLine("ubx[{0}] = {1}\n", i, wux[i]);
                }
            }
        }
        catch (mosek.Exception e)
        {
            Console.WriteLine(e.Code);
            Console.WriteLine(e);
        }

        if (task != null)
        {
            task.Dispose();
        }
        if (task_relaxprimal != null)
        {
            task_relaxprimal.Dispose();
        }
        if (env != null)
        {
            env.Dispose();
        }
    }
コード例 #9
0
ファイル: solvebasis.cs プロジェクト: smahdie1/active-delays
    public static void Main()
    {
        const int NUMCON = 2;
        const int NUMVAR = 2;

        // Since the value infinity is never used, we define
        // 'infinity' symbolic purposes only
        double
            infinity = 0;

        double[]         c    = { 1.0, 1.0 };
        int[]            ptrb = { 0, 2 };
        int[]            ptre = { 2, 3 };
        int[]            asub = { 0, 1,
                                  0, 1 };
        double[]         aval = { 1.0, 1.0,
                                  2.0, 1.0 };
        mosek.boundkey[] bkc = { mosek.boundkey.up,
                                 mosek.boundkey.up };

        double[] blc = { -infinity,
                         -infinity };
        double[] buc = { 2.0,
                         6.0 };

        mosek.boundkey[] bkx = { mosek.boundkey.lo,
                                 mosek.boundkey.lo };
        double[]         blx = { 0.0,
                                 0.0 };

        double[] bux = { +infinity,
                         +infinity };
        mosek.Task
            task = null;
        mosek.Env
            env = null;

        double[] w1 = { 2.0, 6.0 };
        double[] w2 = { 1.0, 0.0 };
        try
        {
            // Make mosek environment.
            env = new mosek.Env();
            // Direct the env log stream to the user specified
            // method env_msg_obj.streamCB
            env.set_Stream(mosek.streamtype.log, new msgclass("[env]"));
            // Initialize the environment.
            env.init();
            // Create a task object linked with the environment env.
            task = new mosek.Task(env, NUMCON, NUMVAR);
            // Directs the log task stream to the user specified
            // method task_msg_obj.streamCB
            task.set_Stream(mosek.streamtype.log, new msgclass("[task]"));
            task.inputdata(NUMCON, NUMVAR,
                           c,
                           0.0,
                           ptrb,
                           ptre,
                           asub,
                           aval,
                           bkc,
                           blc,
                           buc,
                           bkx,
                           blx,
                           bux);
            task.putobjsense(mosek.objsense.maximize);
            try
            {
                task.optimize();
            }
            catch (mosek.Warning w)
            {
                Console.WriteLine("Mosek warning:");
                Console.WriteLine(w.Code);
                Console.WriteLine(w);
            }

            int[] basis = new int[NUMCON];
            task.initbasissolve(basis);

            //List basis variables corresponding to columns of B
            int[] varsub = { 0, 1 };
            for (int i = 0; i < NUMCON; i++)
            {
                if (basis[varsub[i]] < NUMCON)
                {
                    Console.WriteLine("Basis variable no {0} is xc{1}",
                                      i,
                                      basis[i]);
                }
                else
                {
                    Console.WriteLine("Basis variable no {0} is x{1}",
                                      i,
                                      basis[i] - NUMCON);
                }
            }

            // solve Bx = w1
            // varsub contains index of non-zeros in b.
            //  On return b contains the solution x and
            // varsub the index of the non-zeros in x.
            int nz = 2;

            task.solvewithbasis(0, ref nz, varsub, w1);
            Console.WriteLine("nz = {0}", nz);
            Console.WriteLine("Solution to Bx = w1:\n");

            for (int i = 0; i < nz; i++)
            {
                if (basis[varsub[i]] < NUMCON)
                {
                    Console.WriteLine("xc {0} = {1}",
                                      basis[varsub[i]],
                                      w1[varsub[i]]);
                }
                else
                {
                    Console.WriteLine("x{0} = {1}",
                                      basis[varsub[i]] - NUMCON,
                                      w1[varsub[i]]);
                }
            }

            // Solve B^Tx = w2
            nz        = 1;
            varsub[0] = 0;

            task.solvewithbasis(1, ref nz, varsub, w2);

            Console.WriteLine("\nSolution to B^Tx = w2:\n");

            for (int i = 0; i < nz; i++)
            {
                if (basis[varsub[i]] < NUMCON)
                {
                    Console.WriteLine("xc {0} = {1}",
                                      basis[varsub[i]],
                                      w2[varsub[i]]);
                }
                else
                {
                    Console.WriteLine("x {0} = {1}",
                                      basis[varsub[i]] - NUMCON,
                                      w2[varsub[i]]);
                }
            }
        }
        catch (mosek.Exception e)
        {
            Console.WriteLine(e.Code);
            Console.WriteLine(e);
        }

        if (task != null)
        {
            task.Dispose();
        }
        if (env != null)
        {
            env.Dispose();
        }
    }
コード例 #10
0
ファイル: lo2.cs プロジェクト: edljk/Mosek.jl
  public static void Main ()
  {
    const int numcon = 3;
    const int numvar = 4;
    
    // Since the value infinity is never used, we define
    // 'infinity' symbolic purposes only
    double
      infinity = 0;
    
    double[] c    = {3.0, 1.0, 5.0, 1.0};
    int[][]    asub = { new int[] {0,1,2},
                        new int[] {0,1,2,3},
                        new int[] {1,3} };
    double[][] aval = { new double[] {3.0,1.0,2.0},
                        new double[] {2.0,1.0,3.0,1.0},
                        new double[] {2.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};

   mosek.Task 
     task = null;
   mosek.Env  
     env  = null;
   
   double[] xx  = new double[numvar];
   
   try
     {
     // Make mosek environment. 
     env  = new mosek.Env ();
     // Create a task object linked with the environment env.
     task = new mosek.Task (env, 0,0);
     // Directs the log task stream to the user specified
     // method task_msg_obj.streamCB
     task.set_Stream (mosek.streamtype.log, new msgclass (""));

     /* Give MOSEK an estimate of the size of the input data. 
           This is done to increase the speed of inputting data. 
           However, it is optional. */
     /* 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);

     /* Optionally add a constant term to the objective. */
     task.putcfix(0.0);

     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]);
     }
     /* Set the bounds on constraints.
             for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
     for(int i=0; i<numcon; ++i)
     {
         task.putconbound(i,bkc[i],blc[i],buc[i]);

          /* Input row i of A */   
          task.putarow(i,                     /* Row index.*/
                       asub[i],               /* Column indexes of non-zeros in row i.*/
                       aval[i]);              /* Non-zero Values of row i. */
     }

     task.putobjsense(mosek.objsense.maximize);
     task.optimize();
            // Print a summary containing information
            //   about the solution for debugging purposes
     task.solutionsummary(mosek.streamtype.msg);
     
     mosek.solsta solsta;
     /* Get status information about the solution */
     task.getsolsta(mosek.soltype.bas, out solsta);    
     task.getxx(mosek.soltype.bas, // Basic solution.     
                xx);
                
     switch(solsta)
     {
       case mosek.solsta.optimal:
       case mosek.solsta.near_optimal:      
         Console.WriteLine ("Optimal primal solution\n");
         for(int j = 0; j < numvar; ++j)
           Console.WriteLine ("x[{0}]:",xx[j]);
         break;
       case mosek.solsta.dual_infeas_cer:
       case mosek.solsta.prim_infeas_cer:
       case mosek.solsta.near_dual_infeas_cer:
       case mosek.solsta.near_prim_infeas_cer:  
         Console.WriteLine("Primal or dual infeasibility.\n");
         break;
       case mosek.solsta.unknown:
         Console.WriteLine("Unknown solution status.\n");
         break;
       default:
         Console.WriteLine("Other solution status");
         break;
     }
   }
   catch (mosek.Exception e)
   {
     Console.WriteLine (e.Code);
     Console.WriteLine (e);
     throw;
   }
   finally
     {
     if (task != null) task.Dispose ();
     if (env  != null)  env.Dispose ();
   }
  }
コード例 #11
0
ファイル: lo2.cs プロジェクト: Giarcr0b/MVO_Tool
    public static void Main()
    {
        const int numcon = 3;
        const int numvar = 4;

        // Since the value infinity is never used, we define
        // 'infinity' symbolic purposes only
        double
            infinity = 0;

        double[]   c    = { 3.0, 1.0, 5.0, 1.0 };
        int[][]    asub = { new int[] { 0, 1, 2 },
                            new int[] { 0, 1,2, 3 },
                            new int[] { 1, 3 } };
        double[][] aval = { new double[] { 3.0, 1.0, 2.0 },
                            new double[] { 2.0, 1.0,3.0, 1.0 },
                            new double[] { 2.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 };

        mosek.Task
            task = null;
        mosek.Env
            env = null;

        double[] xx = new double[numvar];

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

            /* Give MOSEK an estimate of the size of the input data.
             *    This is done to increase the speed of inputting data.
             *    However, it is optional. */
            /* 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);

            /* Optionally add a constant term to the objective. */
            task.putcfix(0.0);

            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]);
            }

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

                /* Input row i of A */
                task.putarow(i,               /* Row index.*/
                             asub[i],         /* Column indexes of non-zeros in row i.*/
                             aval[i]);        /* Non-zero Values of row i. */
            }

            task.putobjsense(mosek.objsense.maximize);
            task.optimize();

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

            mosek.solsta solsta;
            /* Get status information about the solution */
            task.getsolsta(mosek.soltype.bas, out solsta);
            task.getxx(mosek.soltype.bas, // Basic solution.
                       xx);

            switch (solsta)
            {
            case mosek.solsta.optimal:
            case mosek.solsta.near_optimal:
                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:
            case mosek.solsta.near_dual_infeas_cer:
            case mosek.solsta.near_prim_infeas_cer:
                Console.WriteLine("Primal or dual infeasibility.\n");
                break;

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

            default:
                Console.WriteLine("Other solution status");
                break;
            }
        }
        catch (mosek.Exception e)
        {
            Console.WriteLine(e.Code);
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            if (task != null)
            {
                task.Dispose();
            }
            if (env != null)
            {
                env.Dispose();
            }
        }
    }
コード例 #12
0
    public static void Main(string[] args)
    {
        mosek.Task task = null;
        mosek.Env  env  = null;

        if (args.Length == 0)
        {
            Console.WriteLine("Missing argument. The syntax is:");
            Console.WriteLine(" simple inputfile [ solutionfile ]");
        }
        else
        {
            try
            {
                // Make mosek environment.

                env = new mosek.Env();
                // Initialize the environment.

                env.init();

                // Create a task object linked with the environment env.
                //  We create it initially with 0 variables and 0 columns,
                //  since we don't know the size of the problem.
                task = new mosek.Task(env, 0, 0);

                // We assume that a problem file was given as the first command
                // line argument (received in `args')
                task.readdata(args[0]);

                // Solve the problem
                task.optimize();

                // Print a summary of the solution
                task.solutionsummary(mosek.streamtype.log);

                // If an output file was specified, write a solution
                if (args.Length > 1)
                {
                    // We define the output format to be OPF, and tell MOSEK to
                    // leave out parameters and problem data from the output file.
                    task.putintparam(mosek.iparam.write_data_format, mosek.dataformat.op);
                    task.putintparam(mosek.iparam.opf_write_solutions, mosek.onoffkey.on);
                    task.putintparam(mosek.iparam.opf_write_hints, mosek.onoffkey.off);
                    task.putintparam(mosek.iparam.opf_write_parameters, mosek.onoffkey.off);
                    task.putintparam(mosek.iparam.opf_write_problem, mosek.onoffkey.off);

                    task.writedata(args[1]);
                }
            }
            finally
            {
                // Dispose of task end environment
                if (task != null)
                {
                    task.Dispose();
                }
                if (env != null)
                {
                    env.Dispose();
                }
            }
        }
    }
コード例 #13
0
ファイル: sensitivity.cs プロジェクト: smahdie1/active-delays
    public static void Main()
    {
        mosek.Env
            env = null;
        mosek.Task
            task = null;
        const double
            infinity = 0;

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

        mosek.boundkey[] bkx = new mosek.boundkey[] {
            mosek.boundkey.lo, mosek.boundkey.lo,
            mosek.boundkey.lo, mosek.boundkey.lo,
            mosek.boundkey.lo, mosek.boundkey.lo,
            mosek.boundkey.lo
        };

        int[]    ptrb = new int[] { 0, 2, 4, 6, 8, 10, 12 };
        int[]    ptre = new int[] { 2, 4, 6, 8, 10, 12, 14 };
        int[]    sub  = new int[] { 0, 3, 0, 4, 1, 5, 1, 6, 2, 3, 2, 5, 2, 6 };
        double[] blc  = new double[] {
            -infinity, -infinity,
            -infinity, 800, 100, 500, 500
        };

        double[] buc = new double[] { 400, 1200, 1000, 800, 100, 500, 500 };
        double[] c   = new double[] { 1.0, 2.0, 5.0, 2.0, 1.0, 2.0, 1.0 };
        double[] blx = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
        double[] bux = new double[] { infinity,
                                      infinity,
                                      infinity,
                                      infinity,
                                      infinity,
                                      infinity,
                                      infinity };

        double[] val = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
                                      1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };

        int NUMCON = 7;  /* Number of constraints.             */
        int NUMVAR = 7;  /* Number of variables.               */
        int NUMANZ = 14; /* Number of non-zeros in A.           */

        try
        {
            // Create mosek environment.
            env = new mosek.Env();
            // Direct the env log stream to the user specified
            // method env_msg_obj.streamCB
            env.set_Stream(mosek.streamtype.log, new msgclass("[env]"));
            // Initialize the environment.
            env.init();

            // Create a task object linked with the environment env.
            task = new mosek.Task(env, NUMCON, NUMVAR);
            // Directs the log task stream to the user specified
            // method task_msg_obj.streamCB
            task.set_Stream(mosek.streamtype.log, new msgclass("[task]"));

            task.inputdata(NUMCON, NUMVAR,
                           c,
                           0.0,
                           ptrb,
                           ptre,
                           sub,
                           val,
                           bkc,
                           blc,
                           buc,
                           bkx,
                           blx,
                           bux);

            /* A maximization problem */
            task.putobjsense(mosek.objsense.minimize);

            try
            {
                task.optimize();
            }
            catch (mosek.Warning w)
            {
                Console.WriteLine("Mosek warning:");
                Console.WriteLine(w.Code);
                Console.WriteLine(w);
            }


            /* Analyze upper bound on c1 and the equality constraint on c4 */
            int[]        subi  = new int [] { 0, 3 };
            mosek.mark[] marki = new mosek.mark[] { mosek.mark.up,
                                                    mosek.mark.up };

            /* Analyze lower bound on the variables x12 and x31 */
            int[]        subj  = new int [] { 1, 4 };
            mosek.mark[] markj = new mosek.mark[] { mosek.mark.lo,
                                                    mosek.mark.lo };

            double[] leftpricei  = new  double[2];
            double[] rightpricei = new  double[2];
            double[] leftrangei  = new  double[2];
            double[] rightrangei = new  double[2];
            double[] leftpricej  = new  double[2];
            double[] rightpricej = new  double[2];
            double[] leftrangej  = new  double[2];
            double[] rightrangej = new  double[2];


            task.primalsensitivity(subi,
                                   marki,
                                   subj,
                                   markj,
                                   leftpricei,
                                   rightpricei,
                                   leftrangei,
                                   rightrangei,
                                   leftpricej,
                                   rightpricej,
                                   leftrangej,
                                   rightrangej);

            Console.Write("Results from sensitivity analysis on bounds:\n");

            Console.Write("For constraints:\n");
            for (int i = 0; i < 2; ++i)
            {
                Console.Write(
                    "leftprice = {0}, rightprice = {1},leftrange = {2}, rightrange ={3}\n",
                    leftpricei[i], rightpricei[i], leftrangei[i], rightrangei[i]);
            }

            Console.Write("For variables:\n");
            for (int i = 0; i < 2; ++i)
            {
                Console.Write(
                    "leftprice = {0}, rightprice = {1},leftrange = {2}, rightrange ={3}\n",
                    leftpricej[i], rightpricej[i], leftrangej[i], rightrangej[i]);
            }


            double[] leftprice  = new  double[2];
            double[] rightprice = new  double[2];
            double[] leftrange  = new  double[2];
            double[] rightrange = new  double[2];
            int[]    subc       = new int[] { 2, 5 };

            task.dualsensitivity(subc,
                                 leftprice,
                                 rightprice,
                                 leftrange,
                                 rightrange
                                 );

            Console.Write("Results from sensitivity analysis on objective coefficients:");

            for (int i = 0; i < 2; ++i)
            {
                Console.Write(
                    "leftprice = {0}, rightprice = {1},leftrange = {2}, rightrange = {3}\n",
                    leftprice[i], rightprice[i], leftrange[i], rightrange[i]);
            }
        }
        catch (mosek.Exception e)
        {
            Console.WriteLine(e.Code);
            Console.WriteLine(e);
        }

        if (task != null)
        {
            task.Dispose();
        }
        if (env != null)
        {
            env.Dispose();
        }
    }
コード例 #14
0
ファイル: milo1.cs プロジェクト: smahdie1/active-delays
    public static void Main()
    {
        const int NUMCON = 2;
        const int NUMVAR = 2;
        const int NUMANZ = 4;

        // Since the value infinity is never used, we define
        // 'infinity' symbolic purposes only
        double infinity = 0;


        mosek.boundkey[] bkc = { mosek.boundkey.up,
                                 mosek.boundkey.lo };
        double[]         blc = { -infinity,
                                 -4.0 };
        double[]         buc = { 250.0,
                                 infinity };

        mosek.boundkey[] bkx = { mosek.boundkey.lo,
                                 mosek.boundkey.lo };
        double[]         blx = { 0.0,
                                 0.0 };
        double[]         bux = { infinity,
                                 infinity };

        double[]   c    = { 1.0, 0.64 };
        int[][]    asub = { new int[]  { 0, 1 }, new int[] { 0, 1 } };
        double[][] aval = { new double[] { 50.0, 3.0 }, new double[] { 31.0, -2.0 } };

        double[] xx = new double[NUMVAR];

        mosek.Env  env  = null;
        mosek.Task task = null;

        try
        {
            // Make mosek environment.
            env = new mosek.Env();
            // Direct the env log stream to the user specified
            // method env_msg_obj.streamCB
            MsgClass env_msg_obj = new MsgClass();
            env.set_Stream(mosek.streamtype.log, env_msg_obj);
            // Initialize the environment.
            env.init();
            // Create a task object linked with the environment env.
            task = new mosek.Task(env, NUMCON, NUMVAR);
            // Directs the log task stream to the user specified
            // method task_msg_obj.streamCB
            MsgClass task_msg_obj = new MsgClass();
            task.set_Stream(mosek.streamtype.log, task_msg_obj);

            /* Give MOSEK an estimate of the size of the input data.
             *   This is done to increase the speed of inputting data.
             *   However, it is optional. */
            task.putmaxnumvar(NUMVAR);
            task.putmaxnumcon(NUMCON);
            task.putmaxnumanz(NUMANZ);

            /* Append 'NUMCON' empty constraints.
             *   The constraints will initially have no bounds. */
            task.append(mosek.accmode.con, NUMCON);

            /* Append 'NUMVAR' variables.
             *   The variables will initially be fixed at zero (x=0). */
            task.append(mosek.accmode.var, NUMVAR);

            /* Optionally add a constant term to the objective. */
            task.putcfix(0.0);

            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.putbound(mosek.accmode.var, j, bkx[j], blx[j], bux[j]);
                /* Input column j of A */
                task.putavec(mosek.accmode.var, /* Input columns of A.*/
                             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.
             *     for i=1, ...,NUMCON : blc[i] <= constraint i <= buc[i] */
            for (int i = 0; i < NUMCON; ++i)
            {
                task.putbound(mosek.accmode.con, i, bkc[i], blc[i], buc[i]);
            }

            /* Specify integer variables. */
            for (int j = 0; j < NUMVAR; ++j)
            {
                task.putvartype(j, mosek.variabletype.type_int);
            }
            task.putobjsense(mosek.objsense.maximize);

            task.optimize();

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

            mosek.solsta solsta;
            mosek.prosta prosta;
            /* Get status information about the solution */
            task.getsolutionstatus(mosek.soltype.itg,
                                   out prosta,
                                   out solsta);
            task.getsolutionslice(mosek.soltype.itg, // Integer solution.
                                  mosek.solitem.xx,  // Which part of solution.
                                  0,                 // Index of first variable.
                                  NUMVAR,            // Index of last variable+1
                                  xx);

            switch (solsta)
            {
            case mosek.solsta.optimal:
            case mosek.solsta.near_optimal:
                Console.WriteLine("Optimal primal solution\n");
                for (int j = 0; j < NUMVAR; ++j)
                {
                    Console.WriteLine("x[{0}]:", xx[j]);
                }
                break;

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

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

            default:
                Console.WriteLine("Other solution status");
                break;
            }
        }
        catch (mosek.Exception e)
        {
            Console.WriteLine(e.Code);
            Console.WriteLine(e);
        }
        finally
        {
            if (task != null)
            {
                task.Dispose();
            }
            if (env != null)
            {
                env.Dispose();
            }
        }
    }
コード例 #15
0
ファイル: production.cs プロジェクト: edljk/Mosek.jl
    public static void Main ()
    {
        // Since the value infinity is never used, we define
        // 'infinity' symbolic purposes only
        double  
          infinity = 0;

        const int numcon = 3;
        const int numvar = 3;
  
        double[] c            = {1.5,
                                 2.5,
                                 3.0};
        mosek.boundkey[] bkc  = {mosek.boundkey.up,
                                 mosek.boundkey.up,
                                 mosek.boundkey.up};
        double[] blc          = {-infinity,
                                 -infinity,
                                 -infinity};
        double[] buc          =  {100000,
                                  50000,
                                  60000};
        mosek.boundkey[] bkx  = {mosek.boundkey.lo,
                                  mosek.boundkey.lo,
                                  mosek.boundkey.lo};
        double[] blx           = {0.0,
                                  0.0,
                                  0.0};
        double[] bux           = {+infinity,
                                  +infinity,
                                  +infinity};
        
        int[][] asub = new int[numvar][];
        asub[0] = new int[] {0, 1, 2};
        asub[1] = new int[] {0, 1, 2};
        asub[2] = new int[] {0, 1, 2};

        double[][] aval   = new double[numvar][];
        aval[0] = new double[] { 2.0, 3.0, 2.0 };
        aval[1] = new double[] { 4.0, 2.0, 3.0 };
        aval[2] = new double[] { 3.0, 3.0, 2.0 };

        double[] xx  = new double[numvar];
        
        mosek.Task task = null;
        mosek.Env  env  = null;
        
        try
          {
              // Create mosek environment. 
              env  = new mosek.Env ();
              // Create a task object linked with the environment env.
              task = new mosek.Task (env, numcon,numvar);
        
              /* Append the constraints. */
              task.appendcons(numcon);

              /* Append the variables. */
              task.appendvars(numvar);

              /* Put C. */
              task.putcfix(0.0);
              for(int j=0; j<numvar; ++j)
                  task.putcj(j,c[j]);

              /* Put constraint bounds. */
              for(int i=0; i<numcon; ++i)
                  task.putbound(mosek.accmode.con,i,bkc[i],blc[i],buc[i]);

              /* Put variable bounds. */
              for(int j=0; j<numvar; ++j)
                  task.putbound(mosek.accmode.var,j,bkx[j],blx[j],bux[j]);

              /* Put A. */
              if ( numcon>0 )
                  {
                      for(int j=0; j<numvar; ++j)
                          task.putacol(j,
                                       asub[j],
                                       aval[j]);
                  }

              task.putobjsense(mosek.objsense.maximize);

              try
                  {       
                      task.optimize();
                  }
              catch (mosek.Warning w)
                  {
                      Console.WriteLine("Mosek warning:");
                      Console.WriteLine (w.Code);
                      Console.WriteLine (w);
                  }
            
              task.getxx(mosek.soltype.bas, // Request the basic solution.
                         xx);

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

              /* Make a change to the A matrix */
              task.putaij(0, 0, 3.0);
              task.optimize();

              /* Get index of new variable. */
              int varidx;
              task.getnumvar(out varidx);

              /* Append a new varaible x_3 to the problem */
              task.appendvars(1);
    
              /* Set bounds on new varaible */
              task.putbound(mosek.accmode.var,
                            varidx,
                            mosek.boundkey.lo,
                            0,       
                            +infinity);
    
              /* Change objective */
              task.putcj(varidx,1.0);
    
              /* Put new values in the A matrix */
              int[] acolsub    =  new int[] {0,   2};
              double[] acolval =  new double[] {4.0, 1.0};
      
              task.putacol(varidx, /* column index */
                           acolsub,
                           acolval);
              /* Change optimizer to simplex free and reoptimize */
              task.putintparam(mosek.iparam.optimizer,mosek.optimizertype.free_simplex);
              task.optimize(); 
              /* Get index of new constraint */              
              int conidx; 
              task.getnumcon(out conidx);

              /* Append a new constraint */
              task.appendcons(1);

              /* Set bounds on new constraint */
              task.putbound(
                            mosek.accmode.con,
                            conidx,
                            mosek.boundkey.up,
                            -infinity,
                            30000);

              /* Put new values in the A matrix */

              int[] arowsub = new int[] {0,   1,   2,   3  };
              double[] arowval = new double[]  {1.0, 2.0, 1.0, 1.0};
      
              task.putarow(conidx, /* row index */
                           arowsub,
                           arowval); 
 
              task.optimize();
          }
        catch (mosek.Exception e)
            {
                Console.WriteLine (e.Code);
                Console.WriteLine (e);
            }

        if (task != null) task.Dispose ();
        if (env  != null)  env.Dispose ();
    }
コード例 #16
0
ファイル: mioinitsol.cs プロジェクト: edljk/Mosek.jl
    public static void Main ()
    {
        mosek.Env
            env = null;
        mosek.Task
            task = null;
        // Since the value infinity is never used, we define
        // 'infinity' symbolic purposes only
        double
            infinity = 0;

        int numvar = 4;
        int numcon = 1;
        int NUMINTVAR = 3;

        double[] c = { 7.0, 10.0, 1.0, 5.0 };
        
        mosek.boundkey[] bkc = {mosek.boundkey.up};
        double[] blc = {-infinity};
        double[] buc = {2.5};
        mosek.boundkey[] bkx = {mosek.boundkey.lo,
                                mosek.boundkey.lo,
                                mosek.boundkey.lo,
                                mosek.boundkey.lo};
        double[] blx = {0.0,
                        0.0,
                        0.0,
                        0.0};
        double[] bux = {infinity,
                        infinity,
                        infinity,
                        infinity};
        
        int[] ptrb = {0, 1, 2, 3};
        int[]  ptre = {1, 2, 3, 4};
        double[]  aval = {1.0, 1.0, 1.0, 1.0};
        int[] asub = {0,   0,   0,   0  };
        int[] intsub = {0, 1, 2};  
        double[] xx  = new double[numvar];

        try
          {
            // Make mosek environment. 
            env  = new mosek.Env ();
            // Create a task object linked with the environment env.
            task = new mosek.Task (env, numcon,numvar);
            // Directs the log task stream to the user specified
            // method task_msg_obj.streamCB
            task.set_Stream (mosek.streamtype.log, new msgclass ("[task]"));
            task.inputdata(numcon,numvar,
                           c,
                           0.0,
                           ptrb,
                           ptre,
                           asub,
                           aval,
                           bkc,
                           blc,
                           buc,
                           bkx,
                           blx,
                           bux);

            for(int j=0 ; j<NUMINTVAR ; ++j)            
              task.putvartype(intsub[j],mosek.variabletype.type_int);
            task.putobjsense(mosek.objsense.maximize);

            // Construct an initial feasible solution from the
            //     values of the integer valuse specified 
            task.putintparam(mosek.iparam.mio_construct_sol,
                             mosek.onoffkey.on);
        
            // Set status of all variables to unknown 
            //task.makesolutionstatusunknown(mosek.soltype.itg);
        
            // Assign values 0,2,0 to integer variables. Important to 
            // assign a value to all integer constrained variables.
            double[] values={0.0, 2.0, 0.0};
            task.putxxslice(mosek.soltype.itg, 0, 3, values);
            
            try
              {
                task.optimize();
              }
            catch (mosek.Warning w)
              {
                Console.WriteLine("Mosek warning:");
                Console.WriteLine (w.Code);
                Console.WriteLine (w);
              }
            task.getsolutionslice(mosek.soltype.itg, /* Basic solution.       */
                                  mosek.solitem.xx,  /* Which part of solution.  */
                                  0,                 /* Index of first variable. */
                                  numvar,            /* Index of last variable+1 */
                                  xx);

            for(int j = 0; j < numvar; ++j)
              Console.WriteLine ("x[{0}]:{1}", j,xx[j]);
          }
        catch (mosek.Exception e)
            {
                Console.WriteLine (e.Code);
                Console.WriteLine (e);
                throw;
            }

        if (task != null) task.Dispose ();
        if (env  != null)  env.Dispose ();
    }
コード例 #17
0
    public static void Main()
    {
        const double inf = 0.0; /* We don't actually need any value for infinity */

        const int NUMCON = 1;   /* Number of constraints.             */
        const int NUMVAR = 3;   /* Number of variables.               */
        const int NUMANZ = 3;   /* Number of numzeros in A.           */
        const int NUMQNZ = 4;   /* Number of nonzeros in Q.           */

        mosek.boundkey[]
        bkc = { mosek.boundkey.lo },
        bkx = { mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo };
        int[][]    asub = { new int[] { 0 }, new int[] { 0 }, new int[] { 0 } };
        double[][] aval = { new double[] { 1.0 }, new double[] { 1.0 }, new double[] { 1.0 } };

        double[]
        blc = { 1.0 },
        buc = { inf },
        c   = { 0.0, -1.0, 0.0 },
        blx = { 0.0, 0.0, 0.0 },
        bux = { inf, inf, inf },
        xx  = new double[NUMVAR];
        mosek.Task
            task = null;
        mosek.Env
            env = null;

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

            /* Give MOSEK an estimate of the size of the input data.
             *   This is done to increase the speed of inputting data.
             *   However, it is optional. */
            task.putmaxnumvar(NUMVAR);
            task.putmaxnumcon(NUMCON);
            task.putmaxnumanz(NUMANZ);

            /* Append 'NUMCON' empty constraints.
             *   The constraints will initially have no bounds. */
            task.append(mosek.accmode.con, NUMCON);

            /* Append 'NUMVAR' variables.
             *   The variables will initially be fixed at zero (x=0). */
            task.append(mosek.accmode.var, NUMVAR);

            /* Optionally add a constant term to the objective. */
            task.putcfix(0.0);

            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.putbound(mosek.accmode.var, j, bkx[j], blx[j], bux[j]);
                /* Input column j of A */
                task.putavec(mosek.accmode.var, /* Input columns of A.*/
                             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.
             *     for i=1, ...,NUMCON : blc[i] <= constraint i <= buc[i] */
            for (int i = 0; i < NUMCON; ++i)
            {
                task.putbound(mosek.accmode.con, i, bkc[i], blc[i], buc[i]);
            }

            /*
             * The lower triangular part of the Q
             * matrix in the objective is specified.
             */

            {
                int[]
                qsubi = { 0, 1, 2, 2 },
                qsubj = { 0, 1, 0, 2 };
                double[]
                qval = { 2.0, 0.2, -1.0, 2.0 };

                /* Input the Q for the objective. */

                task.putqobj(qsubi, qsubj, qval);
            }

            /*
             * The lower triangular part of the Q^0
             * matrix in the first constraint is specified.
             * This corresponds to adding the term
             *  - x0^2 - x1^2 - 0.1 x2^2 + 0.2 x0 x2
             */
            {
                int[]
                qsubi = { 0, 1, 2, 2 },
                qsubj = { 0, 1, 2, 0 };
                double[]
                qval = { -2.0, -2.0, -0.2, 0.2 };

                /* put Q^0 in constraint with index 0. */

                task.putqconk(0,
                              qsubi,
                              qsubj,
                              qval);
            }

            task.putobjsense(mosek.objsense.minimize);

            task.optimize();

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

            mosek.solsta solsta;
            mosek.prosta prosta;
            /* Get status information about the solution */
            task.getsolutionstatus(mosek.soltype.itr,
                                   out prosta,
                                   out solsta);
            task.getsolutionslice(mosek.soltype.itr, // Basic solution.
                                  mosek.solitem.xx,  // Which part of solution.
                                  0,                 // Index of first variable.
                                  NUMVAR,            // Index of last variable+1
                                  xx);

            switch (solsta)
            {
            case mosek.solsta.optimal:
            case mosek.solsta.near_optimal:
                Console.WriteLine("Optimal primal solution\n");
                for (int j = 0; j < NUMVAR; ++j)
                {
                    Console.WriteLine("x[{0}]:", xx[j]);
                }
                break;

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

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

            default:
                Console.WriteLine("Other solution status");
                break;
            }
        }
        catch (mosek.Exception e)
        {
            Console.WriteLine(e);
        }
        finally
        {
            if (task != null)
            {
                task.Dispose();
            }
            if (env != null)
            {
                env.Dispose();
            }
        }
    } /* Main */
コード例 #18
0
        public static void Main()
        {
            // Since the value infinity is never used, we define
            // 'infinity' symbolic purposes only
            double
                infinity = 0;

            int numcon = 3;
            int numvar = 3;

            double[]         c = { 1.5,
                                   2.5,
                                   3.0 };
            mosek.boundkey[] bkc = { mosek.boundkey.up,
                                     mosek.boundkey.up,
                                     mosek.boundkey.up };
            double[]         blc = { -infinity,
                                     -infinity,
                                     -infinity };
            double[]         buc = { 100000,
                                     50000,
                                     60000 };
            mosek.boundkey[] bkx = { mosek.boundkey.lo,
                                     mosek.boundkey.lo,
                                     mosek.boundkey.lo };
            double[]         blx = { 0.0,
                                     0.0,
                                     0.0 };
            double[]         bux = { +infinity,
                                     +infinity,
                                     +infinity };

            int[][] asub = new int[numvar][];
            asub[0] = new int[] { 0, 1, 2 };
            asub[1] = new int[] { 0, 1, 2 };
            asub[2] = new int[] { 0, 1, 2 };

            double[][] aval = new double[numvar][];
            aval[0] = new double[] { 2.0, 3.0, 2.0 };
            aval[1] = new double[] { 4.0, 2.0, 3.0 };
            aval[2] = new double[] { 3.0, 3.0, 2.0 };

            double[] xx = new double[numvar];

            mosek.Task task = null;
            mosek.Env  env  = null;

            try
            {
                // Create mosek environment.
                env = new mosek.Env();
                // Create a task object linked with the environment env.
                task = new mosek.Task(env, numcon, numvar);

                /* Append the constraints. */
                task.appendcons(numcon);

                /* Append the variables. */
                task.appendvars(numvar);

                /* Put C. */
                task.putcfix(0.0);
                for (int j = 0; j < numvar; ++j)
                {
                    task.putcj(j, c[j]);
                }

                /* Put constraint bounds. */
                for (int i = 0; i < numcon; ++i)
                {
                    task.putconbound(i, bkc[i], blc[i], buc[i]);
                }

                /* Put variable bounds. */
                for (int j = 0; j < numvar; ++j)
                {
                    task.putvarbound(j, bkx[j], blx[j], bux[j]);
                }

                /* Put A. */
                if (numcon > 0)
                {
                    for (int j = 0; j < numvar; ++j)
                    {
                        task.putacol(j,
                                     asub[j],
                                     aval[j]);
                    }
                }

                task.putobjsense(mosek.objsense.maximize);

                try
                {
                    task.optimize();
                }
                catch (mosek.Warning w)
                {
                    Console.WriteLine("Mosek warning:");
                    Console.WriteLine(w.Code);
                    Console.WriteLine(w);
                }

                task.getxx(mosek.soltype.bas, // Request the basic solution.
                           xx);

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

                /********************** Make a change to the A matrix ********************/
                task.putaij(0, 0, 3.0);
                task.optimize();
                task.getxx(mosek.soltype.bas, // Request the basic solution.
                           xx);

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

                /********************** Add a new variable ********************/
                /* Get index of new variable. */
                int varidx;
                task.getnumvar(out varidx);

                /* Append a new varaible x_3 to the problem */
                task.appendvars(1);
                numvar++;

                /* Set bounds on new varaible */
                task.putvarbound(varidx,
                                 mosek.boundkey.lo,
                                 0,
                                 +infinity);

                /* Change objective */
                task.putcj(varidx, 1.0);

                /* Put new values in the A matrix */
                int[]    acolsub = new int[] { 0, 2 };
                double[] acolval = new double[] { 4.0, 1.0 };

                task.putacol(varidx, /* column index */
                             acolsub,
                             acolval);
                /* Change optimizer to simplex free and reoptimize */
                task.putintparam(mosek.iparam.optimizer, mosek.optimizertype.free_simplex);
                task.optimize();

                xx = new double[numvar];
                task.getxx(mosek.soltype.bas, // Request the basic solution.
                           xx);

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


                /********************** Add a new constraint ********************/
                /* Get index of new constraint */
                int conidx;
                task.getnumcon(out conidx);

                /* Append a new constraint */
                task.appendcons(1);
                numcon++;

                /* Set bounds on new constraint */
                task.putconbound(conidx,
                                 mosek.boundkey.up,
                                 -infinity,
                                 30000);

                /* Put new values in the A matrix */
                int[]    arowsub = new int[] { 0, 1, 2, 3 };
                double[] arowval = new double[]  { 1.0, 2.0, 1.0, 1.0 };

                task.putarow(conidx, /* row index */
                             arowsub,
                             arowval);

                task.optimize();

                task.getxx(mosek.soltype.bas, // Request the basic solution.
                           xx);

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


                /********************** Change constraint bounds ********************/
                mosek.boundkey[] newbkc = { mosek.boundkey.up,
                                            mosek.boundkey.up,
                                            mosek.boundkey.up,
                                            mosek.boundkey.up };
                double[]         newblc = { -infinity,
                                            -infinity,
                                            -infinity,
                                            -infinity };
                double[]         newbuc = { 80000, 40000, 50000, 22000 };

                task.putconboundslice(0, numcon, newbkc, newblc, newbuc);

                task.optimize();

                task.getxx(mosek.soltype.bas, // Request the basic solution.
                           xx);

                for (int j = 0; j < numvar; ++j)
                {
                    Console.WriteLine("x[{0}]:{1}", j, xx[j]);
                }
            }
            catch (mosek.Exception e)
            {
                Console.WriteLine(e.Code);
                Console.WriteLine(e);
            }

            if (task != null)
            {
                task.Dispose();
            }
            if (env != null)
            {
                env.Dispose();
            }
        }
コード例 #19
0
ファイル: production.cs プロジェクト: smahdie1/active-delays
    public static void Main()
    {
        // Since the value infinity is never used, we define
        // 'infinity' symbolic purposes only
        double
            infinity = 0;

        const int NUMCON = 3;
        const int NUMVAR = 3;
        const int NUMANZ = 9;

        double[]         c = { 1.5,
                               2.5,
                               3.0 };
        mosek.boundkey[] bkc = { mosek.boundkey.up,
                                 mosek.boundkey.up,
                                 mosek.boundkey.up };
        double[]         blc = { -infinity,
                                 -infinity,
                                 -infinity };
        double[]         buc = { 100000,
                                 50000,
                                 60000 };
        mosek.boundkey[] bkx = { mosek.boundkey.lo,
                                 mosek.boundkey.lo,
                                 mosek.boundkey.lo };
        double[]         blx = { 0.0,
                                 0.0,
                                 0.0 };
        double[]         bux = { +infinity,
                                 +infinity,
                                 +infinity };

        int[][] asub = new int[NUMVAR][];
        asub[0] = new int[] { 0, 1, 2 };
        asub[1] = new int[] { 0, 1, 2 };
        asub[2] = new int[] { 0, 1, 2 };

        double[][] aval = new double[NUMVAR][];
        aval[0] = new double[] { 2.0, 3.0, 2.0 };
        aval[1] = new double[] { 4.0, 2.0, 3.0 };
        aval[2] = new double[] { 3.0, 3.0, 2.0 };

        double[] xx = new double[NUMVAR];

        mosek.Task task = null;
        mosek.Env  env  = null;

        try
        {
            // Create mosek environment.
            env = new mosek.Env();
            // Initialize the environment.
            env.init();
            // Create a task object linked with the environment env.
            task = new mosek.Task(env, NUMCON, NUMVAR);


            /* Give MOSEK an estimate on the size of
             * the data to input. This is done to increase
             * the speed of inputting data and is optional.*/

            task.putmaxnumvar(NUMVAR);
            task.putmaxnumcon(NUMCON);
            task.putmaxnumanz(NUMANZ);

            /* Append the constraints. */
            task.append(mosek.accmode.con, NUMCON);

            /* Append the variables. */
            task.append(mosek.accmode.var, NUMVAR);

            /* Put C. */
            task.putcfix(0.0);
            for (int j = 0; j < NUMVAR; ++j)
            {
                task.putcj(j, c[j]);
            }

            /* Put constraint bounds. */
            for (int i = 0; i < NUMCON; ++i)
            {
                task.putbound(mosek.accmode.con, i, bkc[i], blc[i], buc[i]);
            }

            /* Put variable bounds. */
            for (int j = 0; j < NUMVAR; ++j)
            {
                task.putbound(mosek.accmode.var, j, bkx[j], blx[j], bux[j]);
            }

            /* Put A. */
            if (NUMCON > 0)
            {
                for (int j = 0; j < NUMVAR; ++j)
                {
                    task.putavec(mosek.accmode.var,
                                 j,
                                 asub[j],
                                 aval[j]);
                }
            }

            task.putobjsense(mosek.objsense.maximize);

            try
            {
                task.optimize();
            }
            catch (mosek.Warning w)
            {
                Console.WriteLine("Mosek warning:");
                Console.WriteLine(w.Code);
                Console.WriteLine(w);
            }

            task.getsolutionslice(mosek.soltype.bas,   /* Basic solution.       */
                                  mosek.solitem.xx,    /* Which part of solution.  */
                                  0,                   /* Index of first variable. */
                                  NUMVAR,              /* Index of last variable+1 */
                                  xx);

            for (int j = 0; j < NUMVAR; ++j)
            {
                Console.WriteLine("x[{0}]:{1}", j, xx[j]);
            }

            /* Make a change to the A matrix */
            task.putaij(0, 0, 3.0);
            task.optimize();
            /* Append a new varaible x_3 to the problem */
            task.append(mosek.accmode.var, 1);

            /* Get index of new variable, this should be 3 */
            int numvar;
            task.getnumvar(out numvar);

            /* Set bounds on new varaible */
            task.putbound(mosek.accmode.var,
                          numvar - 1,
                          mosek.boundkey.lo,
                          0,
                          +infinity);

            /* Change objective */
            task.putcj(numvar - 1, 1.0);

            /* Put new values in the A matrix */
            int[]    acolsub = new int[] { 0, 2 };
            double[] acolval = new double[] { 4.0, 1.0 };

            task.putavec(mosek.accmode.var,
                         numvar - 1, /* column index */
                         acolsub,
                         acolval);
            /* Change optimizer to simplex free and reoptimize */
            task.putintparam(mosek.iparam.optimizer, mosek.optimizertype.free_simplex);
            task.optimize();
            /* Append a new constraint */
            task.append(mosek.accmode.con, 1);

            /* Get index of new constraint, this should be 4 */
            int numcon;
            task.getnumcon(out numcon);

            /* Set bounds on new constraint */
            task.putbound(
                mosek.accmode.con,
                numcon - 1,
                mosek.boundkey.up,
                -infinity,
                30000);

            /* Put new values in the A matrix */

            int[]    arowsub = new int[] { 0, 1, 2, 3 };
            double[] arowval = new double[]  { 1.0, 2.0, 1.0, 1.0 };

            task.putavec(mosek.accmode.con,
                         numcon - 1, /* row index */
                         arowsub,
                         arowval);

            task.optimize();
        }
        catch (mosek.Exception e)
        {
            Console.WriteLine(e.Code);
            Console.WriteLine(e);
        }

        if (task != null)
        {
            task.Dispose();
        }
        if (env != null)
        {
            env.Dispose();
        }
    }
コード例 #20
0
    public static void Main(string[] args)
    {
        mosek.Env
            env = null;
        mosek.Task
            task = null;
        int
            numvar = 0;

        try
        {
            env = new mosek.Env();
            env.init();
            task = new mosek.Task(env, 0, 0);
            {
                double[] c = new double[numvar];
                task.getc(c);
            }
            {
                double[]         upper_bound = new double[8];
                double[]         lower_bound = new double[8];
                mosek.boundkey[] bound_key   = new mosek.boundkey[8];
                task.getboundslice(mosek.accmode.con, 2, 10,
                                   bound_key, lower_bound, upper_bound);
            }
            {
                int[]            bound_index = { 1, 6, 3, 9 };
                mosek.boundkey[] bound_key   =
                { mosek.boundkey.fr,
                  mosek.boundkey.lo,
                  mosek.boundkey.up,
                  mosek.boundkey.fx };
                double[] lower_bound = { 0.0, -10.0, 0.0, 5.0 };
                double[] upper_bound = { 0.0, 0.0, 6.0, 5.0 };
                task.putboundlist(mosek.accmode.con, bound_index,
                                  bound_key, lower_bound, upper_bound);
            }
            {
                int[]    subi = { 1, 3, 5 };
                int[]    subj = { 2, 3, 4 };
                double[] cof  = { 1.1, 4.3, 0.2 };
                task.putaijlist(subi, subj, cof);
            }


            {
                int[]    rowsub = { 0, 1, 2, 3 };
                int[]    ptrb   = { 0, 3, 5, 7 };
                int[]    ptre   = { 3, 5, 7, 8 };
                int[]    sub    = { 0, 2, 3, 1, 4, 0, 3, 2 };
                double[] cof    = { 1.1, 1.3, 1.4, 2.2, 2.5, 3.1, 3.4, 4.4 };

                task.putaveclist(mosek.accmode.con,
                                 rowsub, ptrb, ptre,
                                 sub, cof);
            }
        }
        catch (mosek.ArrayLengthException e)
        {
            Console.WriteLine("Error: An array was too short");
            Console.WriteLine(e.ToString());
        }
        catch (mosek.Exception e)
        /* Catch both mosek.Error and mosek.Warning */
        {
            Console.WriteLine("An error or warning was encountered");
            Console.WriteLine(e.ToString());
        }

        if (task != null)
        {
            task.Dispose();
        }
        if (env != null)
        {
            env.Dispose();
        }
    }
コード例 #21
0
        public void Optimize(Story story, PositionTable <double> position)
        {
            int           count = 0;
            List <double> list  = new List <double>();

            int[,] index = new int[story.Characters.Count, story.FrameCount];
            for (int i = 0; i < story.Characters.Count; ++i)
            {
                for (int frame = 0; frame < story.FrameCount; ++frame)
                {
                    if (story.SessionTable[i, frame] != -1)
                    {
                        index[i, frame] = count++;
                        list.Add(position[i, frame]);
                    }
                }
            }

            double[] X = list.ToArray <double>();

            int[,] Q = new int[X.Length, X.Length];
            for (int i = 0; i < story.Characters.Count; ++i)
            {
                for (int frame = 0; frame < story.FrameCount - 1; ++frame)
                {
                    int left  = frame;
                    int right = frame + 1;
                    if (story.SessionTable[i, left] != -1 && story.SessionTable[i, right] != -1)
                    {
                        Q[index[i, left], index[i, left]]   += 2;
                        Q[index[i, right], index[i, right]] += 2;
                        Q[index[i, left], index[i, right]]  -= 2;
                        Q[index[i, right], index[i, left]]  -= 2;
                    }
                }
            }
            List <int>    li = new List <int>();
            List <int>    lj = new List <int>();
            List <double> lv = new List <double>();

            for (int i = 0; i < X.Length; ++i)
            {
                for (int j = 0; j <= i; ++j)
                {
                    if (Q[i, j] != 0)
                    {
                        li.Add(i);
                        lj.Add(j);
                        lv.Add((double)Q[i, j]);
                    }
                }
            }
            int[]    qsubi = li.ToArray();
            int[]    qsubj = lj.ToArray();
            double[] qval  = lv.ToArray();


            List <Tuple <int, int> > listInner = new List <Tuple <int, int> >();
            List <Tuple <int, int> > listOuter = new List <Tuple <int, int> >();

            for (int frame = 0; frame < story.FrameCount; ++frame)
            {
                List <Tuple <int, double> > l = new List <Tuple <int, double> >();
                for (int i = 0; i < story.Characters.Count; ++i)
                {
                    if (story.SessionTable[i, frame] != -1)
                    {
                        l.Add(new Tuple <int, double>(i, position[i, frame]));
                    }
                }
                l.Sort((a, b) => a.Item2.CompareTo(b.Item2));
                for (int k = 0; k < l.Count - 1; ++k)
                {
                    int x = l[k].Item1;
                    int y = l[k + 1].Item1;
                    if (story.SessionTable[x, frame] == story.SessionTable[y, frame])
                    {
                        listInner.Add(new Tuple <int, int>(index[x, frame], index[y, frame]));
                    }
                    else
                    {
                        listOuter.Add(new Tuple <int, int>(index[x, frame], index[y, frame]));
                    }
                }
            }


            const double infinity      = 0;
            int          NumConstraint = listInner.Count + listOuter.Count;
            int          NumVariable   = X.Length;


            double[]         blx = new double[NumVariable];
            double[]         bux = new double[NumVariable];
            mosek.boundkey[] bkx = new mosek.boundkey[NumVariable];

            for (int i = 0; i < NumVariable; ++i)
            {
                bkx[i] = mosek.boundkey.fr;
            }


            int[][]    asub = new int[NumVariable][];
            double[][] aval = new double[NumVariable][];


            List <int>[]    asubList = new List <int> [NumVariable];
            List <double>[] avalList = new List <double> [NumVariable];
            for (int i = 0; i < NumVariable; ++i)
            {
                asubList[i] = new List <int>();
                avalList[i] = new List <double>();
            }
            for (int i = 0; i < listInner.Count; ++i)
            {
                Tuple <int, int> pair = listInner[i];
                int x = pair.Item1;
                int y = pair.Item2;

                asubList[x].Add(i);
                avalList[x].Add(-1);

                asubList[y].Add(i);
                avalList[y].Add(1);
            }
            for (int i = 0; i < listOuter.Count; ++i)
            {
                int j = i + listInner.Count;
                Tuple <int, int> pair = listOuter[i];
                int x = pair.Item1;
                int y = pair.Item2;
                asubList[x].Add(j);
                avalList[x].Add(-1);

                asubList[y].Add(j);
                avalList[y].Add(1);
            }
            for (int i = 0; i < NumVariable; ++i)
            {
                asub[i] = asubList[i].ToArray();
                aval[i] = avalList[i].ToArray();
            }
            mosek.boundkey[] bkc = new mosek.boundkey[NumConstraint];
            double[]         blc = new double[NumConstraint];
            double[]         buc = new double[NumConstraint];
            for (int i = 0; i < listInner.Count; ++i)
            {
                bkc[i] = mosek.boundkey.fx;
                blc[i] = 28;
                buc[i] = 28;
            }
            for (int i = listInner.Count; i < listInner.Count + listOuter.Count; ++i)
            {
                bkc[i] = mosek.boundkey.lo;
                blc[i] = 84;
                buc[i] = infinity;
            }

            mosek.Task task = null;
            mosek.Env  env  = null;
            double[]   xx   = new double[NumVariable];
            try
            {
                env = new mosek.Env();
                env.init();

                task = new mosek.Task(env, 0, 0);
                task.putmaxnumvar(NumVariable);
                task.putmaxnumcon(NumConstraint);

                task.append(mosek.accmode.con, NumConstraint);
                task.append(mosek.accmode.var, NumVariable);

                task.putcfix(0.0);

                for (int j = 0; j < NumVariable; ++j)
                {
                    task.putcj(j, 0);
                    task.putbound(mosek.accmode.var, j, bkx[j], blx[j], bux[j]);

                    task.putavec(mosek.accmode.var, j, asub[j], aval[j]);
                }

                for (int i = 0; i < NumConstraint; ++i)
                {
                    task.putbound(mosek.accmode.con, i, bkc[i], blc[i], buc[i]);
                }

                task.putobjsense(mosek.objsense.minimize);
                task.putqobj(qsubi, qsubj, qval);

                task.optimize();

                mosek.solsta solsta;
                mosek.prosta prosta;

                task.getsolutionstatus(mosek.soltype.itr,
                                       out prosta,
                                       out solsta);
                task.getsolutionslice(mosek.soltype.itr,
                                      mosek.solitem.xx,
                                      0,
                                      NumVariable,
                                      xx);
            }
            catch (mosek.Exception e)
            {
                Console.WriteLine(e.Code);
                Console.WriteLine(e);
            }
            finally
            {
                if (task != null)
                {
                    task.Dispose();
                }
                if (env != null)
                {
                    env.Dispose();
                }
            }


            for (int i = 0; i < story.Characters.Count; ++i)
            {
                for (int frame = 0; frame < story.FrameCount; ++frame)
                {
                    if (story.SessionTable[i, frame] != -1)
                    {
                        position[i, frame] = xx[index[i, frame]];
                    }
                }
            }
        }
コード例 #22
0
        public void Optimize(Story story, PositionTable <double> position, PositionTable <int> segment)
        {
            int           count = -1;
            List <double> X     = new List <double>();

            int[,] index = new int[story.Characters.Count, story.FrameCount];
            for (int i = 0; i < story.Characters.Count; ++i)
            {
                for (int frame = 0; frame < story.FrameCount; ++frame)
                {
                    index[i, frame] = -2;
                }
            }
            for (int i = 0; i < story.Characters.Count; ++i)
            {
                for (int frame = 0; frame < story.FrameCount; ++frame)
                {
                    if (story.SessionTable[i, frame] != -1)
                    {
                        if (frame > 0 && story.SessionTable[i, frame - 1] != -1 && segment[i, frame] == segment[i, frame - 1])
                        {
                            index[i, frame] = count;
                        }
                        else
                        {
                            index[i, frame] = ++count;
                            X.Add(position[i, frame]);
                        }
                    }
                }
            }

            Dictionary <Tuple <int, int>, double> Q = new Dictionary <Tuple <int, int>, double>();

            for (int i = 0; i < X.Count; ++i)
            {
                Q.Add(new Tuple <int, int>(i, i), 1);
            }

            //int[,] Q = new int[X.Count, X.Count];
            for (int i = 0; i < story.Characters.Count; ++i)
            {
                for (int frame = 0; frame < story.FrameCount - 1; ++frame)
                {
                    int left  = frame;
                    int right = frame + 1;
                    if (story.SessionTable[i, left] != -1 && story.SessionTable[i, right] != -1 && segment[i, left] != segment[i, right])
                    {
                        if (!Q.ContainsKey(new Tuple <int, int>(index[i, left], index[i, left])))
                        {
                            Q.Add(new Tuple <int, int>(index[i, left], index[i, left]), 0);
                        }
                        if (!Q.ContainsKey(new Tuple <int, int>(index[i, right], index[i, right])))
                        {
                            Q.Add(new Tuple <int, int>(index[i, right], index[i, right]), 0);
                        }
                        if (!Q.ContainsKey(new Tuple <int, int>(index[i, left], index[i, right])))
                        {
                            Q.Add(new Tuple <int, int>(index[i, left], index[i, right]), 0);
                        }
                        if (!Q.ContainsKey(new Tuple <int, int>(index[i, right], index[i, left])))
                        {
                            Q.Add(new Tuple <int, int>(index[i, right], index[i, left]), 0);
                        }
                        Q[new Tuple <int, int>(index[i, left], index[i, left])]   += 2;
                        Q[new Tuple <int, int>(index[i, right], index[i, right])] += 2;
                        Q[new Tuple <int, int>(index[i, left], index[i, right])]  -= 2;
                        Q[new Tuple <int, int>(index[i, right], index[i, left])]  -= 2;
                    }
                }
            }

            List <int>    li = new List <int>();
            List <int>    lj = new List <int>();
            List <double> lv = new List <double>();

            foreach (KeyValuePair <Tuple <int, int>, double> pair in Q)
            {
                if (pair.Key.Item1 >= pair.Key.Item2 && pair.Value != 0)
                {
                    li.Add(pair.Key.Item1);
                    lj.Add(pair.Key.Item2);
                    lv.Add((double)pair.Value);
                }
            }

            int[]    qsubi = li.ToArray();
            int[]    qsubj = lj.ToArray();
            double[] qval  = lv.ToArray();


            List <Tuple <int, int> > listInner = new List <Tuple <int, int> >();
            List <Tuple <int, int> > listOuter = new List <Tuple <int, int> >();

            for (int frame = 0; frame < story.FrameCount; ++frame)
            {
                List <Tuple <int, double> > l = new List <Tuple <int, double> >();
                for (int i = 0; i < story.Characters.Count; ++i)
                {
                    if (story.SessionTable[i, frame] != -1)
                    {
                        l.Add(new Tuple <int, double>(i, position[i, frame]));
                    }
                }
                l.Sort((a, b) => a.Item2.CompareTo(b.Item2));
                for (int k = 0; k < l.Count - 1; ++k)
                {
                    int x = l[k].Item1;
                    int y = l[k + 1].Item1;
                    if (story.SessionTable[x, frame] == story.SessionTable[y, frame])
                    {
                        if (!listInner.Contains(new Tuple <int, int>(index[x, frame], index[y, frame])))
                        {
                            listInner.Add(new Tuple <int, int>(index[x, frame], index[y, frame]));
                        }
                    }
                    else
                    {
                        if (!listOuter.Contains(new Tuple <int, int>(index[x, frame], index[y, frame])))
                        {
                            listOuter.Add(new Tuple <int, int>(index[x, frame], index[y, frame]));
                        }
                    }
                }
            }

            Debug.Assert(!ExistingCircle(listInner, X.Count));
            Debug.Assert(!ExistingCircle(listOuter, X.Count));

            foreach (Tuple <int, int> tuple in listInner)
            {
                Debug.Write(tuple.Item1.ToString() + "->" + tuple.Item2.ToString() + ", ");
            }

            const double infinity      = 0;
            int          NumConstraint = listInner.Count + listOuter.Count;
            int          NumVariable   = X.Count;


            double[]         blx = new double[NumVariable];
            double[]         bux = new double[NumVariable];
            mosek.boundkey[] bkx = new mosek.boundkey[NumVariable];

            for (int i = 0; i < NumVariable; ++i)
            {
                bkx[i] = mosek.boundkey.ra;
                blx[i] = -12000;
                bux[i] = 12000;
            }
            bkx[0] = mosek.boundkey.fx;
            bkx[0] = 0;
            bkx[0] = 0;


            int[][]    asub = new int[NumVariable][];
            double[][] aval = new double[NumVariable][];


            List <int>[]    asubList = new List <int> [NumVariable];
            List <double>[] avalList = new List <double> [NumVariable];
            for (int i = 0; i < NumVariable; ++i)
            {
                asubList[i] = new List <int>();
                avalList[i] = new List <double>();
            }
            for (int i = 0; i < listInner.Count; ++i)
            {
                Tuple <int, int> pair = listInner[i];
                int x = pair.Item1;
                int y = pair.Item2;

                asubList[x].Add(i);
                avalList[x].Add(-1);

                asubList[y].Add(i);
                avalList[y].Add(1);
            }
            for (int i = 0; i < listOuter.Count; ++i)
            {
                int j = i + listInner.Count;
                Tuple <int, int> pair = listOuter[i];
                int x = pair.Item1;
                int y = pair.Item2;
                asubList[x].Add(j);
                avalList[x].Add(-1);

                asubList[y].Add(j);
                avalList[y].Add(1);
            }
            for (int i = 0; i < NumVariable; ++i)
            {
                asub[i] = asubList[i].ToArray();
                aval[i] = avalList[i].ToArray();
            }
            mosek.boundkey[] bkc = new mosek.boundkey[NumConstraint];
            double[]         blc = new double[NumConstraint];
            double[]         buc = new double[NumConstraint];
            for (int i = 0; i < listInner.Count; ++i)
            {
                bkc[i] = mosek.boundkey.fx;
                //blc[i] = 28;
                //buc[i] = 28;
                blc[i] = _app.Status.Config.Style.DefaultInnerGap;
                buc[i] = _app.Status.Config.Style.DefaultInnerGap;
            }
            for (int i = listInner.Count; i < listInner.Count + listOuter.Count; ++i)
            {
                bkc[i] = mosek.boundkey.lo;
                //blc[i] = 84;
                blc[i] = _app.Status.Config.Style.OuterGap;
                buc[i] = 1000;
            }

            mosek.Task task  = null;
            mosek.Env  env   = null;
            double[]   xx    = new double[NumVariable];
            DateTime   start = DateTime.Now;

            try
            {
                env = new mosek.Env();
                env.set_Stream(mosek.streamtype.log, new msgclass(""));
                env.init();

                task = new mosek.Task(env, 0, 0);
                task.set_Stream(mosek.streamtype.log, new msgclass(""));
                task.putmaxnumvar(NumVariable);
                task.putmaxnumcon(NumConstraint);

                //task.putdouparam(mosek.dparam.intpnt_nl_tol_pfeas, 1.0e-1);
                //task.putdouparam(mosek.dparam.intpnt_tol_dfeas, 1.0e-1);
                //task.putdouparam(mosek.dparam.intpnt_nl_tol_rel_gap, 1.0e-1);
                //task.putdouparam(mosek.dparam.intpnt_co_tol_infeas, 1.0e-13);
                //task.putdouparam(mosek.dparam.intpnt_nl_tol_mu_red, 1.0e-13);


                task.append(mosek.accmode.con, NumConstraint);
                task.append(mosek.accmode.var, NumVariable);

                task.putcfix(0.0);

                for (int j = 0; j < NumVariable; ++j)
                {
                    task.putcj(j, 0);
                    task.putbound(mosek.accmode.var, j, bkx[j], blx[j], bux[j]);

                    task.putavec(mosek.accmode.var, j, asub[j], aval[j]);
                }

                for (int i = 0; i < NumConstraint; ++i)
                {
                    task.putbound(mosek.accmode.con, i, bkc[i], blc[i], buc[i]);
                }

                task.putobjsense(mosek.objsense.minimize);
                task.putqobj(qsubi, qsubj, qval);

                task.optimize();
                task.solutionsummary(mosek.streamtype.msg);

                mosek.solsta solsta;
                mosek.prosta prosta;

                task.getsolutionstatus(mosek.soltype.itr,
                                       out prosta,
                                       out solsta);
                task.getsolutionslice(mosek.soltype.itr,
                                      mosek.solitem.xx,
                                      0,
                                      NumVariable,
                                      xx);

                switch (solsta)
                {
                case mosek.solsta.optimal:
                case mosek.solsta.near_optimal:
                    Console.WriteLine("Optimal primal solution\n");
                    //for (int j = 0; j < NumVariable; ++j)
                    //    Console.WriteLine("x[{0}]:", xx[j]);
                    break;

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

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

                default:
                    Console.WriteLine("Other solution status");
                    break;
                }
            }
            catch (mosek.Exception e)
            {
                Console.WriteLine(e.Code);
                Console.WriteLine(e);
            }
            finally
            {
                if (task != null)
                {
                    task.Dispose();
                }
                if (env != null)
                {
                    env.Dispose();
                }
            }

            Console.WriteLine("///{0}", DateTime.Now - start);

            for (int i = 0; i < story.Characters.Count; ++i)
            {
                for (int frame = 0; frame < story.FrameCount; ++frame)
                {
                    if (story.SessionTable[i, frame] != -1)
                    {
                        position[i, frame] = xx[index[i, frame]];
                    }
                }
            }
        }
コード例 #23
0
    public static void Main()
    {
        const int n = 3, m = 2, k = 2;

        double alpha = 2.0, beta = 0.5;

        double[] x = { 1.0, 1.0, 1.0 };
        double[] y = { 1.0, 2.0, 3.0 };
        double[] z = { 1.0, 1.0 };

        /*A has m=2 rows and k=3 cols*/
        double[] A = { 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 };
        /*B has k=3 rows and n=3 cols*/
        double[] B = { 1.0, 1.0, 1.0,
                       1.0, 1.0, 1.0,
                       1.0, 1.0, 1.0 };
        double[] C = { 1.0, 2.0, 3.0,
                       4.0, 5.0, 6.0 };

        double[] D = { 1.0, 1.0, 1.0, 1.0 };
        double[] Q = { 1.0, 0.0, 0.0, 2.0 };
        double[] v = new double[2];

        double xy;


        using (mosek.Env env = new mosek.Env())
        {
            /* BLAS routines */

            try
            {
                /*TAG:begin-dot*/
                env.dot(n, x, y, out xy);
                /*TAG:end-dot*/

                /*TAG:begin-axpy*/
                env.axpy(n, alpha, x, y);
                /*TAG:end-axpy*/

                /*TAG:begin-gemv*/
                env.gemv(mosek.transpose.no, m, n, alpha, A, x, beta, z);
                /*TAG:end-gemv*/

                /*TAG:begin-gemm*/
                env.gemm(mosek.transpose.no, mosek.transpose.no, m, n, k, alpha, A, B, beta, C);
                /*TAG:end-gemm*/

                /*TAG:begin-syrk*/
                env.syrk(mosek.uplo.lo, mosek.transpose.no, m, k, alpha, A, beta, D);
                /*TAG:end-syrk*/

                /* LAPACK routines*/

                /*TAG:begin-potrf*/
                env.potrf(mosek.uplo.lo, m, Q);
                /*TAG:end-potrf*/

                /*TAG:begin-syeig*/
                env.syeig(mosek.uplo.lo, m, Q, v);
                /*TAG:end-syeig*/

                /*TAG:begin-syevd*/
                env.syevd(mosek.uplo.lo, m, Q, v);
                /*TAG:end-syevd*/
            }
            catch (mosek.Exception e)
            {
                Console.WriteLine(e.Code);
                Console.WriteLine(e);
            }
            finally
            {
                if (env != null)
                {
                    env.Dispose();
                }
            }
        }
    }