/* 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); }
int main (int argc, char ** argv) { MSKenv_t env = NULL; MSKtask_t task = NULL; MSKrescodee res = MSK_RES_OK; MSKintt numvar = 0; res = MSK_makeenv(&env, NULL,NULL,NULL,NULL); if (res == MSK_RES_OK) res = MSK_initenv(env); if (res == MSK_RES_OK) res = MSK_maketask(env, 0,0, &task); if (res == MSK_RES_OK) { MSKrealt * c = MSK_calloctask(task, numvar, sizeof(MSKrealt)); MSK_getc(task,c); } if (res == MSK_RES_OK) { MSKrealt * upper_bound = MSK_calloctask(task,8,sizeof(MSKrealt)); MSKrealt * lower_bound = MSK_calloctask(task,8,sizeof(MSKrealt)); MSKboundkeye * bound_key = MSK_calloctask(task,8,sizeof(MSKboundkeye)); MSK_getboundslice(task,MSK_ACC_CON, 2,10, bound_key,lower_bound,upper_bound); } if (res == MSK_RES_OK) { MSKidxt bound_index[] = { 1, 6, 3, 9 }; MSKboundkeye bound_key[] = { MSK_BK_FR, MSK_BK_LO, MSK_BK_UP, MSK_BK_FX }; MSKrealt lower_bound[] = { 0.0, -10.0, 0.0, 5.0 }; MSKrealt upper_bound[] = { 0.0, 0.0, 6.0, 5.0 }; MSK_putboundlist(task,MSK_ACC_CON, 4, bound_index, bound_key,lower_bound,upper_bound); } if (res == MSK_RES_OK) { MSKidxt subi[] = { 1, 3, 5 }; MSKidxt subj[] = { 2, 3, 4 }; MSKrealt cof[] = { 1.1, 4.3, 0.2 }; MSK_putaijlist(task,3, subi,subj,cof); } if (res == MSK_RES_OK) { MSKlintt rowsub[] = { 0, 1, 2, 3 }; MSKlidxt ptrb[] = { 0, 3, 5, 7 }; MSKlidxt ptre[] = { 3, 5, 7, 8 }; MSKlidxt sub[] = { 0, 2, 3, 1, 4, 0, 3, 2 }; MSKrealt cof[] = { 1.1, 1.3, 1.4, 2.2, 2.5, 3.1, 3.4, 4.4 }; MSK_putaveclist (task,MSK_ACC_CON,4, rowsub,ptrb,ptre, sub,cof); } MSK_deletetask(&task); MSK_deleteenv(&env); }