Beispiel #1
0
int mosekNNSolverWrapper(const Matrix &Q, const Matrix &Eq, const Matrix &b,
                         const Matrix &InEq, const Matrix &ib,
                         const Matrix &lowerBounds, const Matrix &upperBounds,
                         Matrix &sol, double *objVal, MosekObjectiveType objType)
{
  DBGP("Mosek QP Wrapper started");
  MSKrescodee  r;
  MSKtask_t task = NULL;

  // Get the only instance of the mosek environment.
  MSKenv_t     env  = getMosekEnv();
  // Create the optimization task.
  r = MSK_maketask(env, 0, 0, &task);
  if (r != MSK_RES_OK) {
    DBGA("Failed to create optimization task");
    return -1;
  }
  MSK_linkfunctotaskstream(task, MSK_STREAM_LOG, NULL, printstr);

  //---------------------------------------
  //start inputing the problem
  //prespecify number of variables to make inputting faster
  r = MSK_putmaxnumvar(task, sol.rows());
  //number of constraints (both equality and inequality)
  if (r == MSK_RES_OK) {
    r = MSK_putmaxnumcon(task, Eq.rows() + InEq.rows());
  }
  //make sure default value is 0 for sparse matrices
  assert(Q.getDefault() == 0.0);
  assert(Eq.getDefault() == 0.0);
  assert(InEq.getDefault() == 0.0);
  //number of non-zero entries in A
  if (r == MSK_RES_OK) {
    r = MSK_putmaxnumanz(task, Eq.numElements() + InEq.numElements());
  }
  if (r != MSK_RES_OK) {
    DBGA("Failed to input variables");
    MSK_deletetask(&task);
    return -1;
  }

  //solver is sensitive to numerical problems. Scale the problem down
  //we will use this value to scale down the right hand side of equality
  //and inequality constraints and lower and upper bounds
  //after solving, we must scale back up the solution and the value of the
  //objective
  double scale = b.absMax();
  if (scale < 1.0e2) {
    scale = 1.0;
  } else {
    DBGP("Mosek solver: scaling problem down by " << scale);
  }

  //---------------------------------------
  //insert the actual variables and constraints

  //append the variables
  MSK_append(task, MSK_ACC_VAR, sol.rows());
  //append the constraints.
  MSK_append(task, MSK_ACC_CON, Eq.rows() + InEq.rows());

  int i, j;
  double value;
  if (objType == MOSEK_OBJ_QP) {
    //quadratic optimization objective
    //the quadratic term
    Q.sequentialReset();
    while (Q.nextSequentialElement(i, j, value)) {
      MSK_putqobjij(task, i, j, 2.0 * value);
    }
  } else if (objType == MOSEK_OBJ_LP) {
    //linear objective
    for (j = 0; j < Q.cols(); j++) {
      if (fabs(Q.elem(0, j)) > 1.0e-5) {
        MSK_putcj(task, j, Q.elem(0, j));
      }
    }
  } else {
    assert(0);
  }

  //variable bounds
  assert(sol.rows() == lowerBounds.rows());
  assert(sol.rows() == upperBounds.rows());
  for (i = 0; i < sol.rows(); i++) {
    if (lowerBounds.elem(i, 0) >= upperBounds.elem(i, 0)) {
      if (lowerBounds.elem(i, 0) > upperBounds.elem(i, 0)) {
        assert(0);
      }
      if (lowerBounds.elem(i, 0) == -std::numeric_limits<double>::max()) {
        assert(0);
      }
      if (upperBounds.elem(i, 0) == std::numeric_limits<double>::max()) {
        assert(0);
      }
      //fixed variable
      DBGP(i << ": fixed " << lowerBounds.elem(i, 0) / scale);
      MSK_putbound(task, MSK_ACC_VAR, i, MSK_BK_FX,
                   lowerBounds.elem(i, 0) / scale, upperBounds.elem(i, 0) / scale);
    } else if (lowerBounds.elem(i, 0) != -std::numeric_limits<double>::max()) {
      //finite lower bound
      if (upperBounds.elem(i, 0) != std::numeric_limits<double>::max()) {
        //two finite bounds
        DBGP(i << ": finite bounds " << lowerBounds.elem(i, 0) / scale
             << " " << upperBounds.elem(i, 0) / scale);
        MSK_putbound(task, MSK_ACC_VAR, i, MSK_BK_RA,
                     lowerBounds.elem(i, 0) / scale, upperBounds.elem(i, 0) / scale);
      } else {
        //lower bound
        DBGP(i << ": lower bound " << lowerBounds.elem(i, 0) / scale);
        MSK_putbound(task, MSK_ACC_VAR, i, MSK_BK_LO,
                     lowerBounds.elem(i, 0) / scale, +MSK_INFINITY);

      }
    } else {
      //infinite lower bound
      if (upperBounds.elem(i, 0) != std::numeric_limits<double>::max()) {
        //upper bound
        DBGP(i << ": upper bound " << upperBounds.elem(i, 0) / scale);
        MSK_putbound(task, MSK_ACC_VAR, i, MSK_BK_UP,
                     -MSK_INFINITY, upperBounds.elem(i, 0) / scale);
      } else {
        //unbounded
        DBGP(i << ": unbounded");
        MSK_putbound(task, MSK_ACC_VAR, i, MSK_BK_FR,
                     -MSK_INFINITY, +MSK_INFINITY);

      }
    }
  }

  //constraints and constraint bounds
  //equality constraints
  Eq.sequentialReset();
  while (Eq.nextSequentialElement(i, j, value)) {
    MSK_putaij(task, i, j, value);
  }
  for (i = 0; i < Eq.rows(); i++) {
    MSK_putbound(task, MSK_ACC_CON, i, MSK_BK_FX, b.elem(i, 0) / scale, b.elem(i, 0) / scale);
  }
  //inequality constraints, <=
  InEq.sequentialReset();
  while (InEq.nextSequentialElement(i, j, value)) {
    int eqi = i + Eq.rows();
    MSK_putaij(task, eqi, j, value);
  }
  for (i = 0; i < InEq.rows(); i++) {
    int eqi = i + Eq.rows();
    MSK_putbound(task, MSK_ACC_CON, eqi, MSK_BK_UP, -MSK_INFINITY, ib.elem(i, 0) / scale);
  }
  //specify objective: minimize
  MSK_putobjsense(task, MSK_OBJECTIVE_SENSE_MINIMIZE);

  //give it 800 iterations, twice the default.
  MSK_putintparam(task, MSK_IPAR_INTPNT_MAX_ITERATIONS, 800);

  //----------------------------------

  //solve the thing
  DBGP("Optimization started");
  r = MSK_optimize(task);
  DBGP("Optimization returns");

  //write problem to file
  /*
  static int fileNum = 0;
  if (r != MSK_RES_OK) {
    char filename[50];
    sprintf(filename,"mosek_error_%d_%d.opf",fileNum++, r);
    MSK_writedata(task, filename);
    FILE *fp = fopen(filename,"a");
    fprintf(fp,"\n\nEquality matrix:\n");
    Eq.print(fp);
    fclose(fp);
  }
  */

  if (r != MSK_RES_OK) {
    DBGA("Mosek optimization call failed, error code " << r);
    MSK_deletetask(&task);
    return -1;
  }
  DBGP("Optimization complete");
  //debug code, find out number of iterations used
  //int iter;
  //MSK_getintinf(task, MSK_IINF_INTPNT_ITER, &iter);
  //DBGA("Iterations used: " << iter);

  //find out what kind of solution we have
  MSKprostae pst;
  MSKsolstae sst;
  MSK_getsolutionstatus(task, MSK_SOL_ITR, &pst, &sst);
  int result;
  if (sst == MSK_SOL_STA_OPTIMAL || sst == MSK_SOL_STA_NEAR_OPTIMAL) {
    //success, we have an optimal problem
    if (sst == MSK_SOL_STA_OPTIMAL) {DBGP("QP solution is optimal");}
    else {DBGA("QP solution is *nearly* optimal");}
    result = 0;
  } else if (sst == MSK_SOL_STA_PRIM_INFEAS_CER) {
    //unfeasible problem
    DBGP("Mosek optimization: primal infeasible");
    result = 1;
  } else if (sst == MSK_SOL_STA_DUAL_INFEAS_CER) {
    //unfeasible problem
    DBGA("Mosek optimization: dual infeasible (primal unbounded?)");
    result = 1;
  } else if (sst == MSK_SOL_STA_PRIM_AND_DUAL_FEAS) {
    //i think this means feasible problem, but unbounded solution
    //this shouldn't happen as our Q is positive semidefinite
    DBGA("QP solution is prim and dual feasible, but not optimal");
    DBGA("Is Q positive semidefinite?");
    result = -1;
  } else {
    //unknown return status
    DBGA("QP fails with solution status " << sst << " and problem status " << pst);
    result = -1;
  }

  //MSK_SOL_STA_DUAL_FEAS;

  //retrieve the solutions
  if (!result) {
    //get the value of the objective function
    MSKrealt obj, foo;
    MSK_getsolutioninf(task, MSK_SOL_ITR, &pst, &sst, &obj,
                       &foo, &foo, &foo, &foo, &foo, &foo, &foo, &foo);
    if (objType == MOSEK_OBJ_QP) {
      *objVal = obj * scale * scale;
    } else if (objType == MOSEK_OBJ_LP) {
      *objVal = obj * scale;
    } else {
      assert(0);
    }
    double *xx = new double[sol.rows()];
    MSK_getsolutionslice(task, MSK_SOL_ITR, MSK_SOL_ITEM_XX,
                         0, sol.rows(), xx);
    for (i = 0; i < sol.rows(); i++) {
      sol.elem(i, 0) = scale * xx[i];
      DBGP("x" << i << ": " << xx[i]);
    }
    delete [] xx;
  }
  MSK_deletetask(&task);
  return result;
}
Beispiel #2
0
MSKrescodee put_a(MSKtask_t task,
                   double *aval,
                   MSKidxt *asub,
                   MSKidxt *ptrb,
                   MSKidxt *ptre,
                   int numvar,
                  MSKidxt *basis
                  )

{
  MSKrescodee r = MSK_RES_OK;
  int i;
  MSKstakeye *skx = NULL , *skc = NULL;

  
  skx = (MSKstakeye *) calloc(numvar,sizeof(MSKstakeye));
  if (skx == NULL && numvar)
    r = MSK_RES_ERR_SPACE;
  
  skc = (MSKstakeye *) calloc(numvar,sizeof(MSKstakeye));
  if (skc == NULL && numvar)
    r = MSK_RES_ERR_SPACE;
  
  for (i=0;i<numvar && r == MSK_RES_OK;++i)
  {
    skx[i] = MSK_SK_BAS;
    skc[i] = MSK_SK_FIX;
  }
  
  
  /* Create a coefficient matrix and right hand
     side with the data from the linear system */
  if (r == MSK_RES_OK)
    r = MSK_append(task,MSK_ACC_VAR,numvar);

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

  for (i=0;i<numvar && r == MSK_RES_OK;++i)
    r = MSK_putavec(task,MSK_ACC_VAR,i,ptre[i]-ptrb[i],asub+ptrb[i],aval+ptrb[i]);

  for (i=0;i<numvar && r == MSK_RES_OK;++i)
    r = MSK_putbound(task,MSK_ACC_CON,i,MSK_BK_FX,0,0);
    
  for (i=0;i<numvar && r == MSK_RES_OK;++i)
    r = MSK_putbound(task,MSK_ACC_VAR,i,MSK_BK_FR,-MSK_INFINITY,MSK_INFINITY);
  
  /* Allocate space for the solution and set status to unknown */
  
  if (r == MSK_RES_OK)
  {
    r = MSK_makesolutionstatusunknown(task, MSK_SOL_BAS);
  }

  /* Define a basic solution by specifying
     status keys for variables & constraints. */ 
  for (i=0; i<numvar && r==MSK_RES_OK;++i)
      r = MSK_putsolutioni (
                            task,
                            MSK_ACC_VAR,
                            i,
                            MSK_SOL_BAS, 
                            skx[i], 
                            0.0,
                            0.0,
                            0.0,
                            0.0);
                           
  for (i=0;i<numvar && r == MSK_RES_OK;++i)
      r = MSK_putsolutioni (
                            task,
                            MSK_ACC_CON,
                            i,
                            MSK_SOL_BAS, 
                            skc[i], 
                            0.0,
                            0.0,
                            0.0,
                            0.0);

  
  if (r == MSK_RES_OK)
    r = MSK_initbasissolve(task,basis);

  free (skx);
  free (skc);

  return ( r );
  
}
Beispiel #3
0
int main(int argc,char *argv[])
{
  MSKrescodee  r;
  
  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_LO,
                        MSK_BK_FR,
                        MSK_BK_FR};
  double       blx[] = {0.0,
                        0.0,
                        0.0,
                        0.0,
                        -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,
                        0.0,
                        1.0,
                        1.0};

  MSKintt     aptrb[] = {0, 1, 2, 3, 5, 5};
  MSKintt     aptre[] = {1, 2, 3, 4, 5, 5};
  double      aval[] = {1.0, 1.0, 1.0, 1.0};
  MSKidxt     asub[] = {0, 0, 0, 0};
   
  MSKidxt     i,j,csub[3];
  double      xx[NUMVAR];
  MSKenv_t    env;
  MSKtask_t   task;

  /* Create the mosek environment. */
  r = MSK_makeenv(&env,NULL,NULL,NULL,NULL);
  /* Check if return code is ok. */
  if ( r==MSK_RES_OK )
  {
    /* Directs the log stream to the 
       'printstr' function. */
    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);

    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.*/
                 
      if ( r==MSK_RES_OK )
      {
        /* Append the first cone. */
        csub[0] = 4;
        csub[1] = 0;
        csub[2] = 2;
        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] = 5;
        csub[1] = 1;
        csub[2] = 3;

        r       =  MSK_appendcone(task,
                                  MSK_CT_QUAD,
                                  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_LOG);
        
        if ( r==MSK_RES_OK )
        {
          MSKsolstae solsta;
          MSKidxt    j;
          
          MSK_getsolutionstatus (task,
                                 MSK_SOL_ITR,
                                 NULL,
                                 &solsta);
          
          switch(solsta)
          {
            case MSK_SOL_STA_OPTIMAL:   
            case MSK_SOL_STA_NEAR_OPTIMAL:
              MSK_getsolutionslice(task,
                                   MSK_SOL_ITR,    /* Request the interior 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);
      }
    }
    /* Delete the task and the associated data. */
    MSK_deletetask(&task);
  }
 
  /* Delete the environment and the associated data. */
  MSK_deleteenv(&env);

  return ( r );
} /* main */
Beispiel #4
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 row. */
  MSKlidxt     aptrb[] = {0, 3, 7};
  MSKlidxt     aptre[] = {3, 7, 9};
  MSKidxt      asub[] = { 0,1,2,
                          0,1,2,3,
                          1,3};    
  double       aval[] = { 3.0, 1.0, 2.0,
                          2.0, 1.0, 3.0, 1.0,
                          2.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.*/
    }

    /* 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.*/
      /* Input column j of A */   
      if(r == MSK_RES_OK)
        r = MSK_putavec(task,
                        MSK_ACC_CON,       /* Input row of A.*/
                        i,                 /* Row index.*/
                        aptre[i]-aptrb[i], /* Number of non-zeros in row i.*/
                        asub+aptrb[i],     /* Pointer to column indexes of row i.*/
                        aval+aptrb[i]);    /* Pointer to Values of row i.*/      
    }

    /* 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 #5
0
bool QPSolver::solve(VectorXd& sol)
{
    bool ret = false;
#ifdef _WIN32
    VectorXd solution;
	preSolve();
	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, mCU[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];
				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.";
			}
		}
		if (r == MSK_RES_OK)
		{
			/* Input the Q for the objective . */
			r = MSK_putqobj(task, mQValues.size(), &(mQSubi[0]), &(mQSubj[0]), &(mQValues[0]));
		}

		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);
					for (int k = 0; k < mNumVar; ++k)
						solution[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);
    
	postSolve(solution, ret, sol);
#endif
	return ret;
}
/**********************
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 #8
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 */