Exemplo n.º 1
0
/*
n: size of b and coords, may be smaller than mskEnv->num_variables if we
have dummy vars
b: coefficients of linear part of optimisation function
coords: optimal y* vector, coord[i] is coordinate of node[i]
*/
void mosek_quad_solve_sep(MosekEnv * mskEnv, int n, float *b,
			  float *coords)
{
    int i, j;
    assert(n <= mskEnv->num_variables + 1);
    for (i = 0; i < n - 1 && mskEnv->r == MSK_RES_OK; i++) {
	mskEnv->r = MSK_putcj(mskEnv->task, i, -2 * b[i + 1]);
    }
    if (mskEnv->r == MSK_RES_OK)
	mskEnv->r = MSK_optimize(mskEnv->task);

    if (mskEnv->r == MSK_RES_OK) {
	MSK_getsolutionslice(mskEnv->task,
			     MSK_SOL_ITR,
			     MSK_SOL_ITEM_XX,
			     0, mskEnv->num_variables, mskEnv->xx);

#ifdef DUMP_CONSTRAINTS
	fprintf(logfile, "Primal solution\n");
#endif
	coords[0] = 0;
	for (j = 1; j <= n; j++) {
#ifdef DUMP_CONSTRAINTS
	    fprintf(logfile, "x[%d]: %.2f\n", j, mskEnv->xx[j - 1]);
#endif
	    coords[j] = -mskEnv->xx[j - 1];
	}
    }
    fprintf(logfile, "Return code: %d\n", mskEnv->r);
}
Exemplo n.º 2
0
/*
b: coefficients of linear part of optimisation function
n: number of nodes
coords: optimal y* vector, coord[i] is coordinate of node[i]
hierarchy_boundaries: y coord of boundaries between levels 
	(ie, solution values for the dummy variables used in constraints)
*/
void mosek_quad_solve_hier(MosekEnv * mskEnv, float *b, int n,
			   float *coords, float *hierarchy_boundaries)
{
    int i, j;
    for (i = 1; i < n && mskEnv->r == MSK_RES_OK; i++) {
	mskEnv->r = MSK_putcj(mskEnv->task, i - 1, -2 * b[i]);
    }
#ifdef DUMP_CONSTRAINTS
    fprintf(logfile, "x0=[");
    for (j = 0; j < mskEnv->num_variables; j++) {
	fprintf(logfile, "%f ", j < n ? b[j] : 0);
    }
    fprintf(logfile, "]\n");
    fprintf(logfile, "f=[");
    double *c = N_GNEW(mskEnv->num_variables, double);
    MSK_getc(mskEnv->task, c);
    for (j = 0; j < mskEnv->num_variables; j++) {
	fprintf(logfile, "%f ", c[j]);
    }
    free(c);
    fprintf(logfile, "]\n");
#endif
    if (mskEnv->r == MSK_RES_OK)
	mskEnv->r = MSK_optimize(mskEnv->task);

    if (mskEnv->r == MSK_RES_OK) {
	MSK_getsolutionslice(mskEnv->task,
			     MSK_SOL_ITR,
			     MSK_SOL_ITEM_XX,
			     0, mskEnv->num_variables, mskEnv->xx);

#ifdef DUMP_CONSTRAINTS
	fprintf(logfile, "Primal solution\n");
#endif
	coords[0] = 0;
	for (j = 0; j < mskEnv->num_variables; ++j) {
#ifdef DUMP_CONSTRAINTS
	    fprintf(logfile, "x[%d]: %.2f\n", j, mskEnv->xx[j]);
#endif
	    if (j < n - 1) {
		coords[j + 1] = -mskEnv->xx[j];
	    } else if (j >= n && j < mskEnv->num_variables - 1) {
		hierarchy_boundaries[j - n] = -mskEnv->xx[j];
	    }
	}
    }
    fprintf(logfile, "Return code: %d\n", mskEnv->r);
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
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;
}
Exemplo n.º 5
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;
}
Exemplo n.º 6
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);
}
Exemplo n.º 7
0
MSKrescodee
MSK_dgosetup(MSKtask_t task,
             MSKintt   numvar,
             MSKintt   numcon,
             MSKintt   t,
             double    *v,
             MSKintt   *p,   
             nlhand_t  *nlh)
{

  MSKintt     j,k;
  MSKrescodee r=MSK_RES_OK;
  MSKenv_t    env;


  nlh[0] = NULL;
  
  MSK_getenv(task,&env);  
  
    /* set up nonlinear part */
  
  if (r == MSK_RES_OK)
  {
    nlh[0] = (nlhand_t) MSK_calloctask(task,1,sizeof(nlhandt));
    if (nlh[0] == NULL)
      r = MSK_RES_ERR_SPACE;
  }  

 
  nlh[0]->p = NULL;
  
  if ( r == MSK_RES_OK )
  {
    nlh[0]->n = numvar;

  
    if ( r==MSK_RES_OK )
    {
      nlh[0]->t = t;
      nlh[0]->task = task;


      if (r == MSK_RES_OK)
      {
        nlh[0]->p = MSK_calloctask(task,nlh[0]->t+1,sizeof(int));
        if (nlh[0]->p == NULL)
          r = MSK_RES_ERR_SPACE;
      }

      if ( r == MSK_RES_OK )
      {
        nlh[0]->p[0] = 0;
        for(k=0; k<nlh[0]->t; ++k)
        {        
          nlh[0]->p[k+1] = nlh[0]->p[k]+p[k];
        }

      

        for(k=0; k<nlh[0]->t; ++k)
        {
          for(j=nlh[0]->p[k]; j<nlh[0]->p[k+1]; ++j)
          {
#if DEBUG
            assert(v[j] > 0);
#endif
            MSK_putcj(task,j,OBJSCAL*log(v[j]));
          }
        }  

          
        if ( nlh[0]->p[nlh[0]->t]==nlh[0]->n )
        {
          /*
           * The problem is now defined
           * and the setup can proceed.
           * Next, the number of Hessian non-zeros
           * is computed.
           */

          nlh[0]->numhesnz = nlh[0]->p[1]-nlh[0]->p[0];
          for(k=1; k<nlh[0]->t; ++k)
          {   
            if (( nlh[0]->p[k+1]-nlh[0]->p[k])>1 )  
            {
              /* If only one term in primal constraint, 
                 the corresponding value in H is zero. 
               */
              nlh[0]->numhesnz += ((nlh[0]->p[k+1]-nlh[0]->p[k])
                                   * (1+nlh[0]->p[k+1]-nlh[0]->p[k]))/2;
            } 
          }
          printf("Number of Hessian non-zeros: %d\n",nlh[0]->numhesnz);

          MSK_putnlfunc(task,nlh[0],dgostruc,dgoeval);
        }
        else
        {
          printf("Incorrect function definition.\n");
          printf("n gathered from the task file: %d\n",nlh[0]->n);
          printf("n computed based on p        : %d\n",nlh[0]->p[nlh[0]->t]);
          r = MSK_RES_ERR_UNKNOWN;
        }
      }
    }
  }     

  if (r == MSK_RES_OK)
    r = MSK_putobjsense(task,MSK_OBJECTIVE_SENSE_MAXIMIZE);
  
  return ( r );
} /* dgosetup */
Exemplo n.º 8
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 */
Exemplo n.º 9
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 */
Exemplo n.º 10
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;
}
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 */
Exemplo n.º 12
0
template <typename _Scalar> typename MosekOpt<_Scalar>::ReturnType
MosekOpt<_Scalar>::
update( bool verbose )
{
    if ( _task != NULL )
    {
        std::cerr << "[" << __func__ << "]: " << "update can only be called once! returning." << std::endl;
        return MSK_RES_ERR_UNKNOWN;
    }

    /* Create the optimization task. */
    if ( MSK_RES_OK == _r )
    {
        _r = MSK_maketask( _env, this->getConstraintCount(), this->getVarCount(), &_task );
        if ( MSK_RES_OK != _r )
            std::cerr << "[" << __func__ << "]: " << "could not create task with " << this->getVarCount() << " vars, and " << this->getConstraintCount() << " constraints" << std::endl;
    }

    // redirect output
    if ( MSK_RES_OK == _r )
    {
        _r = MSK_linkfunctotaskstream( _task, MSK_STREAM_LOG, NULL, mosekPrintStr );
        if ( MSK_RES_OK != _r )
            std::cerr << "[" << __func__ << "]: " << "could not create rewire output to mosekPrintStr(), continuing though..." << std::endl;
    }

    // Append _numCon empty constraints. The constraints will initially have no bounds.
    if ( MSK_RES_OK == _r )
    {
        if ( verbose ) std::cout << "my: MSK_appendcons(_task,"<< this->getConstraintCount() <<");" << std::endl;
        _r = MSK_appendcons( _task, this->getConstraintCount() );
        if ( MSK_RES_OK != _r )
            std::cerr << "[" << __func__ << "]: " << "could not append " << this->getConstraintCount() << " constraints" << std::endl;
    }

    // Append _numVar variables. The variables will initially be fixed at zero (x=0).
    if ( MSK_RES_OK == _r )
    {
        if ( verbose ) std::cout << "my: MSK_appendvars(_task," << this->getVarCount() <<");" << std::endl;
        _r = MSK_appendvars( _task, this->getVarCount() );
        if ( MSK_RES_OK != _r )
            std::cerr << "[" << __func__ << "]: " << "could not append " << this->getVarCount() << " variables" << std::endl;
    }

    // Optionally add a constant term to the objective.
    if ( MSK_RES_OK == _r )
    {
        if ( verbose ) std::cout << "my: MSK_putcfix(_task," << this->getObjectiveBias() << ");" << std::endl;
        _r = MSK_putcfix( _task, this->getObjectiveBias() );
        if ( MSK_RES_OK != _r )
            std::cerr << "[" << __func__ << "]: " << "could not add constant " << this->getObjectiveBias() << " to objective function" << std::endl;
    }

    // set Variables
    for ( size_t j = 0; (j < this->getVarCount()) && (MSK_RES_OK == _r); ++j )
    {
        // set Variable j's Bounds // blx[j] <= x_j <= bux[j]
        if ( MSK_RES_OK == _r )
        {
            _r = MSK_putvarbound( _task,
                                  j,                                                     /* Index of variable.*/
                                  MosekOpt<Scalar>::getBoundTypeCustom( this->getVarBoundType(j) ), /* Bound key.*/
                                  this->getVarLowerBound(j),                             /* Numerical value of lower bound.*/
                                  this->getVarUpperBound(j) );                           /* Numerical value of upper bound.*/

            if ( verbose ) std::cout << "my: MSK_putvarbound(_task," << j << "," << this->getVarBoundType(j) << "," << this->getVarLowerBound(j) << "," << this->getVarUpperBound(j) << ");" << std::endl;
        }

        // set Variable j's Type
        if ( MSK_RES_OK == _r )
        {
            _r = MSK_putvartype( _task, j, MosekOpt<Scalar>::getVarTypeCustom(this->getVarType(j)) );
        }

        // set Variable j's linear coefficient in the objective function
        if ( MSK_RES_OK == _r )
        {
            if ( verbose ) std::cout << "my: putcj(_task," << j << "," << this->getLinObjectives()[j] << ")" << std::endl;
            _r = MSK_putcj( _task, j, this->getLinObjectives()[j] );
        }
    }

    // set Quadratic Objectives
    if ( MSK_RES_OK == _r )
    {
        const int numNonZeros = this->getQuadraticObjectives().size();
        MSKint32t *qsubi = new MSKint32t[numNonZeros],
                  *qsubj = new MSKint32t[numNonZeros];
        double    *qval  = new double[numNonZeros];

        for ( size_t qi = 0; qi != this->getQuadraticObjectives().size(); ++qi )
        {
            qsubi[qi] = this->getQuadraticObjectives()[qi].row();
            qsubj[qi] = this->getQuadraticObjectives()[qi].col();
            qval [qi] = this->getQuadraticObjectives()[qi].value();
        }

        if ( verbose ) std::cout<<"my: putqobj( _task, " << numNonZeros << ",\n";
        for ( size_t vi = 0; vi != numNonZeros; ++vi )
        {
            if ( verbose ) std::cout << qsubi[vi] << "," << qsubj[vi] << ", " << qval[vi] << std::endl;
        }
        if ( verbose ) std::cout << ");" << std::endl;

        _r = MSK_putqobj( _task, numNonZeros, qsubi, qsubj, qval );

        if ( qsubi ) { delete[] qsubi; qsubi = NULL; }
        if ( qsubj ) { delete[] qsubj; qsubj = NULL; }
        if ( qval  ) { delete[] qval ; qval  = NULL; }

        if ( MSK_RES_OK != _r )
            std::cerr << "[" << __func__ << "]: " << "Setting Quadratic Objectives caused error code " << (int)_r << std::endl;
    } // ...Quadratic objective

    // set Linear Constraints
    {
        typename ParentType::SparseMatrix A( this->getLinConstraintsMatrix() );
//        ( this->getConstraintCount(), this->getVarCount() );
//        A.setFromTriplets( this->getLinConstraints().begin(), this->getLinConstraints().end() );
        std::vector<double>         aval;                // Linear constraints coeff matrix (sparse)
        std::vector<int>            asub;                // Linear constraints coeff matrix indices
        std::vector<int>            aptrb, aptre;
        for ( int row = 0; (row < A.outerSize()) && (MSK_RES_OK == _r); ++row )
        {
            // set Constraint Bounds for row
            if ( MSK_RES_OK == _r )
            {
                if ( verbose ) std::cout << "my: MSK_putconbound( _task, " << row << ", "
                                         << MosekOpt<Scalar>::getBoundTypeCustom( this->getConstraintBoundType(row) ) << ", "
                                         << this->getConstraintLowerBound( row ) << ", "
                                         << this->getConstraintUpperBound( row ) << ")"
                                         << std::endl; fflush( stdout );

                _r = MSK_putconbound( _task,
                                      row,                                                          /* Index of constraint.*/
                                      MosekOpt<Scalar>::getBoundTypeCustom(this->getConstraintBoundType(row)), /* Bound key.*/
                                      this->getConstraintLowerBound(row),                           /* Numerical value of lower bound.*/
                                      this->getConstraintUpperBound(row) );                         /* Numerical value of upper bound.*/
            }

            // set Linear Constraint row
            if ( MSK_RES_OK == _r )
            {
                // new line starts at index == current size
                aptrb.push_back( aval.size() );
                // add coeffs from new line
                for ( typename ParentType::SparseMatrix::InnerIterator it(A,row); it; ++it )
                {
                    if ( row != it.row() ) std::cerr << "[" << __func__ << "]: " << "this shouldn't happen" << std::endl;
                    // coeff value
                    aval.push_back( it.value() );  // TODO: A should be a matrix, not a vector...
                    // coeff subscript
                    asub.push_back( it.col() );
                }
                // new line ends at index == new size
                aptre.push_back( aval.size() );

                if ( verbose ) {
                    std::cout << "my: MSK_putarow( _task, "
                              << row << ", "
                              << aptre[row] - aptrb[row] << ", "
                              << *(asub.data() + aptrb[row]) << ", "
                              << *(aval.data() + aptrb[row]) << ");"
                              << std::endl; fflush( stdout );
                }

                _r = MSK_putarow( _task,
                                  row,                 /* Row index.*/
                                  aptre[row] - aptrb[row], /* Number of non-zeros in row i.*/
                                  asub.data() + aptrb[row],     /* Pointer to column indexes of row i.*/
                                  aval.data() + aptrb[row]);    /* Pointer to values of row i.*/
            }
        } // ... for A.rows

        // report error
        if ( MSK_RES_OK != _r )
            std::cerr << "[" << __func__ << "]: " << "Setting Lin constraints caused error code " << (int)_r << std::endl;
    } // ...set Linear Constraints

    // set Quadratic constraints
    if ( verbose ) std::cout << "[" << __func__ << "]: " << "adding q constraints" << std::endl;
    for ( size_t constr_id = 0; (constr_id != this->getQuadraticConstraints().size()) && (MSK_RES_OK == _r); ++constr_id )
    {
        const int numNonZeros = this->getQuadraticConstraints(constr_id).size();

        MSKint32t *qsubi = new MSKint32t[numNonZeros],
                  *qsubj = new MSKint32t[numNonZeros];
        double    *qval  = new double[numNonZeros];

        for ( size_t qi = 0; qi != this->getQuadraticConstraints(constr_id).size(); ++qi )
        {
            qsubi[qi] = this->getQuadraticConstraints(constr_id)[qi].row();
            qsubj[qi] = this->getQuadraticConstraints(constr_id)[qi].col();
            qval [qi] = this->getQuadraticConstraints(constr_id)[qi].value();
        }

        if ( verbose ) std::cout<<"my: MSK_putqonk( _task, " << constr_id << ", " << numNonZeros << ",\n";
        for(size_t vi=0;vi!=numNonZeros;++vi)
        {
            if ( verbose ) std::cout << qsubi[vi] << "," << qsubj[vi] << ", " << qval[vi] << std::endl;
        }
        if ( verbose ) std::cout << "); " << std::endl;

        _r = MSK_putqconk(_task,
                          constr_id,
                          numNonZeros,
                          qsubi,
                          qsubj,
                          qval);

        if ( qsubi ) { delete[] qsubi; qsubi = NULL; }
        if ( qsubj ) { delete[] qsubj; qsubj = NULL; }
        if ( qval  ) { delete[] qval ; qval  = NULL; }

        if ( MSK_RES_OK != _r )
            std::cerr << "[" << __func__ << "]: " << "Setting Quad constraints caused error code " << (int)_r << std::endl;
    } // ...set Quadratic Constraints

    // save to file
    {
        if ( _r == MSK_RES_OK )
        {
            _r = MSK_putintparam( _task, MSK_IPAR_WRITE_DATA_FORMAT, MSK_DATA_FORMAT_LP );
            if ( _r == MSK_RES_OK )
            {
                _r = MSK_writedata( _task, "mosek.lp" );
                if ( _r != MSK_RES_OK )
                {
                    std::cerr << "[" << __func__ << "]: " << "Writedata did not work" << (int)_r << std::endl;
                }
            }
        }
    }

    if ( _r == MSK_RES_OK )
    {
        this->_x.setZero();
        this->_updated = true;
    }

    // return error code
    return _r;
} // ...MosekOpt::update()
Exemplo n.º 13
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;
}
Exemplo n.º 14
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 */