double CMyProblem::RealResult() { char buff[256]; int i=1; int j=1; double value; int add; int result=0; int col_num=glp_find_col(lp,GenerateColName(buff, "x", i, j)); while( col_num ) { value = glp_get_col_prim(lp,col_num); if ( abs(1.0-value)<AVAIL_ERROR ) { add=j*w[i-1]; } j++; col_num=glp_find_col(lp,GenerateColName(buff, "x", i, j)); if (!col_num) { result+=add; i++; j=1; col_num=glp_find_col(lp,GenerateColName(buff, "x", i, j)); } } return result; }
void printResult(glp_prob * Prob, FILE * out) { int i; char buf[AUXSIZE]; glp_smcp * param = malloc(sizeof(glp_smcp)); glp_init_smcp(param); param->msg_lev = GLP_MSG_ERR; param->presolve = GLP_ON; int solution[MAXSIZE]; for( i = 0 ; i < n ; ++i ) solution[planes[i].pos] = i; mapSolution(Prob,solution); glp_simplex(Prob,param); if( simpleOutput ) { time(&final); fprintf(out,"%i & %i \\\\ \n",(int)glp_get_obj_val(Prob),(int)difftime(final,initial)); return; } fprintf(out,"Best found solution's value: %lf\n\n",glp_get_obj_val(Prob)); double time; for( i = 0 ; i < n ; ++i ) { sprintf(buf,"x%i",solution[i]); time = glp_get_col_prim(Prob, glp_find_col(Prob, buf)); fprintf(out,"The %i-th airplane to arrive is airplane %i, at the time %lf\n", i+1,solution[i]+1,time); } }
/* Maps the order of planes in solution to a list of constraints &ij */ void mapSolution(glp_prob * Prob, int solution[]) { int i,j; char buf[AUXSIZE]; int uij; for( i = 0 ; i < n ; ++i ) for( j = i+1 ; j < n ; ++j ) { sprintf(buf,"u%i,%i",solution[i],solution[j]); if( (uij = glp_find_col(Prob, buf)) ) { glp_set_col_bnds(Prob, uij, GLP_FX, 1, 1); glp_set_col_kind(Prob, uij, GLP_CV); sprintf(buf,"u%i,%i",solution[j],solution[i]); glp_set_col_bnds(Prob, uij=glp_find_col(Prob, buf), GLP_FX, 0, 0); glp_set_col_kind(Prob, uij, GLP_CV); } } }
inline void PrintSolArray(glp_prob* lp, char* name, ostream &out, bool integer=false) { out << name << endl; char buff[256]; int i=1; int j=1; double value; int col_num=glp_find_col(lp,GenerateColName(buff, name, i, j)); while( col_num ) { if (!integer) { value = glp_get_col_prim(lp,col_num); //проверка иксов на целочисленность if ((strcmp(name,"x")==0)&&(IsNotBinary(value))) { out<<"!!! "; } } else { value = glp_mip_col_val(lp,col_num); } out << value << ";"; //допишем коэффициенты ещё //out << "*(" << glp_get_obj_coef(lp,col_num) << ");"; j++; col_num=glp_find_col(lp,GenerateColName(buff, name, i, j)); if (!col_num) { out << endl; i++; j=1; col_num=glp_find_col(lp,GenerateColName(buff, name, i, j)); } } }
/* Swap the arrival of two adjacent planes */ void swapConstraint(glp_prob * Prob, int i, int solution[], int back) { int t,j; static char firstTime = 1; static int u[MAXSIZE][MAXSIZE]; char buf[AUXSIZE]; if( firstTime ) { firstTime = 0; for( t = 0 ; t < n ; ++t ) for( j = i+1 ; j < n ; ++j ) { sprintf(buf,"u%i,%i",t,j); u[t][j] = glp_find_col(Prob, buf); sprintf(buf,"u%i,%i",j,t); u[j][t] = glp_find_col(Prob, buf); } } if( u[solution[i]][solution[i+1]] ) { glp_set_col_bnds(Prob, u[solution[i]][solution[i+1]], GLP_FX, back, 0); glp_set_col_bnds(Prob, u[solution[i+1]][solution[i]], GLP_FX, !back, 0); } }
/* Restriction xj - xi >= Sij */ void addSeparationConstraint(glp_prob * Prob, int plane1, int plane2) { int cardinal, constr[3], i = plane1, j = plane2; double cValues[3]; char buf[AUXSIZE]; cardinal = glp_add_rows(Prob, 1); sprintf(buf,"S%i,%i",i,j); glp_set_row_name(Prob, cardinal, buf); glp_set_row_bnds(Prob, cardinal, GLP_LO, planes[i].sep[j], 0); sprintf(buf,"x%i",j); constr[1] = glp_find_col(Prob, buf); sprintf(buf,"x%i",i); constr[2] = glp_find_col(Prob, buf); cValues[1] = 1; cValues[2] = -1; glp_set_mat_row(Prob, cardinal, 2, constr, cValues); }
inline void GetSolArray(glp_prob* lp, char* name, vector<vector<double>> &arr, bool integer=false) { arr.clear(); char buff[256]; int i=1; int j=1; double value; glp_create_index(lp); int col_num=glp_find_col(lp,GenerateColName(buff, name, i, j)); vector<double> row; while( col_num ) { if (!integer) { value = glp_get_col_prim(lp,col_num); } else { value = glp_mip_col_val(lp,col_num); } row.push_back(value); //cout << value << ";"; j++; col_num=glp_find_col(lp,GenerateColName(buff, name, i, j)); if (!col_num) { arr.push_back(row); //cout << endl; row.clear(); i++; j=1; col_num=glp_find_col(lp,GenerateColName(buff, name, i, j)); } } }
static int find_col(struct csa *csa, char *name) { /* find column by its symbolic name */ int j; j = glp_find_col(csa->P, name); if (j == 0) { /* not found; create new column */ j = glp_add_cols(csa->P, 1); glp_set_col_name(csa->P, j, name); /* enlarge working arrays, if necessary */ if (csa->n_max < j) { int n_max = csa->n_max; int *ind = csa->ind; double *val = csa->val; char *flag = csa->flag; double *lb = csa->lb; double *ub = csa->ub; csa->n_max += csa->n_max; csa->ind = xcalloc(1+csa->n_max, sizeof(int)); memcpy(&csa->ind[1], &ind[1], n_max * sizeof(int)); xfree(ind); csa->val = xcalloc(1+csa->n_max, sizeof(double)); memcpy(&csa->val[1], &val[1], n_max * sizeof(double)); xfree(val); csa->flag = xcalloc(1+csa->n_max, sizeof(char)); memset(&csa->flag[1], 0, csa->n_max * sizeof(char)); memcpy(&csa->flag[1], &flag[1], n_max * sizeof(char)); xfree(flag); csa->lb = xcalloc(1+csa->n_max, sizeof(double)); memcpy(&csa->lb[1], &lb[1], n_max * sizeof(double)); xfree(lb); csa->ub = xcalloc(1+csa->n_max, sizeof(double)); memcpy(&csa->ub[1], &ub[1], n_max * sizeof(double)); xfree(ub); } csa->lb[j] = +DBL_MAX, csa->ub[j] = -DBL_MAX; } return j; }
int lpx_find_col(LPX *lp, const char *name) { /* find column by its name */ return glp_find_col(lp, name); }
/* Restrictions xj - xi >= Sij&ij + (Li - Ej)&ji and xi - xj >= Sji&ji + (Lj - Ei)&ij and &ij + &ji = 1*/ void addOrderConstraint(glp_prob * Prob, int plane1, int plane2) { int cardinal, constr[3], i = plane1, j = plane2; double cValues[3]; char buf[AUXSIZE]; int xi, xj, uij; sprintf(buf,"x%i",i); xi = glp_find_col(Prob, buf); sprintf(buf,"x%i",j); xj = glp_find_col(Prob, buf); uij = glp_add_cols(Prob, 2); sprintf(buf,"u%i,%i",i,j); glp_set_col_name(Prob, uij, buf); glp_set_col_kind(Prob, uij, GLP_BV); sprintf(buf,"u%i,%i",j,i); glp_set_col_name(Prob, uij+1, buf); glp_set_col_kind(Prob, uij+1, GLP_BV); cardinal = glp_add_rows(Prob, 2); sprintf(buf,"S%i,%i",i,j); glp_set_row_name(Prob, cardinal, buf); glp_set_row_bnds(Prob, cardinal, GLP_LO, 0, 0); constr[1] = xj; constr[2] = xi; constr[3] = uij; constr[4] = uij+1; cValues[1] = 1; cValues[2] = -1; cValues[3] = -planes[i].sep[j]; cValues[4] = planes[i].latest - planes[j].earliest; glp_set_mat_row(Prob, cardinal, 4, constr, cValues); sprintf(buf,"S%i,%i",j,i); glp_set_row_name(Prob, cardinal+1, buf); glp_set_row_bnds(Prob, cardinal+1, GLP_LO, 0, 0); constr[1] = xi; constr[2] = xj; constr[3] = uij+1; constr[4] = uij; cValues[1] = 1; cValues[2] = -1; cValues[3] = -planes[j].sep[i]; cValues[4] = planes[j].latest - planes[i].earliest; glp_set_mat_row(Prob, cardinal+1, 4, constr, cValues); cardinal = glp_add_rows(Prob, 1); sprintf(buf,"E%i,%i",i,j); glp_set_row_name(Prob, cardinal, buf); glp_set_row_bnds(Prob, cardinal, GLP_FX, 1, 0); constr[1] = uij; constr[2] = uij+1; cValues[1] = 1; cValues[2] = 1; glp_set_mat_row(Prob, cardinal, 2, constr, cValues); }
int c_glp_find_col(glp_prob *lp, const char *name){ return glp_find_col(lp, name); }
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; }