int engine(int numassets, int numfactors, double *ub, double *lb, double *mu, double *sigma2, double *V, double *F, double lambda) { int retcode = 0; GRBenv *env = NULL; GRBmodel *model = NULL; int n, i, j, k; double *x; int *qrow, *qcol, Nq; double *qval; int *cind; double rhs; char sense; double *cval; int numnonz; char **names, bigname[100]; double expectedreturnval; printf("running solver engine\n"); n = numassets + numfactors; retcode = GRBloadenv(&env, "engine.log"); if (retcode) goto BACK; /* Create initial model */ retcode = GRBnewmodel(env, &model, "factors", n, NULL, NULL, NULL, NULL, NULL); if (retcode) goto BACK; names = (char **) calloc(n, sizeof(char *)); /** next we create the remaining attributes for the n columns **/ x = (double *) calloc (n, sizeof(double)); for(j = 0; j < numassets; j++){ names[j] = (char *)calloc(3, sizeof(char)); if(names[j] == NULL){ retcode = 1; goto BACK; } sprintf(names[j],"x%d", j); } for(j = numassets; j < numassets + numfactors; j++){ names[j] = (char *)calloc(3, sizeof(char)); if(names[j] == NULL){ retcode = 1; goto BACK; } sprintf(names[j],"F%d", j - numassets); } /* initialize variables */ for(j = 0; j < n; j++){ retcode = GRBsetstrattrelement(model, "VarName", j, names[j]); if (retcode) goto BACK; retcode = GRBsetdblattrelement(model, "Obj", j, -mu[j]); if (retcode) goto BACK; retcode = GRBsetdblattrelement(model, "LB", j, lb[j]); if (retcode) goto BACK; retcode = GRBsetdblattrelement(model, "UB", j, ub[j]); if (retcode) goto BACK; } /** next, the quadratic -- there are numassets + numfactors*numfactors nonzeroes: numassets residual variances plus the numfactors x numfactors factor covariance matrix**/ Nq = numassets + numfactors*numfactors; qrow = (int *) calloc(Nq, sizeof(int)); /** row indices **/ qcol = (int *) calloc(Nq, sizeof(int)); /** column indices **/ qval = (double *) calloc(Nq, sizeof(double)); /** values **/ if( ( qrow == NULL) || ( qcol == NULL) || (qval == NULL) ){ printf("could not create quadratic\n"); retcode = 1; goto BACK; } for (j = 0; j < numassets; j++){ qval[j] = lambda*sigma2[j]; qrow[j] = qcol[j] = j; } for (i = 0; i < numfactors; i++){ for (j = 0; j < numfactors; j++){ k = i*numfactors + j; qval[k + numassets] = lambda*F[k]; qrow[k + numassets] = numassets + i; qcol[k + numassets] = numassets + j; } } retcode = GRBaddqpterms(model, Nq, qrow, qcol, qval); if (retcode) goto BACK; /** now we will add one constraint at a time **/ /** we need to have a couple of auxiliary arrays **/ cind = (int *)calloc(n, sizeof(int)); /** n is over the top since no constraint is totally dense; but it's not too bad here **/ cval= (double *)calloc(n, sizeof(double)); if(!cval){ printf("cannot allocate cval\n"); retcode = 2; goto BACK; } for(i = 0; i < numfactors; i++){ for(j = 0; j < numassets; j++){ cval[j] = V[i*numassets + j]; cind[j] = j; } cind[numassets] = /* j */ numassets + i; cval[numassets] = -1; numnonz = numassets + 1; rhs = 0; sense = GRB_EQUAL; sprintf(bigname,"factor%d",i); retcode = GRBaddconstr(model, numnonz, cind, cval, sense, rhs, bigname); if (retcode) goto BACK; } /** sum of x variables = 1 **/ for (j = 0; j < numassets; j++){ cval[j] = 1.0; cind[j] = j; } numnonz = numassets; rhs = 1.0; sense = GRB_EQUAL; /* let's reuse some space */ sprintf(bigname, "sum"); retcode = GRBaddconstr(model, numnonz, cind, cval, sense, rhs, bigname); if (retcode) goto BACK; retcode = GRBupdatemodel(model); if (retcode) goto BACK; /** optional: write the problem **/ retcode = GRBwrite(model, "engine.lp"); if (retcode) goto BACK; retcode = GRBoptimize(model); if (retcode) goto BACK; /** get solution **/ retcode = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, n, x); if(retcode) goto BACK; /** now let's see the values **/ expectedreturnval = 0; for(j = 0; j < numassets; j++){ if( x[j] > 1.0e-09){ printf("%s = %g\n", names[j], x[j]); expectedreturnval += x[j]*mu[j]; } } printf("\n*** expected portfolio return: %g\n", expectedreturnval); GRBfreemodel(model); GRBfreeenv(env); BACK: printf("engine exits with code %d\n", retcode); return retcode; }
int main(int argc, char *argv[]) { GRBenv *masterenv = NULL; GRBmodel *model = NULL; GRBenv *modelenv = NULL; int error = 0; int optimstatus; double objval; if (argc < 2) { fprintf(stderr, "Usage: lp_c filename\n"); exit(1); } /* Create environment */ error = GRBloadenv(&masterenv, "lp.log"); if (error) goto QUIT; /* Read model from file */ error = GRBreadmodel(masterenv, argv[1], &model); if (error) goto QUIT; /* Solve model */ error = GRBoptimize(model); if (error) goto QUIT; /* Capture solution information */ error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus); if (error) goto QUIT; /* If model is infeasible or unbounded, turn off presolve and resolve */ if (optimstatus == GRB_INF_OR_UNBD) { modelenv = GRBgetenv(model); if (!modelenv) { fprintf(stderr, "Error: could not get model environment\n"); goto QUIT; } /* Change parameter on model environment. The model now has a copy of the master environment, so changing the master will no longer affect the model. */ error = GRBsetintparam(modelenv, "PRESOLVE", 0); if (error) goto QUIT; error = GRBoptimize(model); if (error) goto QUIT; error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus); if (error) goto QUIT; } if (optimstatus == GRB_OPTIMAL) { error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval); if (error) goto QUIT; printf("Optimal objective: %.4e\n\n", objval); } else if (optimstatus == GRB_INFEASIBLE) { printf("Model is infeasible\n\n"); error = GRBcomputeIIS(model); if (error) goto QUIT; error = GRBwrite(model, "model.ilp"); if (error) goto QUIT; } else if (optimstatus == GRB_UNBOUNDED) { printf("Model is unbounded\n\n"); } else { printf("Optimization was stopped with status = %d\n\n", optimstatus); } QUIT: /* Error reporting */ if (error) { printf("ERROR: %s\n", GRBgeterrormsg(masterenv)); exit(1); } /* Free model */ GRBfreemodel(model); /* Free environment */ GRBfreeenv(masterenv); return 0; }
int main(int argc, char *argv[]) { GRBenv *env = NULL; GRBmodel *model = NULL; int error = 0; double sol[3]; int ind[3]; double val[3]; double obj[] = {1, 0, 0}; int qrow[3]; int qcol[3]; double qval[3]; int optimstatus; double objval; /* Create environment */ error = GRBloadenv(&env, "qcp.log"); if (error || env == NULL) { fprintf(stderr, "Error: could not create environment\n"); exit(1); } /* Create an empty model */ error = GRBnewmodel(env, &model, "qcp", 0, NULL, NULL, NULL, NULL, NULL); if (error) goto QUIT; /* Add variables */ error = GRBaddvars(model, 3, 0, NULL, NULL, NULL, obj, NULL, NULL, NULL, NULL); if (error) goto QUIT; /* Change sense to maximization */ error = GRBsetintattr(model, GRB_INT_ATTR_MODELSENSE, GRB_MAXIMIZE); if (error) goto QUIT; /* Integrate new variables */ error = GRBupdatemodel(model); if (error) goto QUIT; /* Linear constraint: x + y + z = 1 */ ind[0] = 0; ind[1] = 1; ind[2] = 2; val[0] = 1; val[1] = 1; val[2] = 1; error = GRBaddconstr(model, 3, ind, val, GRB_EQUAL, 1.0, "c0"); if (error) goto QUIT; /* Cone: x^2 + y^2 <= z^2 */ qrow[0] = 0; qcol[0] = 0; qval[0] = 1.0; qrow[1] = 1; qcol[1] = 1; qval[1] = 1.0; qrow[2] = 2; qcol[2] = 2; qval[2] = -1.0; error = GRBaddqconstr(model, 0, NULL, NULL, 3, qrow, qcol, qval, GRB_LESS_EQUAL, 0.0, "qc0"); if (error) goto QUIT; /* Rotated cone: x^2 <= yz */ qrow[0] = 0; qcol[0] = 0; qval[0] = 1.0; qrow[1] = 1; qcol[1] = 2; qval[1] = -1.0; error = GRBaddqconstr(model, 0, NULL, NULL, 2, qrow, qcol, qval, GRB_LESS_EQUAL, 0.0, "qc1"); if (error) goto QUIT; /* Optimize model */ error = GRBoptimize(model); if (error) goto QUIT; /* Write model to 'qcp.lp' */ error = GRBwrite(model, "qcp.lp"); if (error) goto QUIT; /* Capture solution information */ error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus); if (error) goto QUIT; error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval); if (error) goto QUIT; error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol); if (error) goto QUIT; printf("\nOptimization complete\n"); if (optimstatus == GRB_OPTIMAL) { printf("Optimal objective: %.4e\n", objval); printf(" x=%.2f, y=%.2f, z=%.2f\n", sol[0], sol[1], sol[2]); } else if (optimstatus == GRB_INF_OR_UNBD) { printf("Model is infeasible or unbounded\n"); } else { printf("Optimization was stopped early\n"); } QUIT: /* Error reporting */ if (error) { printf("ERROR: %s\n", GRBgeterrormsg(env)); exit(1); } /* Free model */ GRBfreemodel(model); /* Free environment */ GRBfreeenv(env); return 0; }
int wctlp_write(wctlp *lp, const char *fname) { int val = 0; val = GRBwrite(lp->model, fname); CHECK_VAL_GRB(val, "failed GRBwrite", lp->env); return val; }
int main(int argc, char *argv[]) { FILE *fp = NULL; GRBenv *env = NULL; GRBmodel *model = NULL; int board[DIM][DIM]; char inputline[100]; int ind[DIM]; double val[DIM]; double lb[DIM*DIM*DIM]; char vtype[DIM*DIM*DIM]; char *names[DIM*DIM*DIM]; char namestorage[10*DIM*DIM*DIM]; char *cursor; int optimstatus; double objval; int zero = 0; int i, j, v, ig, jg, count; int error = 0; if (argc < 2) { fprintf(stderr, "Usage: sudoku_c datafile\n"); exit(1); } fp = fopen(argv[1], "r"); if (fp == NULL) { fprintf(stderr, "Error: unable to open input file %s\n", argv[1]); exit(1); } for (i = 0; i < DIM; i++) { fgets(inputline, 100, fp); if (strlen(inputline) < 9) { fprintf(stderr, "Error: not enough board positions specified\n"); exit(1); } for (j = 0; j < DIM; j++) { board[i][j] = (int) inputline[j] - (int) '1'; if (board[i][j] < 0 || board[i][j] >= DIM) board[i][j] = -1; } } /* Create an empty model */ cursor = namestorage; for (i = 0; i < DIM; i++) { for (j = 0; j < DIM; j++) { for (v = 0; v < DIM; v++) { if (board[i][j] == v) lb[i*DIM*DIM+j*DIM+v] = 1; else lb[i*DIM*DIM+j*DIM+v] = 0; vtype[i*DIM*DIM+j*DIM+v] = GRB_BINARY; names[i*DIM*DIM+j*DIM+v] = cursor; sprintf(names[i*DIM*DIM+j*DIM+v], "x[%d,%d,%d]", i, j, v+1); cursor += strlen(names[i*DIM*DIM+j*DIM+v]) + 1; } } } /* Create environment */ error = GRBloadenv(&env, "sudoku.log"); if (error) goto QUIT; /* Create new model */ error = GRBnewmodel(env, &model, "sudoku", DIM*DIM*DIM, NULL, lb, NULL, vtype, names); if (error) goto QUIT; /* Each cell gets a value */ for (i = 0; i < DIM; i++) { for (j = 0; j < DIM; j++) { for (v = 0; v < DIM; v++) { ind[v] = i*DIM*DIM + j*DIM + v; val[v] = 1.0; } error = GRBaddconstr(model, DIM, ind, val, GRB_EQUAL, 1.0, NULL); if (error) goto QUIT; } } /* Each value must appear once in each row */ for (v = 0; v < DIM; v++) { for (j = 0; j < DIM; j++) { for (i = 0; i < DIM; i++) { ind[i] = i*DIM*DIM + j*DIM + v; val[i] = 1.0; } error = GRBaddconstr(model, DIM, ind, val, GRB_EQUAL, 1.0, NULL); if (error) goto QUIT; } } /* Each value must appear once in each column */ for (v = 0; v < DIM; v++) { for (i = 0; i < DIM; i++) { for (j = 0; j < DIM; j++) { ind[j] = i*DIM*DIM + j*DIM + v; val[j] = 1.0; } error = GRBaddconstr(model, DIM, ind, val, GRB_EQUAL, 1.0, NULL); if (error) goto QUIT; } } /* Each value must appear once in each subgrid */ for (v = 0; v < DIM; v++) { for (ig = 0; ig < SUBDIM; ig++) { for (jg = 0; jg < SUBDIM; jg++) { count = 0; for (i = ig*SUBDIM; i < (ig+1)*SUBDIM; i++) { for (j = jg*SUBDIM; j < (jg+1)*SUBDIM; j++) { ind[count] = i*DIM*DIM + j*DIM + v; val[count] = 1.0; count++; } } error = GRBaddconstr(model, DIM, ind, val, GRB_EQUAL, 1.0, NULL); if (error) goto QUIT; } } } /* Optimize model */ error = GRBoptimize(model); if (error) goto QUIT; /* Write model to 'sudoku.lp' */ error = GRBwrite(model, "sudoku.lp"); if (error) goto QUIT; /* Capture solution information */ error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus); if (error) goto QUIT; error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval); if (error) goto QUIT; printf("\nOptimization complete\n"); if (optimstatus == GRB_OPTIMAL) printf("Optimal objective: %.4e\n", objval); else if (optimstatus == GRB_INF_OR_UNBD) printf("Model is infeasible or unbounded\n"); else printf("Optimization was stopped early\n"); printf("\n"); QUIT: /* Error reporting */ if (error) { printf("ERROR: %s\n", GRBgeterrormsg(env)); exit(1); } /* Free model */ GRBfreemodel(model); /* Free environment */ GRBfreeenv(env); return 0; }
int main(int argc, char *argv[]) { GRBenv *env = NULL; GRBmodel *model = NULL; int error = 0; double sol[3]; int ind[3]; double val[3]; double obj[3]; char vtype[3]; int optimstatus; double objval; /* Create environment */ error = GRBloadenv(&env, "mip1.log"); if (error || env == NULL) { fprintf(stderr, "Error: could not create environment\n"); exit(1); } /* Create an empty model */ error = GRBnewmodel(env, &model, "mip1", 0, NULL, NULL, NULL, NULL, NULL); if (error) goto QUIT; /* Add variables */ obj[0] = -1; obj[1] = -1; obj[2] = -2; vtype[0] = GRB_BINARY; vtype[1] = GRB_BINARY; vtype[2] = GRB_BINARY; error = GRBaddvars(model, 3, 0, NULL, NULL, NULL, obj, NULL, NULL, vtype, NULL); if (error) goto QUIT; /* Integrate new variables */ error = GRBupdatemodel(model); if (error) goto QUIT; /* First constraint: x + 2 y + 3 z <= 4 */ ind[0] = 0; ind[1] = 1; ind[2] = 2; val[0] = 1; val[1] = 2; val[2] = 3; error = GRBaddconstr(model, 3, ind, val, GRB_LESS_EQUAL, 4.0, "c0"); if (error) goto QUIT; /* Second constraint: x + y >= 1 */ ind[0] = 0; ind[1] = 1; val[0] = 1; val[1] = 1; error = GRBaddconstr(model, 2, ind, val, GRB_GREATER_EQUAL, 1.0, "c1"); if (error) goto QUIT; /* Optimize model */ error = GRBoptimize(model); if (error) goto QUIT; /* Write model to 'mip1.lp' */ error = GRBwrite(model, "mip1.lp"); if (error) goto QUIT; /* Capture solution information */ error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus); if (error) goto QUIT; error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval); if (error) goto QUIT; error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol); if (error) goto QUIT; printf("\nOptimization complete\n"); if (optimstatus == GRB_OPTIMAL) { printf("Optimal objective: %.4e\n", objval); printf(" x=%.0f, y=%.0f, z=%.0f\n", sol[0], sol[1], sol[2]); } else if (optimstatus == GRB_INF_OR_UNBD) { printf("Model is infeasible or unbounded\n"); } else { printf("Optimization was stopped early\n"); } QUIT: /* Error reporting */ if (error) { printf("ERROR: %s\n", GRBgeterrormsg(env)); exit(1); } /* Free model */ GRBfreemodel(model); /* Free environment */ GRBfreeenv(env); return 0; }
void gurobi_write(char *fn) { int error; error = GRBwrite(model, fn); ERRORCHECK(error); }
double solve_glp_grb(glp_prob *mip, wrapper_params *par){ GLPK_out = par->glp_out; GRB_out = par->grb_out; double obj_val; /** GLPK: Generate Variable indexing **/ glp_create_index(mip); /** GLPK: Generate LP **/ glp_write_mps(mip, GLP_MPS_FILE, NULL, "tmp.mps"); /************/ /** GUROBI **/ /************/ retGRB = GRBloadenv(&env, NULL); if (retGRB || env == NULL) { fprintf(stderr, "Error: could not create environment\n"); exit(1); } retGRB = GRBsetintparam(env, "OutputFlag", GRB_out?1:0); if (retGRB) freeMem(); //retGRB = GRBsetintparam(env, "Sensitivity", 1); //if (retGRB) freeMem(); /** GUROBI: Read model **/ retGRB = GRBreadmodel(env, "tmp.mps", &model); if (retGRB) freeMem(); /** Remove utility files from disk **/ //remove("tmp.mps"); /** GUROBI: Get environment **/ mipenv = GRBgetenv(model); if (!mipenv) freeMem(); /** GUROBI: Set parameters **/ /** GUROBI: Ask for more precision **/ retGRB = GRBsetdblparam(mipenv, "FeasibilityTol", 10E-6); if (retGRB) freeMem(); retGRB = GRBsetdblparam(mipenv, "IntFeasTol", 10E-5); if (retGRB) freeMem(); retGRB = GRBsetdblparam(mipenv, "MIPgap", 10E-6); if (retGRB) freeMem(); /* * Playing with gurobi parameters and attr*/ //gurobi_set_basis(); retGRB = GRBsetintparam(mipenv, "Cuts", 3); if (retGRB) freeMem(); retGRB = GRBsetintparam(mipenv, "RootMethod", 1); if (retGRB) freeMem(); retGRB = GRBsetintparam(mipenv, "Symmetry", -1); if (retGRB) freeMem(); /** GUROBI: get numvars and numrows **/ retGRB = GRBgetintattr(model, "NumVars", &numvars); if (retGRB) freeMem(); /** Test variable names */ for(int j=0;j<numvars;j++){ retGRB = GRBgetstrattrelement(model, "VarName", j, &nameGRB); printf("GRB Var %d Name %s\n",j,nameGRB); } /** GUROBI: get model type **/ retGRB = GRBgetintattr(model, "IsMIP", &GRB_IsMIP); if (retGRB) freeMem(); /** GUROBI: Optimize model **/ retGRB = GRBoptimize(model); if (retGRB) freeMem(); /** GUROBI: Retreive the optimization status **/ GRBgetintattr(model, "Status", &retGRB); switch(retGRB){ case GRB_OPTIMAL: break; case GRB_INFEASIBLE : fprintf(stderr, "Error GRB optimization failed with code GRB_INFEASIBLE\n"); case GRB_INF_OR_UNBD : fprintf(stderr, "Error GRB optimization failed with code GRB_INF_OR_UNBD \n"); case GRB_UNBOUNDED : fprintf(stderr, "Error GRB optimization failed with code GRB_UNBOUNDED \n"); case GRB_CUTOFF : fprintf(stderr, "Error GRB optimization failed with code GRB_CUTOFF \n"); case GRB_ITERATION_LIMIT : fprintf(stderr, "Error GRB optimization failed with code GRB_ITERATION_LIMIT \n"); case GRB_NODE_LIMIT : fprintf(stderr, "Error GRB optimization failed with code GRB_NODE_LIMIT \n"); case GRB_TIME_LIMIT : fprintf(stderr, "Error GRB optimization failed with code GRB_TIME_LIMIT \n"); case GRB_SOLUTION_LIMIT : fprintf(stderr, "Error GRB optimization failed with code GRB_SOLUTION_LIMIT \n"); case GRB_INTERRUPTED : fprintf(stderr, "Error GRB optimization failed with code GRB_INTERRUPTED \n"); case GRB_SUBOPTIMAL : fprintf(stderr, "Error GRB optimization failed with code GRB_SUBOPTIMAL \n"); case GRB_NUMERIC : fprintf(stderr, "Error GRB optimization failed with code GRB_NUMERIC \n"); /** GUROBI: Quit in any case non optimal **/ freeMem(); } /** GUROBI: Get obj function value **/ retGRB = GRBgetdblattr(model, "IntVio", &tmp); if (retGRB) freeMem(); retGRB = GRBgetdblattr(model, "ObjBound", &bound); if (retGRB) freeMem(); retGRB = GRBgetdblattr(model, "ObjVal", &tmp); if (retGRB) freeMem(); /* ********************** */ obj_val = tmp; /* ************ */ if (verbose) printf ("Objective %lf\n", tmp); if (verbose) printf ("Best bound %lf\n", bound); if (verbose) printf ("Absolute gap %lf\n", fabs(tmp - bound)); /** GUROBI: Get variable values **/ for (j = 0; j < numvars; ++j){ retGRB = GRBgetdblattrelement(model, "X", j, &tmp); if (retGRB) freeMem(); retGRB = GRBgetstrattrelement(model, "VarName", j, &nameGRB); printf("GRB Var %d Name %s\n",j,nameGRB); if (retGRB) freeMem(); retGRB = GRBgetcharattrelement(model, "VType", j, &type); if (retGRB) freeMem(); /** GLPK search variable index by name **/ col_index = glp_find_col(mip, nameGRB); if (col_index != 0){ /** GLPK set variable bounds **/ if ((type == 'B') || (type == 'I')){ if (verbose) printf ("Variable %s is of type %c value %lf fixed to %lf\n", nameGRB, type, tmp, round(tmp)); glp_set_col_bnds(mip, col_index, GLP_FX, round(tmp), round(tmp)); } else{ if (verbose) printf ("Variable %s is of type %c value %lf fixed to %lf\n", nameGRB, type, tmp, tmp); glp_set_col_bnds(mip, col_index, GLP_FX, tmp, tmp); } } } if (GRB_IsMIP){ /** GLPK initialize parameters **/ iparm = (glp_iocp*) malloc(sizeof(glp_iocp)); glp_init_iocp(iparm); iparm->presolve = GLP_ON; iparm->mip_gap = glpk_iparm_mip_gap; iparm->tol_int = glpk_iparm_tol_int; iparm->tol_obj = glpk_iparm_tol_obj; /** GLPK get the optimal integer solution **/ ret = glp_intopt(mip, iparm); if (ret){ fprintf(stderr, "glp_intopt, Error on optimizing the model : %d \n", ret); freeMem(); } ret = glp_mip_status(mip); switch (ret){ case GLP_OPT: break; case GLP_FEAS: fprintf(stderr, "Error GLPK simplex is not optimal, GLP_FEAS, code %d\n", ret); freeMem(); case GLP_NOFEAS: fprintf(stderr, "Error GLPK simplex is not optimal, GLP_NOFEAS, code %d\n", ret); freeMem(); case GLP_UNDEF: fprintf(stderr, "Error GLPK simplex is not optimal, GLP_UNDEF, code %d\n", ret); freeMem(); } } else{ /*GLPK initialize parameters */ parm = (glp_smcp*) malloc(sizeof(glp_smcp)); glp_init_smcp(parm); parm->meth = GLP_DUALP; parm->tol_bnd = 10E-4; parm->tol_dj = 10E-4; /* GLPK get the optimal basis */ //ret = glp_simplex(mip, parm); if (ret){ fprintf(stderr, "glp_simplex, Error on optimizing the model : %d \n", ret); freeMem(); } ret = glp_get_status(mip); switch (ret){ case GLP_OPT: break; case GLP_FEAS: fprintf(stderr, "Error GLPK simplex is not optimal, GLP_FEAS, code %d\n", ret); freeMem(); case GLP_INFEAS: fprintf(stderr, "Error GLPK simplex is not optimal, GLP_INFEAS, code %d\n", ret); freeMem(); case GLP_NOFEAS: fprintf(stderr, "Error GLPK simplex is not optimal, GLP_NOFEAS, code %d\n", ret); freeMem(); case GLP_UNBND: fprintf(stderr, "Error GLPK simplex is not optimal, GLP_UNBND, code %d\n", ret); freeMem(); case GLP_UNDEF: fprintf(stderr, "Error GLPK simplex is not optimal, GLP_UNDEF, code %d\n", ret); freeMem(); } } //GRBmodel *fmod = fixed_model(model); //gurobi_sens_output(fmod, "/tmp/sens.sol"); GRBwrite(model, "/tmp/model.sol"); /** GUROBI: free structures **/ if (model) GRBfreemodel(model); if (env) GRBfreeenv(env); return obj_val; }
int main(void) { int retcode = 0; GRBenv *env = NULL; GRBmodel *model = NULL; int n, j; double *obj = NULL; double *lb = NULL; double *ub = NULL; double *x; int *qrow, *qcol, Nq; double *qval; int *cind; double rhs; char sense; double *cval; int numnonz; char **names; n = 9; /** 7 'x' variables, 2 factor variables **/ retcode = GRBloadenv(&env, "factormodel.log"); if (retcode) goto BACK; /* Create initial model */ retcode = GRBnewmodel(env, &model, "second", n, NULL, NULL, NULL, NULL, NULL); if (retcode) goto BACK; names = (char **) calloc(n, sizeof(char *)); /** next we create the remaining attributes for the n columns **/ obj = (double *) calloc (n, sizeof(double)); ub = (double *) calloc (n, sizeof(double)); lb = (double *) calloc (n, sizeof(double)); x = (double *) calloc (n, sizeof(double)); for(j = 0; j < 7; j++){ names[j] = (char *)calloc(3, sizeof(char)); if(names[j] == NULL){ retcode = 1; goto BACK; } sprintf(names[j],"x%d", j); } for(j = 7; j < 9; j++){ names[j] = (char *)calloc(3, sizeof(char)); if(names[j] == NULL){ retcode = 1; goto BACK; } sprintf(names[j],"y%d", j - 7); } obj[0] = -.233; obj[1] = -3.422; obj[2] = -.1904; obj[3] = -.5411; obj[4] = -.045; obj[5] = -1.271; obj[6] = -0.955; /** calloc initializes memory to zero, so all other obj[j] are zero **/ /**next, the upper bounds on the x variables **/ ub[0] = 0.6; ub[1] = 0.8; ub[2] = 0.8; ub[3] = 0.5; ub[4] = 0.5; ub[5] = 0.26; ub[6] = 0.99; /** the upper bounds on the two factor variables -- we make them large **/ ub[7] = 100; ub[8] = 100; /** the lower bounds on the factor variables **/ lb[7] = -100; lb[8] = -100; /* initialize variables */ for(j = 0; j < n; j++){ retcode = GRBsetstrattrelement(model, "VarName", j, names[j]); if (retcode) goto BACK; retcode = GRBsetdblattrelement(model, "Obj", j, obj[j]); if (retcode) goto BACK; retcode = GRBsetdblattrelement(model, "LB", j, lb[j]); if (retcode) goto BACK; retcode = GRBsetdblattrelement(model, "UB", j, ub[j]); if (retcode) goto BACK; } /** next, the quadratic -- there are 11 nonzeroes: 7 residual variances plus the 2x2 factor covariance matrix**/ Nq = 11; qrow = (int *) calloc(Nq, sizeof(int)); /** row indices **/ qcol = (int *) calloc(Nq, sizeof(int)); /** column indices **/ qval = (double *) calloc(Nq, sizeof(double)); /** values **/ if( ( qrow == NULL) || ( qcol == NULL) || (qval == NULL) ){ printf("could not create quadratic\n"); retcode = 1; goto BACK; } qval[0] = 10.0; qrow[0] = 0; qcol[0] = 0; qval[1] = 20.0; qrow[1] = 1; qcol[1] = 1; qval[2] = 30.0; qrow[2] = 2; qcol[2] = 2; qval[3] = 40.0; qrow[3] = 3; qcol[3] = 3; qval[4] = 50.0; qrow[4] = 4; qcol[4] = 4; qval[5] = 60.0; qrow[5] = 5; qcol[5] = 5; qval[6] = 70.0; qrow[6] = 6; qcol[6] = 6; qval[7] = 100.0; qrow[7] = 7; qcol[7] = 7; qval[8] = 200.0; qrow[8] = 8; qcol[8] = 8; qval[9] = 0.1; qrow[9] = 7; qcol[9] = 8; qval[10] = 0.1; qrow[10] = 8; qcol[10] = 7; retcode = GRBaddqpterms(model, 11, qrow, qcol, qval); if (retcode) goto BACK; /** now we will add one constraint at a time **/ /** we need to have a couple of auxiliary arrays **/ cind = (int *)calloc(n, sizeof(int)); /** n is over the top since no constraint is totally dense; but it's not too bad here **/ cval= (double *)calloc(n, sizeof(double)); /** two factor constraints, first one is next**/ cval[0] = 1.508; cval[1] = .7802; cval[2] = 1.8796; cval[3] = 4.256; cval[4] = 1.335; cval[5] = 2.026; cval[6] = 1.909; cval[7] = -1; for(j = 0; j < 7; j++) cind[j] = j; cind[7] = 7; numnonz = 8; rhs = 0; sense = GRB_EQUAL; retcode = GRBaddconstr(model, numnonz, cind, cval, sense, rhs, "first_constraint"); if (retcode) goto BACK; /** second factor constraint **/ cval[0] = 4.228; cval[1] = 1.2945; cval[2] = .827; cval[3] = 2.149; cval[4] = 2.353; cval[5] = 0.3026; cval[6] = 1.487; cval[7] = -1; for(j = 0; j < 7; j++) cind[j] = j; /** redundant! but let's keep it here so that we know it will be used **/ cind[7] = 7; numnonz = 8; rhs = 0; sense = GRB_EQUAL; retcode = GRBaddconstr(model, numnonz, cind, cval, sense, rhs, "second_constraint"); if (retcode) goto BACK; /** sum of x variables = 1 **/ cval[0] = 1.0; cval[1] = 1.0; cval[2] = 1.0; cval[3] = 1.0; cval[4] = 1.0; cval[5] = 1.0; cval[6] = 1.0; for(j = 0; j < 7; j++) cind[j] = j; numnonz = 7; rhs = 1.0; sense = GRB_EQUAL; retcode = GRBaddconstr(model, numnonz, cind, cval, sense, rhs, "convexity"); if (retcode) goto BACK; retcode = GRBupdatemodel(model); if (retcode) goto BACK; /** optional: write the problem **/ retcode = GRBwrite(model, "factorqp.lp"); if (retcode) goto BACK; retcode = GRBoptimize(model); if (retcode) goto BACK; /** get solution **/ retcode = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, n, x); if(retcode) goto BACK; /** now let's see the values **/ for(j = 0; j < n; j++){ printf("%s = %g\n", names[j], x[j]); } GRBfreemodel(model); GRBfreeenv(env); BACK: printf("\nexiting with retcode %d\n", retcode); return retcode; }