Beispiel #1
0
int main(int argc,char *argv[])
{
  MSKenv_t  env = NULL;
  MSKtask_t task = NULL;
  MSKintt r = MSK_RES_OK;
  
  /* Create mosek environment. */
  r = MSK_makeenv(&env,NULL); 

  if ( r==MSK_RES_OK )
    r = MSK_maketask(env,0,0,&task);
  
  if ( r==MSK_RES_OK )
    MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);

  if (r == MSK_RES_OK)
    r = MSK_readdata(task,argv[1]);

  MSK_putintparam(task,MSK_IPAR_OPTIMIZER,MSK_OPTIMIZER_CONCURRENT);
  MSK_putintparam(task,MSK_IPAR_CONCURRENT_NUM_OPTIMIZERS,2);

  if (r == MSK_RES_OK)
    r = MSK_optimize(task);

  MSK_solutionsummary(task,MSK_STREAM_LOG);

   
  MSK_deletetask(&task);
  MSK_deleteenv(&env);

  printf("Return code: %d (0 means no error occured.)\n",r);

  return ( r );
} /* main */
Beispiel #2
0
 MosekEnvAutoPtr() {
   MSKrescodee r = MSK_makeenv(&env, NULL, NULL, NULL, NULL);
   if (r != MSK_RES_OK) {
     DBGA("Failed to create Mosek environment");
     assert(0);
   }
   MSK_linkfunctoenvstream(env, MSK_STREAM_LOG, NULL, printstr);
   r = MSK_initenv(env);
   if (r != MSK_RES_OK) {
     DBGA("Failed to initialize Mosek environment");
     assert(0);
   }
 }
Beispiel #3
0
template <typename _Scalar> void
MosekOpt<_Scalar>::_init( MSKenv_t *env )
{
    this->_r        = MSK_RES_OK;
    this->_env      = env;
    this->_ownsEnv  = !env;
    this->_task     = NULL;

    if ( !this->_env )
    {
        _r = MSK_makeenv( &_env, NULL );
        if ( MSK_RES_OK != _r )
        {
            std::cerr << "[" << __func__ << "]: " << "cannot create Mosek environment, nothing will work from now on..." << std::endl;
            return;
        }
    }
} //...MosekOpt::_init()
Beispiel #4
0
Problem * create_problem() {
  MSKrescodee  r;
  const MSKint32t numvar = 2;
  const MSKint32t numcon = 1;
  MSKboundkeye bkx[] = {MSK_BK_FR,
                        MSK_BK_FR};
  double       blx[] = {-MSK_INFINITY,
                        -MSK_INFINITY};
  double       bux[] = {+MSK_INFINITY,
                        +MSK_INFINITY};
  double       c[]   = {0.0,
                        1.0};
  /* will be used for cones  */
  MSKint32t i, j;
  MSKint32t csub[2];
  MSKenv_t env  = NULL;
  MSKtask_t task = NULL;
  r = MSK_makeenv(&env,NULL);
  r = MSK_maketask(env,numcon,numvar,&task);
  r = MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);
  r = MSK_appendcons(task,numcon);
  r = MSK_appendvars(task,numvar);
  for(j=0; j<numvar && r == MSK_RES_OK; ++j) {
    r = MSK_putcj(task,j,c[j]);
    r = MSK_putvarbound(task,
			j,           /* Index of variable.*/
			bkx[j],      /* Bound key.*/
			blx[j],      /* Numerical value of lower bound.*/
			bux[j]);     /* Numerical value of upper bound.*/
  }
  csub[0] = 0;
  csub[1] = 1;
  r = MSK_appendcone(task,
		     MSK_CT_QUAD,
		     0.0, /* For future use only, can be set to 0.0 */
		     2,
		     csub);
  r = MSK_putobjsense(task, MSK_OBJECTIVE_SENSE_MINIMIZE);
  Problem * p = new Problem;
  p->env_ = env;
  p->task_= task;
  return p;
}
Beispiel #5
0
int main(int argc,char **argv)
{
  MSKenv_t  env;
  MSKtask_t task;
  MSKintt NUMCON = 2;
  MSKintt NUMVAR = 2;
  
  double       c[]    = {1.0, 1.0};
  MSKintt      ptrb[] = {0, 2};
  MSKintt      ptre[] = {2, 3};
  MSKidxt      asub[] = {0, 1,
                        0, 1};
  double aval[] = {1.0, 1.0,
                   2.0, 1.0};
  MSKboundkeye bkc[]  = {MSK_BK_UP,
                       MSK_BK_UP};
  
  double blc[]  = {-MSK_INFINITY,
                   -MSK_INFINITY};
  double buc[]  = {2.0,
                   6.0};
  
  MSKboundkeye  bkx[]  = {MSK_BK_LO,
                          MSK_BK_LO};
  double  blx[]  = {0.0,
                    0.0};
  
  double  bux[]  = {+MSK_INFINITY,
                    +MSK_INFINITY};
  
  
  MSKrescodee       r = MSK_RES_OK;
  MSKidxt       i,nz;
  double    w1[] = {2.0,6.0};
  double    w2[] = {1.0,0.0};
  MSKidxt   sub[] = {0,1};
  MSKidxt   *basis;
    
  if (r == MSK_RES_OK)
    r = MSK_makeenv(&env,NULL,NULL,NULL,NULL);
  
  if ( r==MSK_RES_OK )
    MSK_linkfunctoenvstream(env,MSK_STREAM_LOG,NULL,printstr);
  
  if ( r==MSK_RES_OK )
    r = MSK_initenv(env);
  
  if ( r==MSK_RES_OK )
    r = MSK_makeemptytask(env,&task);
  
  if ( r==MSK_RES_OK )
      MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);  

  if ( r == MSK_RES_OK)
    r = MSK_inputdata(task, NUMCON,NUMVAR, NUMCON,NUMVAR, c, 0.0,
                      ptrb, ptre, asub, aval, bkc, blc, buc, bkx, blx, bux);

  if (r == MSK_RES_OK)
    r = MSK_putobjsense(task,MSK_OBJECTIVE_SENSE_MAXIMIZE); 

  
 
  if (r == MSK_RES_OK)
    r = MSK_optimize(task);

  if (r == MSK_RES_OK)
    basis = MSK_calloctask(task,NUMCON,sizeof(MSKidxt));
  
  if (r == MSK_RES_OK)
    r = MSK_initbasissolve(task,basis);

  /* List basis variables corresponding to columns of B */
  for (i=0;i<NUMCON && r == MSK_RES_OK;++i)
  {
    printf("basis[%d] = %d\n",i,basis[i]);   
    if (basis[sub[i]] < NUMCON)
      printf ("Basis variable no %d is xc%d.\n",i, basis[i]);
    else
      printf ("Basis variable no %d is x%d.\n",i,basis[i] - NUMCON); 
  }
  
  nz = 2;
  /* solve Bx = w1 */
  /* sub contains index of non-zeros in w1.
     On return w1 contains the solution x and sub 
     the index of the non-zeros in x. 
   */
  if (r == MSK_RES_OK)
    r = MSK_solvewithbasis(task,0,&nz,sub,w1);

  if (r == MSK_RES_OK)
  {
    printf("\nSolution to Bx = w1:\n\n");

    /* Print solution and b. */

    for (i=0;i<nz;++i) 
    {    
      if (basis[sub[i]] < NUMCON)     
        printf ("xc%d = %e\n",basis[sub[i]] , w1[sub[i]] );     
      else   
        printf ("x%d = %e\n",basis[sub[i]] - NUMCON , w1[sub[i]] );   
    }
  } 
    /* Solve B^Tx = c */
  nz = 2;
  sub[0] = 0;
  sub[1] = 1;

  if (r == MSK_RES_OK)  
    r = MSK_solvewithbasis(task,1,&nz,sub,w2);

  if (r == MSK_RES_OK)
  {
    printf("\nSolution to B^Tx = w2:\n\n");
    /* Print solution and y. */
    for (i=0;i<nz;++i) 
    {    
      if (basis[sub[i]] < NUMCON)     
        printf ("xc%d = %e\n",basis[sub[i]] , w2[sub[i]] );    
      else   
        printf ("x%d = %e\n",basis[sub[i]] - NUMCON , w2[sub[i]] );   
    }
  }
   
   printf("Return code: %d (0 means no error occurred.)\n",r);
   
   return ( r );
   
}/* main */
int main (int argc, char * argv[])
{
  MSKtask_t   task = NULL;
  MSKenv_t    env  = NULL;
  MSKrescodee r  = MSK_RES_OK;

  if (argc <= 1)
  {
    printf ("Missing argument. The syntax is:\n");
    printf (" simple inputfile [ solutionfile ]\n");
  }
  else
  {    
    /* Create the mosek environment. 
       The `NULL' arguments here, are used to specify customized 
       memory allocators and a memory debug file. These can
       safely be ignored for now. */
    
    r = MSK_makeenv(&env, NULL, NULL, NULL, NULL);
      
    /* Initialize the environment */
    if ( r==MSK_RES_OK )
      MSK_initenv (env);

    /* Create a task object linked to the environment env.
       Initially we create it with 0 variables and 0 columns, 
       since we do not know the size of the problem. */ 
    if ( r==MSK_RES_OK )
      r = MSK_maketask (env, 0,0, &task);

    if (r == MSK_RES_OK)
      MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);
      
    /* We assume that a problem file was given as the first command
       line argument (received in `argv'). */
    if ( r==MSK_RES_OK )   
      r = MSK_readdata (task, argv[1]);


    /* Solve the problem */
    if ( r==MSK_RES_OK )
    {
      MSKrescodee trmcode;
      
      MSK_optimizetrm(task,&trmcode);
    }

    /* Print a summary of the solution. */
    MSK_solutionsummary(task, MSK_STREAM_MSG);

    if (r == MSK_RES_OK)
    {
      MSKprostae prosta;
      MSKsolstae  solsta;
      MSKrealt primalobj,maxpbi,maxpcni,maxpeqi,maxinti,
        dualobj, maxdbi, maxdcni, maxdeqi;
      MSKintt isdef;
      MSKsoltypee whichsol = MSK_SOL_BAS;
      int accepted = 1;
      
        
      MSK_getsolutioninf (
                          task,
                          whichsol,
                          &prosta,
                          &solsta,
                          &primalobj,
                          &maxpbi,
                          &maxpcni,
                          &maxpeqi,
                          &maxinti,
                          &dualobj,
                          &maxdbi,
                          &maxdcni,
                          &maxdeqi);

        switch(solsta)
        {
          case MSK_SOL_STA_OPTIMAL:
          case MSK_SOL_STA_NEAR_OPTIMAL:
            {
              double max_primal_infeas = 0.0; /* maximal primal infeasibility */
              double max_dual_infeas   = 0.0; /* maximal dual infeasibility */
              double obj_gap = fabs(dualobj-primalobj);
           
            
              max_primal_infeas = double_max(max_primal_infeas,maxpbi);
              max_primal_infeas = double_max(max_primal_infeas,maxpcni);
              max_primal_infeas = double_max(max_primal_infeas,maxpeqi);
            
              max_dual_infeas = double_max(max_dual_infeas,maxdbi);
              max_dual_infeas = double_max(max_dual_infeas,maxdcni);
              max_dual_infeas = double_max(max_dual_infeas,maxdeqi);

              /* Assume the application needs the solution to be within
                 1e-6 ofoptimality in an absolute sense. Another approach
                 would be looking at the relative objective gap */
              printf("Objective gap: %e\n",obj_gap);
              if (obj_gap > 1e-6)
              {
                printf("Warning: The objective gap is too large.");
                accepted = 0;
              }            

              printf("Max primal infeasibility: %e\n", max_primal_infeas);
              printf("Max dual infeasibility: %e\n"  , max_dual_infeas);

              /* We will accept a primal infeasibility of 1e-8 and
                 dual infeasibility of 1e-6 */
            
              if (max_primal_infeas > 1e-8)
              {
                printf("Warning: Primal infeasibility is too large");
                accepted = 0;
              }

              if (max_dual_infeas > 1e-6)
              {
                printf("Warning: Dual infeasibility is too large");
                accepted = 0;
              }         
            }
            
            if (accepted && r == MSK_RES_OK)
            {
              MSKintt numvar,j;
              MSKrealt *xx = NULL;

              MSK_getnumvar(task,&numvar);
              
              xx = (double *) malloc(numvar*sizeof(MSKrealt));
              
              MSK_getsolutionslice(task,
                                   MSK_SOL_BAS,    /* Request the basic solution. */
                                   MSK_SOL_ITEM_XX,/* Which part of solution.     */
                                   0,              /* Index of first variable.    */
                                   numvar,         /* Index of last variable+1.   */
                                   xx);

      
              printf("Optimal primal solution\n");
              for(j=0; j<numvar; ++j)
                printf("x[%d]: %e\n",j,xx[j]);

              free(xx);
            }
            else
            {
              /* Print detailed information about the solution */
              if (r == MSK_RES_OK)
                r = MSK_analyzesolution(task,MSK_STREAM_LOG,whichsol);
            }
            break;
          case MSK_SOL_STA_DUAL_INFEAS_CER:
          case MSK_SOL_STA_PRIM_INFEAS_CER:
          case MSK_SOL_STA_NEAR_DUAL_INFEAS_CER:
          case MSK_SOL_STA_NEAR_PRIM_INFEAS_CER:  
            printf("Primal or dual infeasibility certificate found.\n");
            break;
          case MSK_SOL_STA_UNKNOWN:
            printf("The status of the solution could not be determined.\n");
            break;
          default:
            printf("Other solution status");
            break;
        }
    }
    else
    {
      printf("Error while optimizing.\n");
    }

    MSK_deletetask(&task);
    MSK_deleteenv(&env);
  }
  return r;
}
int main(int argc,char *argv[])
{
  const MSKint32t numvar = 3, 
                  numcon = 3;
  MSKint32t       i,j;
  double          c[]    = {1.5, 2.5, 3.0};
  MSKint32t       ptrb[] = {0, 3, 6},
                  ptre[] = {3, 6, 9},
                  asub[] = { 0, 1, 2,
                             0, 1, 2,
                             0, 1, 2};
  
  double          aval[] = { 2.0, 3.0, 2.0,
                             4.0, 2.0, 3.0,
                             3.0, 3.0, 2.0};
 
  MSKboundkeye    bkc[]  = {MSK_BK_UP, MSK_BK_UP, MSK_BK_UP    };
  double          blc[]  = {-MSK_INFINITY, -MSK_INFINITY, -MSK_INFINITY};
  double          buc[]  = {100000, 50000, 60000};
  
  MSKboundkeye    bkx[]  = {MSK_BK_LO,     MSK_BK_LO,    MSK_BK_LO};
  double          blx[]  = {0.0,           0.0,          0.0,};
  double          bux[]  = {+MSK_INFINITY, +MSK_INFINITY,+MSK_INFINITY};
  
  double          *xx=NULL;               
  MSKenv_t        env;
  MSKtask_t       task;
  MSKint32t       varidx,conidx; 
  MSKrescodee     r;

  /* Create the mosek environment. */
  r = MSK_makeenv(&env,NULL);

  if ( r==MSK_RES_OK )
  {
    /* Create the optimization task. */
    r = MSK_maketask(env,numcon,numvar,&task);

    /* Directs the log task stream to the 
       'printstr' function. */

    MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);
          
    /* Append the constraints. */
    if (r == MSK_RES_OK)
      r = MSK_appendcons(task,numcon);

    /* Append the variables. */
    if (r == MSK_RES_OK)
      r = MSK_appendvars(task,numvar);

    /* Put C. */
    if (r == MSK_RES_OK)
      r = MSK_putcfix(task, 0.0);

    if (r == MSK_RES_OK)
      for(j=0; j<numvar; ++j)
        r = MSK_putcj(task,j,c[j]);

    /* Put constraint bounds. */
    if (r == MSK_RES_OK)
      for(i=0; i<numcon; ++i)
        r = MSK_putconbound(task,i,bkc[i],blc[i],buc[i]);

    /* Put variable bounds. */
    if (r == MSK_RES_OK)
      for(j=0; j<numvar; ++j)
        r = MSK_putvarbound(task,j,bkx[j],blx[j],bux[j]);
                    
    /* Put A. */
    if (r == MSK_RES_OK)
      if ( numcon>0 )
        for(j=0; j<numvar; ++j)
          r = MSK_putacol(task,
                          j,
                          ptre[j]-ptrb[j],
                          asub+ptrb[j],
                          aval+ptrb[j]);
           
    if (r == MSK_RES_OK)
      r = MSK_putobjsense(task,
                          MSK_OBJECTIVE_SENSE_MAXIMIZE);

    if (r == MSK_RES_OK)
      r = MSK_optimizetrm(task,NULL);

    if (r == MSK_RES_OK)
    {
      xx = calloc(numvar,sizeof(double));
      if ( !xx )
        r = MSK_RES_ERR_SPACE;
    }

    if (r == MSK_RES_OK)
      r = MSK_getxx(task,
                    MSK_SOL_BAS,       /* Basic solution.       */
                    xx);
    
/* Make a change to the A matrix */
    if (r == MSK_RES_OK)
      r = MSK_putaij(task, 0, 0, 3.0);
    if (r == MSK_RES_OK)
      r = MSK_optimizetrm(task,NULL);

    /* Get index of new variable, this should be 3 */
    if (r == MSK_RES_OK)
      r = MSK_getnumvar(task,&varidx);

    /* Append a new variable x_3 to the problem */
    if (r == MSK_RES_OK)
      r = MSK_appendvars(task,1);
    
    /* Set bounds on new variable */
    if (r == MSK_RES_OK)
      r = MSK_putvarbound(task,
                          varidx,
                          MSK_BK_LO,
                          0,
                          +MSK_INFINITY);
    
    /* Change objective */
    if (r == MSK_RES_OK)
      r = MSK_putcj(task,varidx,1.0);
    
    /* Put new values in the A matrix */
    if (r == MSK_RES_OK)
    {
      MSKint32t acolsub[] = {0,   2};
      double    acolval[] =  {4.0, 1.0};
      
       r = MSK_putacol(task,
                       varidx, /* column index */
                       2, /* num nz in column*/
                       acolsub,
                       acolval);
    }
    
    /* Change optimizer to free simplex and reoptimize */
    if (r == MSK_RES_OK)
      r = MSK_putintparam(task,MSK_IPAR_OPTIMIZER,MSK_OPTIMIZER_FREE_SIMPLEX);
    
    if (r == MSK_RES_OK)
      r = MSK_optimizetrm(task,NULL);

    /* Get index of new constraint*/
    if (r == MSK_RES_OK)
      r = MSK_getnumcon(task,&conidx);

    /* Append a new constraint */
    if (r == MSK_RES_OK)
      r = MSK_appendcons(task,1);
    
    /* Set bounds on new constraint */
    if (r == MSK_RES_OK)
      r = MSK_putconbound(task,
                          conidx,
                          MSK_BK_UP,
                          -MSK_INFINITY,
                          30000);

    /* Put new values in the A matrix */
    if (r == MSK_RES_OK)
    {
      MSKidxt arowsub[] = {0,   1,   2,   3  };
      double arowval[] =  {1.0, 2.0, 1.0, 1.0};
      
      r = MSK_putarow(task,
                      conidx, /* row index */
                      4,      /* num nz in row*/
                      arowsub,
                      arowval);
    }
    if (r == MSK_RES_OK)
      r = MSK_optimizetrm(task,NULL);

    if ( xx )
      free(xx);
    
    MSK_deletetask(&task);
  }
  MSK_deleteenv(&env);

  printf("Return code: %d (0 means no error occured.)\n",r);

  return ( r );
} /* main */
Beispiel #8
0
int main()
{
  char         buffer[MSK_MAX_STR_LEN];
  double       oprfo[NUMOPRO],oprgo[NUMOPRO],oprho[NUMOPRO],
               oprfc[NUMOPRC],oprgc[NUMOPRC],oprhc[NUMOPRC],
               c[NUMVAR],aval[NUMANZ],
               blc[NUMCON],buc[NUMCON],blx[NUMVAR],bux[NUMVAR];
  int          numopro,numoprc,
               numcon=NUMCON,numvar=NUMVAR,
               opro[NUMOPRO],oprjo[NUMOPRO],
               oprc[NUMOPRC],opric[NUMOPRC],oprjc[NUMOPRC],
               aptrb[NUMVAR],aptre[NUMVAR],asub[NUMANZ];
  MSKboundkeye bkc[NUMCON],bkx[NUMVAR];
  MSKenv_t     env;
  MSKrescodee  r;
  MSKtask_t    task;
  schand_t     sch;

  /* Specify nonlinear terms in the objective. */
  numopro  = NUMOPRO;
  opro[0]  = MSK_OPR_LOG; /* Defined in scopt.h */
  oprjo[0] = 2;
  oprfo[0] = -1.0;
  oprgo[0] = 1.0;  /* This value is never used. */
  oprho[0] = 0.0;

  /* Specify nonlinear terms in the constraints. */
  numoprc  = NUMOPRC;
  
  oprc[0]  = MSK_OPR_POW;
  opric[0] = 0;
  oprjc[0] = 0;
  oprfc[0] = 1.0;
  oprgc[0] = 2.0;
  oprhc[0] = 0.0;

  oprc[1]  = MSK_OPR_POW;
  opric[1] = 0;
  oprjc[1] = 1;
  oprfc[1] = 1.0;
  oprgc[1] = 2.0;
  oprhc[1] = 0.0;

  /* Specify c */
  c[0] = 1.0; c[1] = 0.0; c[2] = 0.0;

  /* Specify a. */
  aptrb[0] = 0;   aptrb[1] = 1;   aptrb[2] = 2;
  aptre[0] = 1;   aptre[1] = 2;   aptre[2] = 3;
  asub[0]  = 1;   asub[1]  = 1;   asub[2]  = 1;
  aval[0]  = 1.0; aval[1]  = 2.0; aval[2]  = -1.0;

  /* Specify bounds for constraints. */
  bkc[0] = MSK_BK_UP;     bkc[1] = MSK_BK_FX;
  blc[0] = -MSK_INFINITY; blc[1] = 0.0;
  buc[0] = 1.0;           buc[1] = 0.0;

  /* Specify bounds for variables. */
  bkx[0] = MSK_BK_FR;      bkx[1] = MSK_BK_FR;     bkx[2] = MSK_BK_LO;
  blx[0] = -MSK_INFINITY;  blx[1] = -MSK_INFINITY; blx[2] = 0.0;
  bux[0] = MSK_INFINITY;   bux[1] = MSK_INFINITY;  bux[2] = MSK_INFINITY;

  /* Create  the mosek environment. */
  r = MSK_makeenv(&env,NULL);
 
  if ( r==MSK_RES_OK )
  {  
    /* Make the optimization task. */
    r = MSK_makeemptytask(env,&task);
    if ( r==MSK_RES_OK )
      MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);

    if ( r==MSK_RES_OK )
    {
      /* Setup the linear part of the problem. */
      r = MSK_inputdata(task,
                        numcon,numvar,
                        numcon,numvar,
                        c,0.0,
                        aptrb,aptre,
                        asub,aval,
                        bkc,blc,buc,
                        bkx,blx,bux);
    }
   
    if ( r== MSK_RES_OK )
    {
      /* Set-up of nonlinear expressions. */
      r = MSK_scbegin(task,
                      numopro,opro,oprjo,oprfo,oprgo,oprho,
                      numoprc,oprc,opric,oprjc,oprfc,oprgc,oprhc,
                      &sch);

      if ( r==MSK_RES_OK )
      {
        printf("Start optimizing\n");

        r = MSK_optimize(task);

        printf("Done optimizing\n");

        MSK_solutionsummary(task,MSK_STREAM_MSG);
      }
       
      /* The nonlinear expressions are no longer needed. */
      MSK_scend(task,&sch);
    }
    MSK_deletetask(&task);
  }
  MSK_deleteenv(&env);
       
  printf("Return code: %d\n",r);
  if ( r!=MSK_RES_OK )
  {
    MSK_getcodedesc(r,buffer,NULL);
    printf("Description: %s\n",buffer);
  }

  return r;
} /* main */
Beispiel #9
0
int main(int argc,char *argv[])
{
  MSKrescodee
    r;
  MSKboundkeye
    bkc[NUMCON],bkx[NUMVAR];
  int 
    j,i,
    ptrb[NUMVAR],ptre[NUMVAR],sub[NUMANZ];
  double
    blc[NUMCON],buc[NUMCON],
    c[NUMVAR],blx[NUMVAR],bux[NUMVAR],val[NUMANZ],
    xx[NUMVAR];
  MSKenv_t  env;
  MSKtask_t task;

  /* Make mosek environment. */
  r = MSK_makeenv(&env,NULL,NULL,NULL,NULL); 

  /* Check is return code is ok. */
  if ( r==MSK_RES_OK )
  {
    /* Directs the env log stream to the user
       specified procedure 'printstr'. */
       
    MSK_linkfunctoenvstream(env,MSK_STREAM_LOG,NULL,printstr);
  }

  /* Initialize the environment. */   
  r = MSK_initenv(env);

  if ( r==MSK_RES_OK )
  {  
    /* Send a message to the MOSEK Message stream. */
    MSK_echoenv(env,
                MSK_STREAM_MSG,
                "\nMaking the MOSEK optimization task\n");

    /* Make the optimization task. */
    r = MSK_maketask(env,NUMCON,NUMVAR,&task);

    if ( r==MSK_RES_OK )
    {
      /* Directs the log task stream to the user
         specified procedure 'printstr'. */

      MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);

      MSK_echotask(task,
                   MSK_STREAM_MSG,
                   "\nDefining the problem data.\n");

      /* Define bounds for the constraints. */

      /* Constraint: 0 */
      bkc[0] = MSK_BK_FX;  /* Type of bound. */
      blc[0] = 30.0;       /* Lower bound on the
                              constraint. */
      buc[0] = 30.0;       /* Upper bound on the
                              constraint. */

      /* Constraint: 1 */
      bkc[1] = MSK_BK_LO;
      blc[1] = 15.0;
      buc[1] = MSK_INFINITY;

      /* Constraint: 2 */
      bkc[2] = MSK_BK_UP;
      blc[2] = -MSK_INFINITY;
      buc[2] = 25.0;

      /* Define information for the variables. */

      /* Variable: x0 */
      c[0]    = 3.0;              /* The objective function. */

      ptrb[0] = 0;  ptre[0] = 2;  /* First column in
                                     the constraint matrix. */
      sub[0]  = 0;  val[0]  = 3.0;
      sub[1]  = 1;  val[1]  = 2.0;

      bkx[0]  = MSK_BK_LO;        /* Type of bound. */
      blx[0]  = 0.0;              /* Lower bound on the
                                     variables. */
      bux[0]  = MSK_INFINITY;     /* Upper bound on the
                                     variables.  */

      /* Variable: x1 */
      c[1]    = 1.0;

      ptrb[1] = 2;  ptre[1] = 5;
      sub[2]  = 0;  val[2]  = 1.0;
      sub[3]  = 1;  val[3]  = 1.0;
      sub[4]  = 2;  val[4]  = 2.0;

      bkx[1]  = MSK_BK_RA;
      blx[1]  = 0.0;
      bux[1]  = 10;


      /* Variable: x2 */
      c[2]    = 5.0;

      ptrb[2] = 5;  ptre[2] = 7;
      sub[5]  = 0;  val[5]  = 2.0;
      sub[6]  = 1;  val[6]  = 3.0;

      bkx[2]  = MSK_BK_LO;
      blx[2]  = 0.0;
      bux[2]  = MSK_INFINITY;

      /* Variable: x3 */
      c[3]    = 1.0;

      ptrb[3] = 7;  ptre[3] = 9;
      sub[7]  = 1;  val[7]  = 1.0;
      sub[8]  = 2;  val[8]  = 3.0;

      bkx[3]  = MSK_BK_LO;
      blx[3]  = 0.0;
      bux[3]  = MSK_INFINITY;

      MSK_putobjsense(task,
                      MSK_OBJECTIVE_SENSE_MAXIMIZE);

      /* Use the primal simplex optimizer. */
      MSK_putintparam(task,
                      MSK_IPAR_OPTIMIZER,
                      MSK_OPTIMIZER_PRIMAL_SIMPLEX);


      MSK_echotask(task,
                   MSK_STREAM_MSG,
                   "\nAdding constraints\n");
    
      r = MSK_append(task,
                     MSK_ACC_CON,
                     NUMCON);
   
      /* Adding bounds on empty constraints */
      for(i=0; r==MSK_RES_OK && i<NUMCON; ++i)
      {
        r = MSK_putbound(task,
                         MSK_ACC_CON,
                         i,
                         bkc[i], 
                         blc[i], 
                         buc[i]);
                         
      }

      /* Dynamically adding columns */
      for(j= 0; r==MSK_RES_OK && j<NUMVAR; ++j)
      {
        MSK_echotask(task,
                     MSK_STREAM_MSG,
                     "\nAdding a new variable.\n");

        r = MSK_append(task,MSK_ACC_VAR,1);

        if ( r==MSK_RES_OK )
          r = MSK_putcj(task,j,c[j]); 
                   
        if ( r==MSK_RES_OK )
          r = MSK_putavec(task,
                          MSK_ACC_VAR,
                          j,
                          ptre[j]-ptrb[j],
                          sub+ptrb[j],
                          val+ptrb[j]); 

        if ( r==MSK_RES_OK )
          r = MSK_putbound(task,
                           MSK_ACC_VAR,
                           j,
                           bkx[j], 
                           blx[j], 
                           bux[j]);
                             
        if(  r == MSK_RES_OK )
        {                            
          MSK_echotask(task,
                       MSK_STREAM_MSG,
                      "\nOptimizing\n");
                                                                        
          r = MSK_optimize(task);

          MSK_solutionsummary(task,MSK_STREAM_MSG);        
        }
      }

      MSK_deletetask(&task);
    }
  }
  MSK_deleteenv(&env);

  printf("Return code: %d (0 means no error occured.)\n",r);

  return ( r );
} /* main */
Beispiel #10
0
int main(int argc,char *argv[])
{
  MSKrescodee  r;
  MSKidxt i,j;
  double       c[]    = {3.0, 1.0, 5.0, 1.0};

  /* Below is the sparse representation of the A
     matrix stored by column. */
  MSKlidxt     aptrb[] = {0, 2, 5, 7};
  MSKlidxt     aptre[] = {2, 5, 7, 9};
  MSKidxt      asub[] = { 0, 1,
                          0, 1, 2,
                          0, 1,
                          1, 2};
  double       aval[] = { 3.0, 2.0,
                          1.0, 1.0, 2.0,
                          2.0, 3.0,
                          1.0, 3.0};

  /* Bounds on constraints. */
  MSKboundkeye bkc[]  = {MSK_BK_FX, MSK_BK_LO,     MSK_BK_UP    };
  double       blc[]  = {30.0,      15.0,          -MSK_INFINITY};
  double       buc[]  = {30.0,      +MSK_INFINITY, 25.0         };
  /* Bounds on variables. */
  MSKboundkeye bkx[]  = {MSK_BK_LO,     MSK_BK_RA, MSK_BK_LO,     MSK_BK_LO     };
  double       blx[]  = {0.0,           0.0,       0.0,           0.0           };
  double       bux[]  = {+MSK_INFINITY, 10.0,      +MSK_INFINITY, +MSK_INFINITY };
  double xx[NUMVAR];               
  MSKenv_t     env  = NULL;
  MSKtask_t    task = NULL; 
  
  /* Create the mosek environment. */
  r = MSK_makeenv(&env,NULL,NULL,NULL,NULL);
  
  /* Directs the env log stream to the 'printstr' function. */
  if ( r==MSK_RES_OK )
    MSK_linkfunctoenvstream(env,MSK_STREAM_LOG,NULL,printstr);
  
  /* Initialize the environment. */
  if ( r==MSK_RES_OK )
    r = MSK_initenv(env);
  
  if ( r==MSK_RES_OK )
  {
    /* Create the optimization task. */
    r = MSK_maketask(env,NUMCON,NUMVAR,&task);

    /* Directs the log task stream to the 'printstr' function. */
    if ( r==MSK_RES_OK )
      MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);

    /* 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. */
    if (r == MSK_RES_OK)
      r = MSK_putmaxnumvar(task,NUMVAR);
  
    if (r == MSK_RES_OK)
      r = MSK_putmaxnumcon(task,NUMCON);
    
    if (r == MSK_RES_OK)
      r = MSK_putmaxnumanz(task,NUMANZ);

    /* Append 'NUMCON' empty constraints.
     The constraints will initially have no bounds. */
    if ( r == MSK_RES_OK )
      r = MSK_append(task,MSK_ACC_CON,NUMCON);

    /* Append 'NUMVAR' variables.
     The variables will initially be fixed at zero (x=0). */
    if ( r == MSK_RES_OK )
      r = MSK_append(task,MSK_ACC_VAR,NUMVAR);

    /* Optionally add a constant term to the objective. */
    if ( r ==MSK_RES_OK )
      r = MSK_putcfix(task,0.0);
    for(j=0; j<NUMVAR && r == MSK_RES_OK; ++j)
    {
      /* Set the linear term c_j in the objective.*/  
      if(r == MSK_RES_OK)
        r = MSK_putcj(task,j,c[j]);

      /* Set the bounds on variable j.
       blx[j] <= x_j <= bux[j] */
      if(r == MSK_RES_OK)
        r = MSK_putbound(task,
                         MSK_ACC_VAR, /* Put bounds on variables.*/
                         j,           /* Index of variable.*/
                         bkx[j],      /* Bound key.*/
                         blx[j],      /* Numerical value of lower bound.*/
                         bux[j]);     /* Numerical value of upper bound.*/

      /* Input column j of A */   
      if(r == MSK_RES_OK)
        r = MSK_putavec(task,
                        MSK_ACC_VAR,       /* Input columns of A.*/
                        j,                 /* Variable (column) index.*/
                        aptre[j]-aptrb[j], /* Number of non-zeros in column j.*/
                        asub+aptrb[j],     /* Pointer to row indexes of column j.*/
                        aval+aptrb[j]);    /* Pointer to Values of column j.*/
      
    }

    /* Set the bounds on constraints.
       for i=1, ...,NUMCON : blc[i] <= constraint i <= buc[i] */
    for(i=0; i<NUMCON && r==MSK_RES_OK; ++i)
      r = MSK_putbound(task,
                       MSK_ACC_CON, /* Put bounds on constraints.*/
                       i,           /* Index of constraint.*/
                       bkc[i],      /* Bound key.*/
                       blc[i],      /* Numerical value of lower bound.*/
                       buc[i]);     /* Numerical value of upper bound.*/

    /* Maximize objective function. */
    if (r == MSK_RES_OK)
      r = MSK_putobjsense(task,
                          MSK_OBJECTIVE_SENSE_MAXIMIZE);

    if ( r==MSK_RES_OK )
    {
      MSKrescodee trmcode;
    
      /* Run optimizer */
      r = MSK_optimizetrm(task,&trmcode);

      /* Print a summary containing information
       about the solution for debugging purposes. */
      MSK_solutionsummary (task,MSK_STREAM_LOG);
     
      if ( r==MSK_RES_OK )
      {
        MSKsolstae solsta;
        int j;
        MSK_getsolutionstatus (task,
                               MSK_SOL_BAS,
                               NULL,
                               &solsta);
        switch(solsta)
        {
          case MSK_SOL_STA_OPTIMAL:   
          case MSK_SOL_STA_NEAR_OPTIMAL:
            MSK_getsolutionslice(task,
                                 MSK_SOL_BAS,    /* Request the basic solution. */
                                 MSK_SOL_ITEM_XX,/* Which part of solution.     */
                                 0,              /* Index of first variable.    */
                                 NUMVAR,         /* Index of last variable+1.   */
                                 xx);
      
            printf("Optimal primal solution\n");
            for(j=0; j<NUMVAR; ++j)
              printf("x[%d]: %e\n",j,xx[j]);
          
            break;
          case MSK_SOL_STA_DUAL_INFEAS_CER:
          case MSK_SOL_STA_PRIM_INFEAS_CER:
          case MSK_SOL_STA_NEAR_DUAL_INFEAS_CER:
          case MSK_SOL_STA_NEAR_PRIM_INFEAS_CER:  
            printf("Primal or dual infeasibility certificate found.\n");
            break;
            
          case MSK_SOL_STA_UNKNOWN:
            printf("The status of the solution could not be determined.\n");
            break;
          default:
            printf("Other solution status.");
            break;
        }
      }
      else
      {
        printf("Error while optimizing.\n");
      }
    }
    
    if (r != MSK_RES_OK)
    {
      /* In case of an error print error code and description. */      
      char symname[MSK_MAX_STR_LEN];
      char desc[MSK_MAX_STR_LEN];
      
      printf("An error occurred while optimizing.\n");     
      MSK_getcodedesc (r,
                       symname,
                       desc);
      printf("Error %s - '%s'\n",symname,desc);
    }
    
    MSK_deletetask(&task);
    
    MSK_deleteenv(&env);
  }
    
  return r;
}
Beispiel #11
0
int main(int argc,char *argv[])
{
  MSKrescodee  r;
  
  const MSKint32t numvar = 6,
                  numcon = 1;
      
  MSKboundkeye bkc[] = { MSK_BK_FX };
  double       blc[] = { 1.0 };
  double       buc[] = { 1.0 };
  
  MSKboundkeye bkx[] = {MSK_BK_LO,
                        MSK_BK_LO,
                        MSK_BK_LO,
                        MSK_BK_FR,
                        MSK_BK_FR,
                        MSK_BK_FR};
  double       blx[] = {0.0,
                        0.0,
                        0.0,
                        -MSK_INFINITY,
                        -MSK_INFINITY,
                        -MSK_INFINITY};
  double       bux[] = {+MSK_INFINITY,
                        +MSK_INFINITY,
                        +MSK_INFINITY,
                        +MSK_INFINITY,
                        +MSK_INFINITY,
                        +MSK_INFINITY};
  
  double       c[]   = {0.0,
                        0.0,
                        0.0,
                        1.0,
                        1.0,
                        1.0};

  MSKint32t   aptrb[] = {0, 1, 2, 3, 3, 3},
              aptre[] = {1, 2, 3, 3, 3, 3},
              asub[]  = {0, 0, 0, 0};
  double      aval[]  = {1.0, 1.0, 2.0};
  
   
  MSKint32t   i,j,csub[3];

  MSKenv_t    env  = NULL;
  MSKtask_t   task = NULL;

  /* Create the mosek environment. */
  r = MSK_makeenv(&env,NULL);

  if ( r==MSK_RES_OK )
  {
    /* Create the optimization task. */
    r = MSK_maketask(env,numcon,numvar,&task);

    if ( r==MSK_RES_OK )
    {
      MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);

      /* Append 'numcon' empty constraints.
     The constraints will initially have no bounds. */
      if ( r == MSK_RES_OK )
        r = MSK_appendcons(task,numcon);

      /* Append 'numvar' variables.
     The variables will initially be fixed at zero (x=0). */
      if ( r == MSK_RES_OK )
        r = MSK_appendvars(task,numvar);

      for(j=0; j<numvar && r == MSK_RES_OK; ++j)
      {
        /* Set the linear term c_j in the objective.*/  
        if(r == MSK_RES_OK)
          r = MSK_putcj(task,j,c[j]);

        /* Set the bounds on variable j.
       blx[j] <= x_j <= bux[j] */
        if(r == MSK_RES_OK)
          r = MSK_putvarbound(task,
                              j,           /* Index of variable.*/
                              bkx[j],      /* Bound key.*/
                              blx[j],      /* Numerical value of lower bound.*/
                              bux[j]);     /* Numerical value of upper bound.*/

        /* Input column j of A */   
        if(r == MSK_RES_OK)
          r = MSK_putacol(task,
                          j,                 /* Variable (column) index.*/
                          aptre[j]-aptrb[j], /* Number of non-zeros in column j.*/
                          asub+aptrb[j],     /* Pointer to row indexes of column j.*/
                          aval+aptrb[j]);    /* Pointer to Values of column j.*/
      
      }

      /* Set the bounds on constraints.
       for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
      for(i=0; i<numcon && r==MSK_RES_OK; ++i)
        r = MSK_putconbound(task,
                            i,           /* Index of constraint.*/
                            bkc[i],      /* Bound key.*/
                            blc[i],      /* Numerical value of lower bound.*/
                            buc[i]);     /* Numerical value of upper bound.*/
                 
      if ( r==MSK_RES_OK )
      {
        /* Append the first cone. */
        csub[0] = 3;
        csub[1] = 0;
        csub[2] = 1;
        r = MSK_appendcone(task,
                           MSK_CT_QUAD,
                           0.0, /* For future use only, can be set to 0.0 */
                           3,
                           csub);
      }

      if ( r==MSK_RES_OK )
      {
        /* Append the second cone. */
        csub[0] = 4;
        csub[1] = 5;
        csub[2] = 2;

        r = MSK_appendcone(task,
                           MSK_CT_RQUAD,
                           0.0,
                           3,
                           csub);
      }

      if ( r==MSK_RES_OK )
      {
        MSKrescodee trmcode;
        
        /* Run optimizer */
        r = MSK_optimizetrm(task,&trmcode);


        /* Print a summary containing information
           about the solution for debugging purposes*/
        MSK_solutionsummary (task,MSK_STREAM_MSG);
        
        if ( r==MSK_RES_OK )
        {
          MSKsolstae solsta;
          
          MSK_getsolsta (task,MSK_SOL_ITR,&solsta);
          
          switch(solsta)
          {
             case MSK_SOL_STA_OPTIMAL:   
             case MSK_SOL_STA_NEAR_OPTIMAL:
               {
                 double *xx = NULL;
                 
                 xx = calloc(numvar,sizeof(double));
                 if ( xx )
                 {                 
                   MSK_getxx (task,
                              MSK_SOL_ITR,    /* Request the interior solution. */
                              xx);

                   printf("Optimal primal solution\n");
                   for(j=0; j<numvar; ++j)
                     printf("x[%d]: %e\n",j,xx[j]);
                 }
                 else
                 {
                   r = MSK_RES_ERR_SPACE;
                 }
                 free(xx);
               }
               break;
             case MSK_SOL_STA_DUAL_INFEAS_CER:
             case MSK_SOL_STA_PRIM_INFEAS_CER:
             case MSK_SOL_STA_NEAR_DUAL_INFEAS_CER:
             case MSK_SOL_STA_NEAR_PRIM_INFEAS_CER:  
               printf("Primal or dual infeasibility certificate found.\n");
               break;
             case MSK_SOL_STA_UNKNOWN:
               printf("The status of the solution could not be determined.\n");
               break;
             default:
               printf("Other solution status.");
               break;
          }
        }
        else
        {
          printf("Error while optimizing.\n");
        }
      }
    
      if (r != MSK_RES_OK)
      {
        /* In case of an error print error code and description. */      
        char symname[MSK_MAX_STR_LEN];
        char desc[MSK_MAX_STR_LEN];
        
        printf("An error occurred while optimizing.\n");     
        MSK_getcodedesc (r,
                         symname,
                         desc);
        printf("Error %s - '%s'\n",symname,desc);
      }
    }
    /* Delete the task and the associated data. */
    MSK_deletetask(&task);
  }
 
  /* Delete the environment and the associated data. */
  MSK_deleteenv(&env);

  return ( r );
} /* main */
Beispiel #12
0
int mosek_qp_optimize(double** G, double* delta, double* alpha, long k, double C, double *dual_obj) {
  long i,j,t;
  double *c;
  MSKlidxt *aptrb;
  MSKlidxt *aptre;
  MSKidxt *asub;
  double *aval;
  MSKboundkeye bkc[1];
  double blc[1];
  double buc[1];
  MSKboundkeye *bkx;
  double *blx;
  double *bux;
  MSKidxt *qsubi,*qsubj;
  double *qval;

  MSKenv_t env;
  MSKtask_t task;
  MSKrescodee r;
  /*double dual_obj;*/

  c = (double*) malloc(sizeof(double)*k);
  assert(c!=NULL);
  aptrb = (MSKlidxt*) malloc(sizeof(MSKlidxt)*k);
  assert(aptrb!=NULL);
  aptre = (MSKlidxt*) malloc(sizeof(MSKlidxt)*k);
  assert(aptre!=NULL);
  asub = (MSKidxt*) malloc(sizeof(MSKidxt)*k);
  assert(asub!=NULL);
  aval = (double*) malloc(sizeof(double)*k);
  assert(aval!=NULL);
  bkx = (MSKboundkeye*) malloc(sizeof(MSKboundkeye)*k);
  assert(bkx!=NULL);
  blx = (double*) malloc(sizeof(double)*k);
  assert(blx!=NULL);
  bux = (double*) malloc(sizeof(double)*k);
  assert(bux!=NULL);
  qsubi = (MSKidxt*) malloc(sizeof(MSKidxt)*(k*(k+1)/2));
  assert(qsubi!=NULL);  
  qsubj = (MSKidxt*) malloc(sizeof(MSKidxt)*(k*(k+1)/2));
  assert(qsubj!=NULL);  
  qval = (double*) malloc(sizeof(double)*(k*(k+1)/2));
  assert(qval!=NULL);  
  
  
  /* DEBUG */
  /*
  for (i=0;i<k;i++) {
    printf("delta: %.4f\n", delta[i]);
  }
  printf("G:\n"); 
  for (i=0;i<k;i++) {
    for (j=0;j<k;j++) {
      printf("%.4f ", G[i][j]);
    }
    printf("\n");
  }
  fflush(stdout);
  */
  /* DEBUG */


  for (i=0;i<k;i++) {
		c[i] = -delta[i];
		aptrb[i] = i;
		aptre[i] = i+1;
		asub[i] = 0;
		aval[i] = 1.0;
		bkx[i] = MSK_BK_LO;
		blx[i] = 0.0;
		bux[i] = MSK_INFINITY;
  }
  bkc[0] = MSK_BK_UP;
  blc[0] = -MSK_INFINITY;
  buc[0] = C;
	/*
  bkc[0] = MSK_BK_FX;
  blc[0] = C;
  buc[0] = C;  
	*/
  
  /* create mosek environment */
  r = MSK_makeenv(&env, NULL, NULL, NULL, NULL);

  /* check return code */
  if (r==MSK_RES_OK) {
    /* directs output to printstr function */
    MSK_linkfunctoenvstream(env, MSK_STREAM_LOG, NULL, printstr);
  }

  /* initialize the environment */
  r = MSK_initenv(env);

  if (r==MSK_RES_OK) {
    /* create the optimization task */
    r = MSK_maketask(env,1,k,&task);
	
    if (r==MSK_RES_OK) {
      r = MSK_linkfunctotaskstream(task, MSK_STREAM_LOG,NULL,printstr);
	  
      if (r==MSK_RES_OK) {
	r = MSK_inputdata(task,
			  1,k,
			  1,k,
			  c,0.0,
			  aptrb,aptre,
			  asub,aval,
			  bkc,blc,buc,
			  bkx,blx,bux);
						  
      }
	  
      if (r==MSK_RES_OK) {
	/* coefficients for the Gram matrix */
	t = 0;
	for (i=0;i<k;i++) {
	  for (j=0;j<=i;j++) {
	    qsubi[t] = i;
	    qsubj[t] = j;
			qval[t] = G[i][j];
	    t++;
	  }
	}
	    
	r = MSK_putqobj(task, k*(k+1)/2, qsubi,qsubj,qval);
      }
      

      /* DEBUG */
      /*
      printf("t: %ld\n", t);
      for (i=0;i<t;i++) {
	printf("qsubi: %d, qsubj: %d, qval: %.4f\n", qsubi[i], qsubj[i], qval[i]);
      }
      fflush(stdout);
      */
      /* DEBUG */

      /* set relative tolerance gap (DEFAULT = 1E-8)*/
      //MSK_putdouparam(task, MSK_DPAR_INTPNT_TOL_REL_GAP, 1E-10);
      MSK_putdouparam(task, MSK_DPAR_INTPNT_TOL_REL_GAP, 1E-14);

      if (r==MSK_RES_OK) {
	r = MSK_optimize(task);
      }
      
      if (r==MSK_RES_OK) {
	MSK_getsolutionslice(task,
			     MSK_SOL_ITR,
			     MSK_SOL_ITEM_XX,
			     0,
			     k,
			     alpha);
        /* print out alphas */
	/*
	for (i=0;i<k;i++) {
	  printf("alpha[%ld]: %.8f\n", i, alpha[i]); fflush(stdout);
	}
	*/
	/* output the objective value */
	MSK_getprimalobj(task, MSK_SOL_ITR, dual_obj);
	//printf("ITER DUAL_OBJ %.8g\n", -(*dual_obj)); fflush(stdout);
      }
      MSK_deletetask(&task);
    }
    MSK_deleteenv(&env);
  }
  
  
  /* free the memory */
  free(c);
  free(aptrb);
  free(aptre);
  free(asub);
  free(aval);
  free(bkx);
  free(blx);
  free(bux);
  free(qsubi);  
  free(qsubj);  
  free(qval);  
  
	if(r == MSK_RES_OK)
  	return(0);  
	else
		return(r);
}
/**********************
lap: the upper RHS of the symmetric graph laplacian matrix which will be transformed
	to the hessian of the non-linear part of the optimisation function
n: number of nodes (length of coords array)
ordering: array containing sequences of nodes for each level,
	ie, ordering[levels[i]] is first node of (i+1)th level
level_indexes: array of starting node for each level in ordering
	ie, levels[i] is index to first node of (i+1)th level
	also, levels[0] is number of nodes in first level
	and, levels[i]-levels[i-1] is number of nodes in ith level
	and, n - levels[num_divisions-1] is number of nodes in last level
num_divisions: number of divisions between levels, ie number of levels - 1
separation: the minimum separation between nodes on different levels
***********************/
MosekEnv *mosek_init_hier(float *lap, int n, int *ordering,
    int *level_indexes, int num_divisions,
    float separation)
{
    int count = 0;
    int i, j, num_levels = num_divisions + 1;
    int num_constraints;
    MosekEnv *mskEnv = GNEW(MosekEnv);
    DigColaLevel *levels;
    int nonzero_lapsize = (n * (n - 1)) / 2;
    /* vars for nodes (except x0) + dummy nodes between levels
     * x0 is fixed at 0, and therefore is not included in the opt problem
     * add 2 more vars for top and bottom constraints
     */
    mskEnv->num_variables = n + num_divisions + 1;

    logfile = fopen("quad_solve_log", "w");
    levels = assign_digcola_levels(ordering, n, level_indexes, num_divisions);
#ifdef DUMP_CONSTRAINTS
    print_digcola_levels(logfile, levels, num_levels);
#endif

    /* nonlinear coefficients matrix of objective function */
    /* int lapsize=mskEnv->num_variables+(mskEnv->num_variables*(mskEnv->num_variables-1))/2; */
    mskEnv->qval = N_GNEW(nonzero_lapsize, double);
    mskEnv->qsubi = N_GNEW(nonzero_lapsize, int);
    mskEnv->qsubj = N_GNEW(nonzero_lapsize, int);

    /* solution vector */
    mskEnv->xx = N_GNEW(mskEnv->num_variables, double);

    /* constraint matrix */
    separation /= 2.0;		/* separation between each node and it's adjacent constraint */
    num_constraints = get_num_digcola_constraints(levels,
				    num_levels) + num_divisions + 1;
    /* constraints of the form x_i - x_j >= sep so 2 non-zero entries per constraint in LHS matrix
     * except x_0 (fixed at 0) constraints which have 1 nz val each.
     */
#ifdef EQUAL_WIDTH_LEVELS
    num_constraints += num_divisions;
#endif
    /* pointer to beginning of nonzero sequence in a column */

    for (i = 0; i < n - 1; i++) {
	for (j = i; j < n - 1; j++) {
	    mskEnv->qval[count] = -2 * lap[count + n];
	    assert(mskEnv->qval[count] != 0);
	    mskEnv->qsubi[count] = j;
	    mskEnv->qsubj[count] = i;
	    count++;
	}
    }
#ifdef DUMP_CONSTRAINTS
    fprintf(logfile, "Q=[");
    int lapcntr = n;
    for (i = 0; i < mskEnv->num_variables; i++) {
	if (i != 0)
	    fprintf(logfile, ";");
	for (j = 0; j < mskEnv->num_variables; j++) {
	    if (j < i || i >= n - 1 || j >= n - 1) {
		fprintf(logfile, "0 ");
	    } else {
		fprintf(logfile, "%f ", -2 * lap[lapcntr++]);
	    }
	}
    }
    fprintf(logfile, "]\nQ=Q-diag(diag(Q))+Q'\n");
#endif
    fprintf(logfile, "\n");
    /* Make the mosek environment. */
    mskEnv->r = MSK_makeenv(&mskEnv->env, NULL, NULL, NULL, NULL);

    /* Check whether the return code is ok. */
    if (mskEnv->r == MSK_RES_OK) {
	/* Directs the log stream to the user
	 * specified procedure 'printstr'. 
	 */
	MSK_linkfunctoenvstream(mskEnv->env, MSK_STREAM_LOG, NULL,
				printstr);
    }

    /* Initialize the environment. */
    mskEnv->r = MSK_initenv(mskEnv->env);
    if (mskEnv->r == MSK_RES_OK) {
	/* Make the optimization task. */
	mskEnv->r =
	    MSK_maketask(mskEnv->env, num_constraints,
			 mskEnv->num_variables, &mskEnv->task);

	if (mskEnv->r == MSK_RES_OK) {
	    int c_ind = 0;
	    int c_var = n - 1;
	    mskEnv->r =
		MSK_linkfunctotaskstream(mskEnv->task, MSK_STREAM_LOG,
					 NULL, printstr);
	    /* Resize the task. */
	    if (mskEnv->r == MSK_RES_OK)
		mskEnv->r = MSK_resizetask(mskEnv->task, num_constraints, mskEnv->num_variables, 0,	/* no cones!! */
					   /* each constraint applies to 2 vars */
					   2 * num_constraints +
					   num_divisions, nonzero_lapsize);

	    /* Append the constraints. */
	    if (mskEnv->r == MSK_RES_OK)
		mskEnv->r = MSK_append(mskEnv->task, 1, num_constraints);

	    /* Append the variables. */
	    if (mskEnv->r == MSK_RES_OK)
		mskEnv->r =
		    MSK_append(mskEnv->task, 0, mskEnv->num_variables);
	    /* Put variable bounds. */
	    for (j = 0;
		 j < mskEnv->num_variables && mskEnv->r == MSK_RES_OK; ++j)
		mskEnv->r =
		    MSK_putbound(mskEnv->task, 0, j, MSK_BK_RA,
				 -MSK_INFINITY, MSK_INFINITY);
	    for (j = 0; j < levels[0].num_nodes && mskEnv->r == MSK_RES_OK;
		 j++) {
		int node = levels[0].nodes[j] - 1;
		if (node >= 0) {
		    INIT_sub_val(c_var,node);
		    mskEnv->r =
			MSK_putavec(mskEnv->task, 1, c_ind, 2, subi, vali);
		} else {
		    /* constraint for y0 (fixed at 0) */
		    mskEnv->r =
			MSK_putaij(mskEnv->task, c_ind, c_var, 1.0);
		}
		mskEnv->r =
		    MSK_putbound(mskEnv->task, 1, c_ind, MSK_BK_LO,
				 separation, MSK_INFINITY);
		c_ind++;
	    }
	    for (i = 0; i < num_divisions && mskEnv->r == MSK_RES_OK; i++) {
		c_var = n + i;
		for (j = 0;
		     j < levels[i].num_nodes && mskEnv->r == MSK_RES_OK;
		     j++) {
		    /* create separation constraint a>=b+separation */
		    int node = levels[i].nodes[j] - 1;
		    if (node >= 0) {	/* no constraint for fixed node */
			INIT_sub_val(node,c_var);
			mskEnv->r =
			    MSK_putavec(mskEnv->task, 1, c_ind, 2, subi,
					vali);
		    } else {
			/* constraint for y0 (fixed at 0) */
			mskEnv->r =
			    MSK_putaij(mskEnv->task, c_ind, c_var, -1.0);
		    }
		    mskEnv->r =
			MSK_putbound(mskEnv->task, 1, c_ind, MSK_BK_LO,
				     separation, MSK_INFINITY);
		    c_ind++;
		}
		for (j = 0;
		     j < levels[i + 1].num_nodes
		     && mskEnv->r == MSK_RES_OK; j++) {
		    int node = levels[i + 1].nodes[j] - 1;
		    if (node >= 0) {
			INIT_sub_val(c_var,node);
			mskEnv->r =
			    MSK_putavec(mskEnv->task, 1, c_ind, 2, subi,
					vali);
		    } else {
			/* constraint for y0 (fixed at 0) */
			mskEnv->r =
			    MSK_putaij(mskEnv->task, c_ind, c_var, 1.0);
		    }
		    mskEnv->r =
			MSK_putbound(mskEnv->task, 1, c_ind, MSK_BK_LO,
				     separation, MSK_INFINITY);
		    c_ind++;
		}
	    }
	    c_var = n + i;
	    for (j = 0; j < levels[i].num_nodes && mskEnv->r == MSK_RES_OK;
		 j++) {
		/* create separation constraint a>=b+separation */
		int node = levels[i].nodes[j] - 1;
		if (node >= 0) {	/* no constraint for fixed node */
		    INIT_sub_val(node,c_var);
		    mskEnv->r =
			MSK_putavec(mskEnv->task, 1, c_ind, 2, subi, vali);
		} else {
		    /* constraint for y0 (fixed at 0) */
		    mskEnv->r =
			MSK_putaij(mskEnv->task, c_ind, c_var, -1.0);
		}
		mskEnv->r =
		    MSK_putbound(mskEnv->task, 1, c_ind, MSK_BK_LO,
				 separation, MSK_INFINITY);
		c_ind++;
	    }
	    /* create constraints preserving the order of dummy vars */
	    for (i = 0; i < num_divisions + 1 && mskEnv->r == MSK_RES_OK;
		 i++) {
		int c_var = n - 1 + i, c_var2 = c_var + 1;
		INIT_sub_val(c_var,c_var2);
		mskEnv->r =
		    MSK_putavec(mskEnv->task, 1, c_ind, 2, subi, vali);
		mskEnv->r =
		    MSK_putbound(mskEnv->task, 1, c_ind, MSK_BK_LO, 0,
				 MSK_INFINITY);
		c_ind++;
	    }
#ifdef EQUAL_WIDTH_LEVELS
	    for (i = 1; i < num_divisions + 1 && mskEnv->r == MSK_RES_OK;
		 i++) {
		int c_var = n - 1 + i, c_var_lo = c_var - 1, c_var_hi =
		    c_var + 1;
		INIT_sub_val3(c_var_lo, c_var, c_var_h);
		mskEnv->r =
		    MSK_putavec(mskEnv->task, 1, c_ind, 3, subi, vali);
		mskEnv->r =
		    MSK_putbound(mskEnv->task, 1, c_ind, MSK_BK_FX, 0, 0);
		c_ind++;
	    }
#endif
	    assert(c_ind == num_constraints);
#ifdef DUMP_CONSTRAINTS
	    fprintf(logfile, "A=[");
	    for (i = 0; i < num_constraints; i++) {
		if (i != 0)
		    fprintf(logfile, ";");
		for (j = 0; j < mskEnv->num_variables; j++) {
		    double aij;
		    MSK_getaij(mskEnv->task, i, j, &aij);
		    fprintf(logfile, "%f ", aij);
		}
	    }
	    fprintf(logfile, "]\n");
	    fprintf(logfile, "b=[");
	    for (i = 0; i < num_constraints; i++) {
		fprintf(logfile, "%f ", separation);
	    }
	    fprintf(logfile, "]\n");
#endif
	    if (mskEnv->r == MSK_RES_OK) {
		/*
		 * The lower triangular part of the Q
		 * matrix in the objective is specified.
		 */
		mskEnv->r =
		    MSK_putqobj(mskEnv->task, nonzero_lapsize,
				mskEnv->qsubi, mskEnv->qsubj,
				mskEnv->qval);
	    }
	}
    }
    delete_digcola_levels(levels, num_levels);
    return mskEnv;
}
/**********************
lap: the upper RHS of the symmetric graph laplacian matrix which will be transformed
	to the hessian of the non-linear part of the optimisation function
	has dimensions num_variables, dummy vars do not have entries in lap
cs: array of pointers to separation constraints
***********************/
MosekEnv *mosek_init_sep(float *lap, int num_variables, int num_dummy_vars,
			 Constraint ** cs, int num_constraints)
{
    int i, j;
    MosekEnv *mskEnv = GNEW(MosekEnv);
    int count = 0;
    int nonzero_lapsize = num_variables * (num_variables - 1) / 2;
    /* fix var 0 */
    mskEnv->num_variables = num_variables + num_dummy_vars - 1;

    fprintf(stderr, "MOSEK!\n");
    logfile = fopen("quad_solve_log", "w");

    /* nonlinear coefficients matrix of objective function */
    mskEnv->qval = N_GNEW(nonzero_lapsize, double);
    mskEnv->qsubi = N_GNEW(nonzero_lapsize, int);
    mskEnv->qsubj = N_GNEW(nonzero_lapsize, int);

    /* solution vector */
    mskEnv->xx = N_GNEW(mskEnv->num_variables, double);

    /* pointer to beginning of nonzero sequence in a column */

    for (i = 0; i < num_variables - 1; i++) {
	for (j = i; j < num_variables - 1; j++) {
	    mskEnv->qval[count] = -2 * lap[count + num_variables];
	    /* assert(mskEnv->qval[count]!=0); */
	    mskEnv->qsubi[count] = j;
	    mskEnv->qsubj[count] = i;
	    count++;
	}
    }
#ifdef DUMP_CONSTRAINTS
    fprintf(logfile, "Q=[");
    count = 0;
    for (i = 0; i < num_variables - 1; i++) {
	if (i != 0)
	    fprintf(logfile, ";");
	for (j = 0; j < num_variables - 1; j++) {
	    if (j < i) {
		fprintf(logfile, "0 ");
	    } else {
		fprintf(logfile, "%f ", -2 * lap[num_variables + count++]);
	    }
	}
    }
    fprintf(logfile, "]\nQ=Q-diag(diag(Q))+Q'\n");
#endif
    /* Make the mosek environment. */
    mskEnv->r = MSK_makeenv(&mskEnv->env, NULL, NULL, NULL, NULL);

    /* Check whether the return code is ok. */
    if (mskEnv->r == MSK_RES_OK) {
	/* Directs the log stream to the user
	   specified procedure 'printstr'. */
	MSK_linkfunctoenvstream(mskEnv->env, MSK_STREAM_LOG, NULL,
				printstr);
    }

    /* Initialize the environment. */
    mskEnv->r = MSK_initenv(mskEnv->env);
    if (mskEnv->r == MSK_RES_OK) {
	/* Make the optimization task. */
	mskEnv->r =
	    MSK_maketask(mskEnv->env, num_constraints,
			 mskEnv->num_variables, &mskEnv->task);

	if (mskEnv->r == MSK_RES_OK) {
	    mskEnv->r =
		MSK_linkfunctotaskstream(mskEnv->task, MSK_STREAM_LOG,
					 NULL, printstr);
	    /* Resize the task. */
	    if (mskEnv->r == MSK_RES_OK)
		mskEnv->r = MSK_resizetask(mskEnv->task, num_constraints, mskEnv->num_variables, 0,	/* no cones!! */
					   /* number of non-zero constraint matrix entries:
					    *   each constraint applies to 2 vars
					    */
					   2 * num_constraints,
					   nonzero_lapsize);

	    /* Append the constraints. */
	    if (mskEnv->r == MSK_RES_OK)
		mskEnv->r = MSK_append(mskEnv->task, 1, num_constraints);

	    /* Append the variables. */
	    if (mskEnv->r == MSK_RES_OK)
		mskEnv->r =
		    MSK_append(mskEnv->task, 0, mskEnv->num_variables);
	    /* Put variable bounds. */
	    for (j = 0;
		 j < mskEnv->num_variables && mskEnv->r == MSK_RES_OK; j++)
		mskEnv->r =
		    MSK_putbound(mskEnv->task, 0, j, MSK_BK_RA,
				 -MSK_INFINITY, MSK_INFINITY);
	    for (i = 0; i < num_constraints; i++) {
		int u = getLeftVarID(cs[i]) - 1;
		int v = getRightVarID(cs[i]) - 1;
		double separation = getSeparation(cs[i]);
		if (u < 0) {
		    mskEnv->r =
			MSK_putbound(mskEnv->task, 0, v, MSK_BK_RA,
				     -MSK_INFINITY, -separation);
		    assert(mskEnv->r == MSK_RES_OK);
		} else if (v < 0) {
		    mskEnv->r =
			MSK_putbound(mskEnv->task, 0, u, MSK_BK_RA,
				     separation, MSK_INFINITY);
		    assert(mskEnv->r == MSK_RES_OK);
		} else {
		    /* fprintf(stderr,"u=%d,v=%d,sep=%f\n",u,v,separation); */
		    INIT_sub_val(u,v);
		    mskEnv->r =
			MSK_putavec(mskEnv->task, 1, i, 2, subi, vali);
		    assert(mskEnv->r == MSK_RES_OK);
		    mskEnv->r =
			MSK_putbound(mskEnv->task, 1, i, MSK_BK_LO,
				     separation, MSK_INFINITY);
		    assert(mskEnv->r == MSK_RES_OK);
		}
	    }
	    if (mskEnv->r == MSK_RES_OK) {
		/*
		 * The lower triangular part of the Q
		 * matrix in the objective is specified.
		 */
		mskEnv->r =
		    MSK_putqobj(mskEnv->task, nonzero_lapsize,
				mskEnv->qsubi, mskEnv->qsubj,
				mskEnv->qval);
		assert(mskEnv->r == MSK_RES_OK);
	    }
	}
    }
    return mskEnv;
}
Beispiel #15
0
int do_thing()
{
   const MSKint32t numvar = 4,
   numcon = 3;
   
   double       c[]     = {3.0, 1.0, 5.0, 1.0};
   /* Below is the sparse representation of the A
    matrix stored by column. */
   MSKint32t    aptrb[] = {0, 2, 5, 7},
   aptre[] = {2, 5, 7, 9},
   asub[]  = { 0, 1,
      0, 1, 2,
      0, 1,
      1, 2};
   double       aval[]  = { 3.0, 2.0,
      1.0, 1.0, 2.0,
      2.0, 3.0,
      1.0, 3.0};
   
   /* Bounds on constraints. */
   MSKboundkeye bkc[]  = {MSK_BK_FX, MSK_BK_LO,     MSK_BK_UP    };
   double       blc[]  = {30.0,      15.0,          -MSK_INFINITY};
   double       buc[]  = {30.0,      +MSK_INFINITY, 25.0         };
   /* Bounds on variables. */
   MSKboundkeye bkx[]  = {MSK_BK_LO,     MSK_BK_RA, MSK_BK_LO,     MSK_BK_LO     };
   double       blx[]  = {0.0,           0.0,       0.0,           0.0           };
   double       bux[]  = {+MSK_INFINITY, 10.0,      +MSK_INFINITY, +MSK_INFINITY };
   MSKenv_t     env  = NULL;
   MSKtask_t    task = NULL;
   MSKrescodee  r;
   MSKint32t    i,j;
   
   /* Create the mosek environment. */
   r = MSK_makeenv(&env,NULL);
   
   if ( r==MSK_RES_OK )
   {
      /* Create the optimization task. */
      r = MSK_maketask(env,numcon,numvar,&task);
      
      /* Directs the log task stream to the 'printstr' function. */
      if ( r==MSK_RES_OK )
         r = MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);
      
      /* Append 'numcon' empty constraints.
       The constraints will initially have no bounds. */
      if ( r == MSK_RES_OK )
         r = MSK_appendcons(task,numcon);
      
      /* Append 'numvar' variables.
       The variables will initially be fixed at zero (x=0). */
      if ( r == MSK_RES_OK )
         r = MSK_appendvars(task,numvar);
      
      for(j=0; j<numvar && r == MSK_RES_OK; ++j)
      {
         /* Set the linear term c_j in the objective.*/
         if(r == MSK_RES_OK)
            r = MSK_putcj(task,j,c[j]);
         
         /* Set the bounds on variable j.
          blx[j] <= x_j <= bux[j] */
         if(r == MSK_RES_OK)
            r = MSK_putvarbound(task,
                                j,           /* Index of variable.*/
                                bkx[j],      /* Bound key.*/
                                blx[j],      /* Numerical value of lower bound.*/
                                bux[j]);     /* Numerical value of upper bound.*/
         
         /* Input column j of A */
         if(r == MSK_RES_OK)
            r = MSK_putacol(task,
                            j,                 /* Variable (column) index.*/
                            aptre[j]-aptrb[j], /* Number of non-zeros in column j.*/
                            asub+aptrb[j],     /* Pointer to row indexes of column j.*/
                            aval+aptrb[j]);    /* Pointer to Values of column j.*/
      }
      
      /* Set the bounds on constraints.
       for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
      for(i=0; i<numcon && r==MSK_RES_OK; ++i)
         r = MSK_putconbound(task,
                             i,           /* Index of constraint.*/
                             bkc[i],      /* Bound key.*/
                             blc[i],      /* Numerical value of lower bound.*/
                             buc[i]);     /* Numerical value of upper bound.*/
      
      /* Maximize objective function. */
      if (r == MSK_RES_OK)
         r = MSK_putobjsense(task, MSK_OBJECTIVE_SENSE_MAXIMIZE);
      
      if ( r==MSK_RES_OK )
      {
         MSKrescodee trmcode;
         
         /* Run optimizer */
         r = MSK_optimizetrm(task,&trmcode);
         
         /* Print a summary containing information
          about the solution for debugging purposes. */
         MSK_solutionsummary (task,MSK_STREAM_LOG);
         
         if ( r==MSK_RES_OK )
         {
            MSKsolstae solsta;
            
            if ( r==MSK_RES_OK )
               r = MSK_getsolsta (task,
                                  MSK_SOL_BAS,
                                  &solsta);
            switch(solsta)
            {
               case MSK_SOL_STA_OPTIMAL:
               case MSK_SOL_STA_NEAR_OPTIMAL:
               {
                  double *xx = (double*) calloc(numvar,sizeof(double));
                  if ( xx )
                  {
                     MSK_getxx(task,
                               MSK_SOL_BAS,    /* Request the basic solution. */
                               xx);
                     
                     printf("Optimal primal solution\n");
                     for(j=0; j<numvar; ++j)
                        printf("x[%d]: %e\n",j,xx[j]);
                     
                     free(xx);
                  }
                  else
                     r = MSK_RES_ERR_SPACE;
                  
                  break;
               }
               case MSK_SOL_STA_DUAL_INFEAS_CER:
               case MSK_SOL_STA_PRIM_INFEAS_CER:
               case MSK_SOL_STA_NEAR_DUAL_INFEAS_CER:
               case MSK_SOL_STA_NEAR_PRIM_INFEAS_CER:
                  printf("Primal or dual infeasibility certificate found.\n");
                  break;
               case MSK_SOL_STA_UNKNOWN:
               {
                  char symname[MSK_MAX_STR_LEN];
                  char desc[MSK_MAX_STR_LEN];
                  
                  /* If the solutions status is unknown, print the termination code
                   indicating why the optimizer terminated prematurely. */
                  
                  MSK_getcodedesc(trmcode,
                                  symname,
                                  desc);
                  
                  printf("The solution status is unknown.\n");
                  printf("The optimizer terminitated with code: %s\n",symname);
                  break;
               }
               default:
                  printf("Other solution status.\n");
                  break;
            }
         }
      }
      
      if (r != MSK_RES_OK)
      {
         /* In case of an error print error code and description. */
         char symname[MSK_MAX_STR_LEN];
         char desc[MSK_MAX_STR_LEN];
         
         printf("An error occurred while optimizing.\n");     
         MSK_getcodedesc (r,
                          symname,
                          desc);
         printf("Error %s - '%s'\n",symname,desc);
      }
      
      /* Delete the task and the associated data. */
      MSK_deletetask(&task);
   }
   
   /* Delete the environment and the associated data. */
   MSK_deleteenv(&env);
   
   return r;
}
Beispiel #16
0
int main(int argc,char *argv[])
{
  const MSKint32t numvar = 2,
                  numcon = 2;

  double       c[]   = {  1.0, 0.64 };
  MSKboundkeye bkc[] = { MSK_BK_UP,    MSK_BK_LO };
  double       blc[] = { -MSK_INFINITY,-4.0 };
  double       buc[] = { 250.0,        MSK_INFINITY };

  MSKboundkeye bkx[] = { MSK_BK_LO,    MSK_BK_LO };
  double       blx[] = { 0.0,          0.0 };
  double       bux[] = { MSK_INFINITY, MSK_INFINITY };
  

  MSKint32t    aptrb[] = { 0, 2 },
               aptre[] = { 2, 4 },
               asub[] = { 0,    1,   0,    1 };
  double       aval[] = { 50.0, 3.0, 31.0, -2.0 };
  MSKint32t    i,j;

  MSKenv_t     env = NULL;
  MSKtask_t    task = NULL;
  MSKrescodee  r;

  /* Create the mosek environment. */
  r = MSK_makeenv(&env,NULL);

  /* Check if return code is ok. */
  if ( r==MSK_RES_OK )
  {
    /* Create the optimization task. */
    r = MSK_maketask(env,0,0,&task);

    if ( r==MSK_RES_OK )
      r = MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);
    
    /* Append 'numcon' empty constraints.
     The constraints will initially have no bounds. */
    if ( r == MSK_RES_OK )
      r = MSK_appendcons(task,numcon);

    /* Append 'numvar' variables.
     The variables will initially be fixed at zero (x=0). */
    if ( r == MSK_RES_OK )
      r = MSK_appendvars(task,numvar);

    /* Optionally add a constant term to the objective. */
    if ( r ==MSK_RES_OK )
      r = MSK_putcfix(task,0.0);
    for(j=0; j<numvar && r == MSK_RES_OK; ++j)
    {
      /* Set the linear term c_j in the objective.*/  
      if(r == MSK_RES_OK)
        r = MSK_putcj(task,j,c[j]);

      /* Set the bounds on variable j.
       blx[j] <= x_j <= bux[j] */
      if(r == MSK_RES_OK)
        r = MSK_putvarbound(task,
                            j,           /* Index of variable.*/
                            bkx[j],      /* Bound key.*/
                            blx[j],      /* Numerical value of lower bound.*/
                            bux[j]);     /* Numerical value of upper bound.*/

      /* Input column j of A */   
      if(r == MSK_RES_OK)
        r = MSK_putacol(task,
                        j,                 /* Variable (column) index.*/
                        aptre[j]-aptrb[j], /* Number of non-zeros in column j.*/
                        asub+aptrb[j],     /* Pointer to row indexes of column j.*/
                        aval+aptrb[j]);    /* Pointer to Values of column j.*/
      
    }

    /* Set the bounds on constraints.
       for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
    for(i=0; i<numcon && r==MSK_RES_OK; ++i)
      r = MSK_putconbound(task,
                          i,           /* Index of constraint.*/
                          bkc[i],      /* Bound key.*/
                          blc[i],      /* Numerical value of lower bound.*/
                          buc[i]);     /* Numerical value of upper bound.*/
    
    /* Specify integer variables. */
    for(j=0; j<numvar && r == MSK_RES_OK; ++j)
      r = MSK_putvartype(task,j,MSK_VAR_TYPE_INT);
    
    if ( r==MSK_RES_OK )
      r =  MSK_putobjsense(task,
                           MSK_OBJECTIVE_SENSE_MAXIMIZE);
    
    if ( r==MSK_RES_OK )
    {
      MSKrescodee trmcode;

      /* Run optimizer */
      r = MSK_optimizetrm(task,&trmcode);

      /* Print a summary containing information
         about the solution for debugging purposes*/
      MSK_solutionsummary (task,MSK_STREAM_MSG);

      if ( r==MSK_RES_OK )
      {
        MSKint32t  j;
        MSKsolstae solsta;
        double     *xx = NULL; 

        MSK_getsolsta (task,MSK_SOL_ITG,&solsta);

        xx = calloc(numvar,sizeof(double));
        if ( xx ) 
        {        
          switch(solsta)
          {
             case MSK_SOL_STA_INTEGER_OPTIMAL:
             case MSK_SOL_STA_NEAR_INTEGER_OPTIMAL :             
               MSK_getxx(task,
                         MSK_SOL_ITG,    /* Request the integer solution. */
                         xx);
               
               printf("Optimal solution.\n");
               for(j=0; j<numvar; ++j)
                 printf("x[%d]: %e\n",j,xx[j]);                              
               break;
             case MSK_SOL_STA_PRIM_FEAS:
               /* A feasible but not necessarily optimal solution was located. */
               MSK_getxx(task,MSK_SOL_ITG,xx);
               
               printf("Feasible solution.\n");
               for(j=0; j<numvar; ++j)
                 printf("x[%d]: %e\n",j,xx[j]);               
               break;
             case MSK_SOL_STA_UNKNOWN:
               {
                 MSKprostae prosta; 
                 MSK_getprosta(task,MSK_SOL_ITG,&prosta); 
                 switch (prosta) 
                 {
                    case MSK_PRO_STA_PRIM_INFEAS_OR_UNBOUNDED:
                      printf("Problem status Infeasible or unbounded\n"); 
                      break; 
                    case MSK_PRO_STA_PRIM_INFEAS:
                      printf("Problem status Infeasible.\n"); 
                      break; 
                    case MSK_PRO_STA_UNKNOWN:
                      printf("Problem status unknown.\n"); 
                      break; 
                    default:
                      printf("Other problem status."); 
                      break;
                 }
               }
               break; 
             default:
               printf("Other solution status."); 
               break;               
          }
        }
        else
        {
          r = MSK_RES_ERR_SPACE;
        }
        free(xx);
      }
    }

    if (r != MSK_RES_OK)
    {
      /* In case of an error print error code and description. */
      char symname[MSK_MAX_STR_LEN];
      char desc[MSK_MAX_STR_LEN];

      printf("An error occurred while optimizing.\n");
      MSK_getcodedesc (r,
                       symname,
                       desc);
      printf("Error %s - '%s'\n",symname,desc);
    }

    MSK_deletetask(&task);
  }
  MSK_deleteenv(&env);

  printf("Return code: %d.\n",r);
  return ( r );
} /* main */
Beispiel #17
0
int main(int argc,char **argv)
{
  MSKintt   r=MSK_RES_OK,i;
  MSKenv_t  env = NULL;
  MSKtask_t task = NULL;
  MSKtask_t task_list[NUMTASKS];

  /* Ensure that we can delete tasks even if they are not allocated */
  task_list[0] = NULL;

  /* Create mosek environment. */
  r = MSK_makeenv(&env,NULL); 
  
  /* Create a task for each concurrent optimization.
     The 'task' is the master task that will hold the problem data.
  */ 

  if ( r==MSK_RES_OK )
    r = MSK_maketask(env,0,0,&task);

  if (r == MSK_RES_OK)
    r = MSK_maketask(env,0,0,&task_list[0]); 
     
  /* Assign call-back functions to each task */

  if (r == MSK_RES_OK)
    MSK_linkfunctotaskstream(task,
                             MSK_STREAM_LOG,
                             NULL,
                             printstr1);

  if (r == MSK_RES_OK)
    MSK_linkfunctotaskstream(task_list[0],
                             MSK_STREAM_LOG,
                             NULL,
                             printstr2);

  if (r == MSK_RES_OK)
     r = MSK_linkfiletotaskstream(task,
                                  MSK_STREAM_LOG,
                                  "simplex.log",
                                  0);

   if (r == MSK_RES_OK)
     r = MSK_linkfiletotaskstream(task_list[0],
                                  MSK_STREAM_LOG,
                                  "intpnt.log",
                                  0);


  if (r == MSK_RES_OK)
    r = MSK_readdata(task,argv[1]);

  /* Assign different parameter values to each task.
     In this case different optimizers. */

  if (r == MSK_RES_OK)
    r = MSK_putintparam(task,
                        MSK_IPAR_OPTIMIZER,
                        MSK_OPTIMIZER_PRIMAL_SIMPLEX);

  if (r == MSK_RES_OK)
    r = MSK_putintparam(task_list[0],
                        MSK_IPAR_OPTIMIZER,
                        MSK_OPTIMIZER_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].
   */

  if (r == MSK_RES_OK)
    r = MSK_optimizeconcurrent (task,
                                task_list,
                                NUMTASKS);

  printf ("Return Code = %d\n",r);

  MSK_solutionsummary(task,
                      MSK_STREAM_LOG);

  MSK_deletetask(&task);
  MSK_deletetask(&task_list[0]);
  MSK_deleteenv(&env);

  return r;
}
Beispiel #18
0
void MyQCQP::optimize()
{
	// resize alpha
	if(alpha != NULL)
		delete []alpha;
	alpha = new double[numvar];

	double *c_ = new double[numvar];
	for(int i = 0;i<numvar;i++) c_[i] = c[i];

	MSKboundkeye *bkc_ = new MSKboundkeye[numcon];
	double * blc_ = new double[numcon];
	double * buc_ = new double[numcon];
	for(int i = 0;i<numcon;i++)
	{
		bkc_[i] = bkc[i];
		blc_[i] = blc[i];
		buc_[i] = buc[i];
	}

	MSKboundkeye *bkx_ = new MSKboundkeye[numvar];
	double * blx_ = new double[numvar];
	double * bux_ = new double[numvar];
	for(int i = 0;i<numvar;i++)
	{
		bkx_[i] = bkx[i];
		blx_[i] = blx[i];
		bux_[i] = bux[i];
	}

	MSKlidxt *aptrb_ = new MSKlidxt[aptrb.size()];
	for(size_t i = 0;i<aptrb.size();i++) aptrb_[i] = aptrb[i];
	MSKlidxt * aptre_ = new MSKlidxt[aptre.size()];
	for(size_t i = 0;i<aptre.size();i++) aptre_[i] = aptre[i];
	MSKidxt * asub_ = new MSKidxt[asub.size()];
	for(size_t i = 0;i<asub.size();i++) asub_[i] = asub[i];
	double *aval_ = new double[aval.size()];
	for(size_t i = 0;i<aval.size();i++) aval_[i] = aval[i];

	MSKrescodee r;
	MSKenv_t env;
	MSKtask_t task;
	r = MSK_makeenv(&env,NULL,NULL,NULL,NULL);
	r = MSK_initenv(env);

	if(r == MSK_RES_OK)
	{
		r = MSK_maketask(env,numcon,numvar,&task);
		if(r == MSK_RES_OK)
			r = MSK_append(task,MSK_ACC_CON,numcon);

		if(r == MSK_RES_OK)
			r = MSK_append(task,MSK_ACC_VAR, numvar);

		for(int j = 0;j<numvar && r== MSK_RES_OK;j++)
		{
			if(r == MSK_RES_OK)
				r = MSK_putcj(task,j,c_[j]);

			if(r == MSK_RES_OK)
				r = MSK_putbound(task,MSK_ACC_VAR,j,bkx_[j],blx_[j],bux_[j]);

			if(r == MSK_RES_OK)
				r = MSK_putavec(task,MSK_ACC_VAR,j,aptre_[j] - aptrb_[j], asub_ + aptrb_[j],aval_+aptrb_[j]);
		}

		for(int i=0;i<numcon  && r== MSK_RES_OK;i++)
		{
			r = MSK_putbound(task,MSK_ACC_CON,i,bkc_[i],blc_[i],buc_[i]);
		}


		delete []c_;
		delete []bkx_;
		delete []blx_;
		delete []bux_;
		delete []aptrb_;
		delete []aptre_;
		delete []asub_;
		delete []aval_;
		delete []bkc_;
		delete []blc_;
		delete []buc_;


		for(int i=0;i<numcon-1 && r== MSK_RES_OK;i++) // numcon-1 quadratic constraints
		{
			int nzero = qsubi[i].size();
			MSKidxt * qsubi_ = new MSKidxt[nzero];
			MSKidxt * qsubj_ = new MSKidxt[nzero];
			double * qval_ = new double[nzero];
			for(int m = 0;m<nzero;m++)
			{
				qsubi_[m] = qsubi[i][m];
				qsubj_[m] = qsubj[i][m];
				qval_[m] = qval[i][m];
			}

			if(r == MSK_RES_OK)
				r = MSK_putqconk(task,i,nzero,qsubi_,qsubj_,qval_);

			delete []qsubi_;
			delete []qsubj_;
			delete []qval_;
		}


		if(r == MSK_RES_OK)
			r = MSK_putobjsense(task,MSK_OBJECTIVE_SENSE_MINIMIZE);

		if(r == MSK_RES_OK)
		{
			MSKrescodee trmcode;
			r = MSK_optimizetrm(task,&trmcode);

			MSK_getsolutionslice(task,MSK_SOL_ITR, MSK_SOL_ITEM_XX,0,numvar,alpha);

			MSK_getsolutionslice(task,MSK_SOL_ITR,MSK_SOL_ITEM_SUC,0,numcon,mu);
		}
		MSK_deletetask(&task);
	}
	MSK_deleteenv(&env);
}
Beispiel #19
0
int main(int argc,char **argv)
{
  MSKenv_t  env;
  MSKtask_t task;
  MSKrescodee r = MSK_RES_OK;
  MSKintt   numvar = NUMCON;
  MSKintt   numcon = NUMVAR;   /* we must have numvar == numcon */
  int       i,nz;
  double    aval[] = {-1.0,1.0,1.0};
  MSKidxt   asub[] = {1,0,1};
  MSKidxt   ptrb[] = {0,1};
  MSKidxt   ptre[] = {1,3};
  
  MSKidxt   bsub[NUMCON];
  double    b[NUMCON];

  MSKidxt   *basis = NULL;

  if (r == MSK_RES_OK)
    r = MSK_makeenv(&env,NULL,NULL,NULL,NULL);
  
  if ( r==MSK_RES_OK )
    MSK_linkfunctoenvstream(env,MSK_STREAM_LOG,NULL,printstr);
  
  if ( r==MSK_RES_OK )
    r = MSK_initenv(env);
  
  if ( r==MSK_RES_OK )
    r = MSK_makeemptytask(env,&task);
  
  if ( r==MSK_RES_OK )
      MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);
  
  basis = (MSKidxt *) calloc(numcon,sizeof(MSKidxt));
  if ( basis == NULL && numvar)
    r = MSK_RES_ERR_SPACE;
    
    
  /* Put A matrix and factor A.
     Call this function only once for a given task. */ 
  if (r == MSK_RES_OK)
    r = put_a( task,
               aval,
               asub,
               ptrb,
               ptre,
               numvar,
               basis
               );

  /* now solve rhs */
  b[0] = 1;
  b[1] = -2;
  bsub[0] = 0;
  bsub[1] = 1;
  nz = 2;
  
  if (r == MSK_RES_OK)
    r = MSK_solvewithbasis(task,0,&nz,bsub,b);

  if (r == MSK_RES_OK)
  {
    printf("\nSolution to Bx = b:\n\n");
    /* Print solution and show correspondents
       to original variables in the problem */
    for (i=0;i<nz;++i) 
    {    
      if (basis[bsub[i]] < numcon)
        printf("This should never happen\n");
      else   
        printf ("x%d = %e\n",basis[bsub[i]] - numcon , b[bsub[i]] );   
    }
  }
    
  b[0] = 7;
  bsub[0] = 0;
  nz = 1;
  
  if (r == MSK_RES_OK)
    r = MSK_solvewithbasis(task,0,&nz,bsub,b);

  if (r == MSK_RES_OK)
  {
    printf("\nSolution to Bx = b:\n\n");
    /* Print solution and show correspondents
       to original variables in the problem */
    for (i=0;i<nz;++i) 
    {    
      if (basis[bsub[i]] < numcon)
        printf("This should never happen\n");
      else   
        printf ("x%d = %e\n",basis[bsub[i]] - numcon , b[bsub[i]] );   
    }
  }
    
  free (basis);
  return r;
}
Beispiel #20
0
bool ConicSolver::Solve(VectorXd& sol)
{
    bool ret = false;
#ifdef _WIN32
    VectorXd solution;
	convertMatrixVectorFormat();
	MSKenv_t env;
	MSKtask_t task;
	MSKrescodee r;

	r = MSK_makeenv(&env, NULL, NULL, NULL, NULL);
	if (r == MSK_RES_OK)
	{
		r = MSK_linkfunctoenvstream(env, MSK_STREAM_LOG, NULL, printstr);
	}

	r = MSK_initenv(env);
	if (r == MSK_RES_OK)
	{
		r = MSK_maketask(env, mNumCon, mNumVar, &task);
		if (r == MSK_RES_OK)
		{
			r = MSK_linkfunctotaskstream(task, MSK_STREAM_LOG, NULL, printstr);
		}

		if (r == MSK_RES_OK)
			r = MSK_putmaxnumvar(task, mNumVar);
		if (r == MSK_RES_OK)
			r = MSK_putmaxnumcon(task, mNumCon);

		/* Append ¡¯NUMCON ¡¯ empty constraints .
		 The constraints will initially have no bounds . */
		if (r == MSK_RES_OK)
			r = MSK_append(task, MSK_ACC_CON, mNumCon);
		/* Append ¡¯NUMVAR ¡¯ variables .
		 The variables will initially be fixed at zero (x =0). */
		if (r == MSK_RES_OK)
			r = MSK_append(task, MSK_ACC_VAR, mNumVar);

		/* Optionally add a constant term to the objective . */
		if (r == MSK_RES_OK)
			r = MSK_putcfix(task, mConstant);

		for (int j = 0; j < mNumVar && r == MSK_RES_OK; ++j)
		{
			/* Set the linear term c_j in the objective .*/
			if (r == MSK_RES_OK)
				r = MSK_putcj(task, j, mc[j]);
			/* Set the bounds on variable j.*/
			if (r == MSK_RES_OK)
			{
				if (mbLowerBounded[j] && mbUpperBounded[j])
				{
					if (mlb[j] == mub[j])
						r = MSK_putbound(task, MSK_ACC_VAR, j, MSK_BK_FX, mlb[j], mub[j]);
					else
					{
						CHECK(mlb[j] < mub[j]);
						r = MSK_putbound(task, MSK_ACC_VAR, j, MSK_BK_RA, mlb[j], mub[j]);
					}
				}
				else if (mbLowerBounded[j])
				{
					r = MSK_putbound(task, MSK_ACC_VAR, j , MSK_BK_LO, mlb[j], +MSK_INFINITY);
				}
				else if (mbUpperBounded[j])
				{
					r = MSK_putbound(task, MSK_ACC_VAR, j, MSK_BK_UP, -MSK_INFINITY, mub[j]);
				}	
				else
				{
					r = MSK_putbound(task, MSK_ACC_VAR, j, MSK_BK_FR, -MSK_INFINITY, +MSK_INFINITY);
				}
			}
			/* Input column j of A */
			if (r == MSK_RES_OK && mNumCon)
			{
				int currentColumnIdx = mAColumnStartIdx[j];
				int nextColumnIdx = mAColumnStartIdx[j + 1];
                if (nextColumnIdx - currentColumnIdx > 0)
				    r = MSK_putavec(task, MSK_ACC_VAR, j, nextColumnIdx - currentColumnIdx, &(mARowIdx[currentColumnIdx]), &(mAValues[currentColumnIdx]));
			}
		}
		/* Set the bounds on constraints .
		 for i=1, ... , NUMCON : blc [i] <= constraint i <= buc [i] */
		for (int i = 0; i < mNumCon && r == MSK_RES_OK; ++i)
		{
			if (mbConstraintLowerBounded[i] && mbConstraintUpperBounded[i])
			{
				if (mlbc[i] == mubc[i])
				{
					r = MSK_putbound(task, MSK_ACC_CON, i, MSK_BK_FX, mlbc[i], mubc[i]);
				}
				else 
				{
					r = MSK_putbound(task, MSK_ACC_CON, i, MSK_BK_RA, mlbc[i], mubc[i]);
				}
			}
			else if (mbConstraintLowerBounded[i])
			{
				r = MSK_putbound(task, MSK_ACC_CON, i, MSK_BK_LO, mlbc[i], +MSK_INFINITY);
			}
			else if (mbConstraintUpperBounded[i])
			{
				r = MSK_putbound(task, MSK_ACC_CON, i, MSK_BK_UP, -MSK_INFINITY, mubc[i]);
			}
			else
			{
				LOG(WARNING) << "Every constraint should not be free.";
			}
		}
        for (int i = 0; i < mNumCone; ++i)
        {
            Cone& cone = mCones[i];
            r = MSK_appendcone(task, MSK_CT_RQUAD, 0.0, cone.mSubscripts.size(), cone.GetMosekConeSubId());
            //r = MSK_appendcone(task, MSK_CT_QUAD, 0.0, cone.mSubscripts.size(), cone.GetMosekConeSubId());
        }
		if (r == MSK_RES_OK)
		{
			MSKrescodee trmcode;

			r = MSK_optimizetrm(task, &trmcode);
			MSK_solutionsummary(task, MSK_STREAM_LOG);

			if (r == MSK_RES_OK)
			{
				MSKsolstae solsta;
				MSK_getsolutionstatus(task, MSK_SOL_ITR, NULL, &solsta);
				double* result = new double[mNumVar];
				switch (solsta)
				{
				case MSK_SOL_STA_OPTIMAL:
				case MSK_SOL_STA_NEAR_OPTIMAL:
					MSK_getsolutionslice(task, MSK_SOL_ITR, MSK_SOL_ITEM_XX, 0, mNumVar, result);
					LOG(INFO) << "Optimal primal solution";
                    ret = true;
					solution = VectorXd::Zero(mNumVar);
                    sol = VectorXd::Zero(mNumVar);
					for (int k = 0; k < mNumVar; ++k)
                    {
						solution[k] = result[k];
                        sol[k] = result[k];
                    }
					break;
				case MSK_SOL_STA_DUAL_INFEAS_CER:
				case MSK_SOL_STA_PRIM_INFEAS_CER:
				case MSK_SOL_STA_NEAR_DUAL_INFEAS_CER:
				case MSK_SOL_STA_NEAR_PRIM_INFEAS_CER:
					LOG(WARNING) << "Primal or dual infeasibility certificate found.";
					break;
				case MSK_SOL_STA_UNKNOWN:
					LOG(WARNING) << "The status of the solution could not be determined.";
					break;
				default:
					LOG(WARNING) << "Other solution status.";
					break;

				}
				delete[] result;

			}
		}
		else
		{
			LOG(WARNING) << "Error while optimizing.";
		}
		if (r != MSK_RES_OK)
		{
			char symname[MSK_MAX_STR_LEN];
			char desc[MSK_MAX_STR_LEN];
			LOG(WARNING) << "An error occurred while optimizing.";
			MSK_getcodedesc(r, symname, desc);
			LOG(WARNING) << "Error " << symname << " - " << desc;
		
		}
       
	}
	MSK_deletetask(&task);
	MSK_deleteenv(&env);
#endif    
	return ret;
}
Beispiel #21
0
int main (int argc, char ** argv)
{
  MSKenv_t  env = NULL;
  MSKtask_t task = NULL;
  MSKrescodee res = MSK_RES_OK;
  MSKintt numvar = 0;

  res = MSK_makeenv(&env, NULL,NULL,NULL,NULL);
  
  if (res == MSK_RES_OK)
    res = MSK_initenv(env);
  
  if (res == MSK_RES_OK)
    res = MSK_maketask(env, 0,0, &task);

  if (res == MSK_RES_OK)
  {
    MSKrealt * c = MSK_calloctask(task, numvar, sizeof(MSKrealt));
    MSK_getc(task,c);
  }

  if (res == MSK_RES_OK)
  {
    MSKrealt * upper_bound   = MSK_calloctask(task,8,sizeof(MSKrealt));
    MSKrealt * lower_bound   = MSK_calloctask(task,8,sizeof(MSKrealt));
    MSKboundkeye * bound_key = MSK_calloctask(task,8,sizeof(MSKboundkeye));
    MSK_getboundslice(task,MSK_ACC_CON, 2,10,
                      bound_key,lower_bound,upper_bound);
  }
  if (res == MSK_RES_OK)
  {
    MSKidxt bound_index[]    = {         1,         6,         3,         9 };
    MSKboundkeye bound_key[] = { MSK_BK_FR, MSK_BK_LO, MSK_BK_UP, MSK_BK_FX };
    MSKrealt lower_bound[]   = {       0.0,     -10.0,       0.0,       5.0 };
    MSKrealt upper_bound[]   = {       0.0,       0.0,       6.0,       5.0 };
    MSK_putboundlist(task,MSK_ACC_CON, 4, bound_index,
                      bound_key,lower_bound,upper_bound);
  }
  if (res == MSK_RES_OK)
  {
    MSKidxt subi[] = {   1,   3,   5 };
    MSKidxt subj[] = {   2,   3,   4 };
    MSKrealt cof[] = { 1.1, 4.3, 0.2 };
    MSK_putaijlist(task,3, subi,subj,cof);
  }


  if (res == MSK_RES_OK)
  {
    MSKlintt rowsub[] = { 0, 1, 2, 3 };
    MSKlidxt ptrb[]   = { 0, 3, 5, 7 };
    MSKlidxt ptre[]   = { 3, 5, 7, 8 };
    MSKlidxt sub[]    = { 0, 2, 3, 1, 4, 0, 3, 2 };
    MSKrealt cof[]    = { 1.1, 1.3, 1.4, 2.2, 2.5, 3.1, 3.4, 4.4 };
                  
    MSK_putaveclist (task,MSK_ACC_CON,4,
                     rowsub,ptrb,ptre,
                     sub,cof);
  }
  
  MSK_deletetask(&task);
  MSK_deleteenv(&env);
}