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

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

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

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

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

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

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

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

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

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

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

        r = MSK_optimize(task);

        printf("Done optimizing\n");

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

  return r;
} /* main */
Example #2
0
int main(int argc,char **argv)
{
  MSKenv_t  env;
  MSKtask_t task;
  MSKintt NUMCON = 2;
  MSKintt NUMVAR = 2;
  
  double       c[]    = {1.0, 1.0};
  MSKintt      ptrb[] = {0, 2};
  MSKintt      ptre[] = {2, 3};
  MSKidxt      asub[] = {0, 1,
                        0, 1};
  double aval[] = {1.0, 1.0,
                   2.0, 1.0};
  MSKboundkeye bkc[]  = {MSK_BK_UP,
                       MSK_BK_UP};
  
  double blc[]  = {-MSK_INFINITY,
                   -MSK_INFINITY};
  double buc[]  = {2.0,
                   6.0};
  
  MSKboundkeye  bkx[]  = {MSK_BK_LO,
                          MSK_BK_LO};
  double  blx[]  = {0.0,
                    0.0};
  
  double  bux[]  = {+MSK_INFINITY,
                    +MSK_INFINITY};
  
  
  MSKrescodee       r = MSK_RES_OK;
  MSKidxt       i,nz;
  double    w1[] = {2.0,6.0};
  double    w2[] = {1.0,0.0};
  MSKidxt   sub[] = {0,1};
  MSKidxt   *basis;
    
  if (r == MSK_RES_OK)
    r = MSK_makeenv(&env,NULL,NULL,NULL,NULL);
  
  if ( r==MSK_RES_OK )
    MSK_linkfunctoenvstream(env,MSK_STREAM_LOG,NULL,printstr);
  
  if ( r==MSK_RES_OK )
    r = MSK_initenv(env);
  
  if ( r==MSK_RES_OK )
    r = MSK_makeemptytask(env,&task);
  
  if ( r==MSK_RES_OK )
      MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);  

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

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

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

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

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

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

    /* Print solution and b. */

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

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

  if (r == MSK_RES_OK)
  {
    printf("\nSolution to B^Tx = w2:\n\n");
    /* Print solution and y. */
    for (i=0;i<nz;++i) 
    {    
      if (basis[sub[i]] < NUMCON)     
        printf ("xc%d = %e\n",basis[sub[i]] , w2[sub[i]] );    
      else   
        printf ("x%d = %e\n",basis[sub[i]] - NUMCON , w2[sub[i]] );   
    }
  }
   
   printf("Return code: %d (0 means no error occurred.)\n",r);
   
   return ( r );
   
}/* main */
Example #3
0
int main(int argc,char **argv)
{
  MSKenv_t  env;
  MSKtask_t task;
  MSKrescodee r = MSK_RES_OK;
  MSKintt   numvar = NUMCON;
  MSKintt   numcon = NUMVAR;   /* we must have numvar == numcon */
  int       i,nz;
  double    aval[] = {-1.0,1.0,1.0};
  MSKidxt   asub[] = {1,0,1};
  MSKidxt   ptrb[] = {0,1};
  MSKidxt   ptre[] = {1,3};
  
  MSKidxt   bsub[NUMCON];
  double    b[NUMCON];

  MSKidxt   *basis = NULL;

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

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

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

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