int CMyProblem::PrintMIPSolution(ostream &out) { glp_create_index(lp); out << "MIP solution" << endl; out << "Dir;" << ((glp_get_obj_dir(lp)==GLP_MIN) ? "min" : "max") << endl; out << "f;" << glp_mip_obj_val(lp) << endl; out << "Status;" << DecodeStatus(glp_mip_status(lp)) << endl; PrintSolArray(lp,"x",out,true); PrintSolArray(lp,"y",out,true); glp_delete_index(lp); return 0; }
int CMyProblem::PrintLPSolution(ostream &out) { glp_create_index(lp); out << "LP solution" << endl; out << "Dir;" << ((glp_get_obj_dir(lp)==GLP_MIN) ? "min" : "max") << endl; out << "f; " << glp_get_obj_val(lp) << ";/*" << RealResult() << "*/" << endl; out << "Status;" << DecodeStatus(glp_get_status(lp)) << endl; PrintSolArray(lp,"x",out); PrintSolArray(lp,"y",out); glp_delete_index(lp); return 0; }
int main(int argc, char * argv[]) { int i,j; time(&initial); srand(SEED); /* Default values */ outFile = stdout; maxAlpha = 2; maxIter = 100; maxTime = 30; randomSeed = SEED; simpleOutput = 0; /* Read arguments */ if( argc > 7 ) argc = 7; switch(argc) { case 7: simpleOutput = atoi(argv[6]); case 6: if( !(randomSeed = atoi(argv[5])) ) leave(argv[0]); case 5: if( !(maxTime = atoi(argv[4])) ) leave(argv[0]); case 4: if( !(maxIter = atoi(argv[3])) ) leave(argv[0]); case 3: if( !(maxAlpha = atoi(argv[2])) ) leave(argv[0]); case 2: if( simpleOutput ) { if( !(outFile = fopen(argv[1],"a")) ) leave(argv[0]); break; } if( !(outFile = fopen(argv[1],"w")) ) leave(argv[0]); } readInput(stdin); /* Initiate positions */ for( i = 0 ; i < n ; ++i ) { pOrd[i].ideal = planes[i].ideal; pOrd[i].pos = i; } qsort (pOrd, n, sizeof(struct planeOrder), compIdealT); for( i = 0 ; i < n ; ++i ) { planes[pOrd[i].pos].pos = i; } /* Create lp instance */ glp_prob * Prob; Prob = glp_create_prob(); glp_set_prob_name(Prob, "Airplane Landing Problem"); glp_set_obj_name(Prob, "Cost"); /* Create basic constraints */ for( i = 0 ; i < n ; ++i ) { addBasicRestriction(Prob,i); } glp_create_index(Prob); /* Create separation constraints and order variables (&ij) if necessary */ for( i = 0 ; i < n ; ++i ) { for( j = i+1 ; j < n ; ++j ) { if( planes[i].latest >= planes[j].earliest && planes[j].latest >= planes[i].earliest ) { addOrderConstraint(Prob,i,j); } else if ( planes[i].latest < planes[j].earliest && planes[i].latest + planes[i].sep[j] >= planes[j].earliest ) { addSeparationConstraint(Prob, i, j); } else if ( planes[j].latest < planes[i].earliest && planes[j].latest + planes[j].sep[i] >= planes[i].earliest ) { addSeparationConstraint(Prob, j, i); } } } /* Write problem in MPS format so glpsol can (try to) solve it */ glp_write_mps(Prob, GLP_MPS_FILE, NULL,"mpsProblem.txt"); glp_delete_index(Prob); glp_create_index(Prob); /* GRASP */ /* Data to handle glp solving, time checking and solution generating */ glp_smcp * param = malloc(sizeof(glp_smcp)); glp_init_smcp(param); param->msg_lev = GLP_MSG_ERR; int solution[MAXSIZE], timeAux[MAXSIZE], t; double currResult = DBL_MAX, bestResult = DBL_MAX; alpha = 0; time_t start, curr; time(&start); for( t = 0 ; t < maxIter ; ++t ) { /* Greedy solution generation */ while(createSolution(solution,timeAux,0)) alpha = n; /* Building the right constraints */ mapSolution(Prob,solution); /* Solving with glpsol */ param->presolve = GLP_ON; glp_simplex(Prob,param); param->presolve = GLP_OFF; currResult = glp_get_obj_val(Prob); /* Local search using the first increase */ for( i = 0 ; i < n-1 ; ++i ) { /* Swap two adjacent planes */ swapConstraint(Prob,i,solution,0); glp_simplex(Prob,param); /* Check for improvements */ if( GLP_OPT == glp_get_status(Prob) && glp_get_obj_val(Prob) < currResult ) { currResult = glp_get_obj_val(Prob); /* Changing the solution */ int swp; swp = solution[i]; solution[i] = solution[i+1]; solution[i+1] = swp; /* Restarting */ i = -1; } else swapConstraint(Prob,i,solution,1); } /* Checking improvements */ if( bestResult > currResult ) { bestResult = currResult; for( i = 0 ; i < n ; ++i ) planes[solution[i]].pos = i; } /* Choosing alpha */ alpha = rand()%(maxAlpha+1); /* Is our time up? */ time(&curr); if( difftime(curr,start) > maxTime ) break; } /* Print Answer */ printResult(Prob, stdout); if( outFile ) { printResult(Prob, outFile); fclose(outFile); } return 0; }
void lpx_delete_index(LPX *lp) { /* delete the name index */ glp_delete_index(lp); return; }
int glp_read_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname) { /* read problem data in CPLEX LP format */ glp_cpxcp _parm; struct csa _csa, *csa = &_csa; int ret; xprintf("Reading problem data from '%s'...\n", fname); if (parm == NULL) glp_init_cpxcp(&_parm), parm = &_parm; /* check control parameters */ check_parm("glp_read_lp", parm); /* initialize common storage area */ csa->P = P; csa->parm = parm; csa->fname = fname; csa->fp = NULL; if (setjmp(csa->jump)) { ret = 1; goto done; } csa->count = 0; csa->c = '\n'; csa->token = T_EOF; csa->image[0] = '\0'; csa->imlen = 0; csa->value = 0.0; csa->n_max = 100; csa->ind = xcalloc(1+csa->n_max, sizeof(int)); csa->val = xcalloc(1+csa->n_max, sizeof(double)); csa->flag = xcalloc(1+csa->n_max, sizeof(char)); memset(&csa->flag[1], 0, csa->n_max * sizeof(char)); csa->lb = xcalloc(1+csa->n_max, sizeof(double)); csa->ub = xcalloc(1+csa->n_max, sizeof(double)); #if 1 /* 27/VII-2013 */ csa->lb_warn = csa->ub_warn = 0; #endif /* erase problem object */ glp_erase_prob(P); glp_create_index(P); /* open input CPLEX LP file */ csa->fp = glp_open(fname, "r"); if (csa->fp == NULL) { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg()); ret = 1; goto done; } /* scan very first token */ scan_token(csa); /* parse definition of the objective function */ if (!(csa->token == T_MINIMIZE || csa->token == T_MAXIMIZE)) error(csa, "'minimize' or 'maximize' keyword missing\n"); parse_objective(csa); /* parse constraints section */ if (csa->token != T_SUBJECT_TO) error(csa, "constraints section missing\n"); parse_constraints(csa); /* parse optional bounds section */ if (csa->token == T_BOUNDS) parse_bounds(csa); /* parse optional general, integer, and binary sections */ while (csa->token == T_GENERAL || csa->token == T_INTEGER || csa->token == T_BINARY) parse_integer(csa); /* check for the keyword 'end' */ if (csa->token == T_END) scan_token(csa); else if (csa->token == T_EOF) warning(csa, "keyword 'end' missing\n"); else error(csa, "symbol '%s' in wrong position\n", csa->image); /* nothing must follow the keyword 'end' (except comments) */ if (csa->token != T_EOF) error(csa, "extra symbol(s) detected beyond 'end'\n"); /* set bounds of variables */ { int j, type; double lb, ub; for (j = 1; j <= P->n; j++) { lb = csa->lb[j]; ub = csa->ub[j]; if (lb == +DBL_MAX) lb = 0.0; /* default lb */ if (ub == -DBL_MAX) ub = +DBL_MAX; /* default ub */ if (lb == -DBL_MAX && ub == +DBL_MAX) type = GLP_FR; else if (ub == +DBL_MAX) type = GLP_LO; else if (lb == -DBL_MAX) type = GLP_UP; else if (lb != ub) type = GLP_DB; else type = GLP_FX; glp_set_col_bnds(csa->P, j, type, lb, ub); } } /* print some statistics */ xprintf("%d row%s, %d column%s, %d non-zero%s\n", P->m, P->m == 1 ? "" : "s", P->n, P->n == 1 ? "" : "s", P->nnz, P->nnz == 1 ? "" : "s"); if (glp_get_num_int(P) > 0) { int ni = glp_get_num_int(P); int nb = glp_get_num_bin(P); if (ni == 1) { if (nb == 0) xprintf("One variable is integer\n"); else xprintf("One variable is binary\n"); } else { xprintf("%d integer variables, ", ni); if (nb == 0) xprintf("none"); else if (nb == 1) xprintf("one"); else if (nb == ni) xprintf("all"); else xprintf("%d", nb); xprintf(" of which %s binary\n", nb == 1 ? "is" : "are"); } } xprintf("%d lines were read\n", csa->count); /* problem data has been successfully read */ glp_delete_index(P); glp_sort_matrix(P); ret = 0; done: if (csa->fp != NULL) glp_close(csa->fp); xfree(csa->ind); xfree(csa->val); xfree(csa->flag); xfree(csa->lb); xfree(csa->ub); if (ret != 0) glp_erase_prob(P); return ret; }