#include #include #include using namespace std; vector solveSimplex() { vector mins; vector maxs; vector i_mins; vector i_maxs; vector signs; vector objectives; vector > left_parts; // here is hardcoded the problem definition left_parts = { {1.04909999999999992,5.07731250000000056,0.17905000000000001,2.54800000000000004,9.30000000000799965,0,0,0,0}, {0.06509999999999999,0.68006249999999990,0.02325000000000000,0.74400000000000011,9.30000000000000071,0,0,0,0}, {0.00820000000000000,3.52856250000000005,0.10291000000000000,0.36899999999999999,0.00000000000400000,0,0,0,0}, {0.97580000000000000,0.86868750000000006,0.05289000000000000,1.43499999999999983,0.00000000000400000,0,0,0,0}, {1,18.75000000000000000,1,10,1,0,0,0,0},{1.04909999999999992,5.07731250000000056,0.17905000000000001,2.54800000000000004,9.30000000000799965,1,0,0,0}, {1.04909999999999992,5.07731250000000056,0.17905000000000001,2.54800000000000004,9.30000000000799965,-1,0,0,0}, {0.06509999999999999,0.68006249999999990,0.02325000000000000,0.74400000000000011,9.30000000000000071,0,1,0,0}, {0.06509999999999999,0.68006249999999990,0.02325000000000000,0.74400000000000011,9.30000000000000071,0,-1,0,0}, {0.00820000000000000,3.52856250000000005,0.10291000000000000,0.36899999999999999,0.00000000000400000,0,0,1,0}, {0.00820000000000000,3.52856250000000005,0.10291000000000000,0.36899999999999999,0.00000000000400000,0,0,-1,0}, {0.97580000000000000,0.86868750000000006,0.05289000000000000,1.43499999999999983,0.00000000000400000,0,0,0,1}, {0.97580000000000000,0.86868750000000006,0.05289000000000000,1.43499999999999983,0.00000000000400000,0,0,0,-1} }; mins = {-80000,-32000,-16000,-32000,0,400,400,160,160,80,80,160,160}; maxs = {80000,32000,16000,32000,800,400,400,160,160,80,80,160,160}; i_mins = {100,8,2,4,4}; i_maxs = {400,16,400,20,30}; signs = {0,0,0,0,1,-1,1,-1,1,-1,1,-1,1}; objectives = {0,0,0,0,0,6,6,6,6}; glp_prob *lp; int *ia = NULL; int *ja = NULL; double *ar = NULL; vector results; lp = glp_create_prob(); glp_set_prob_name(lp, "amounts"); glp_set_obj_dir(lp, GLP_MIN); glp_add_rows(lp, left_parts.size()); for (unsigned int i = 0; i < left_parts.size(); i++) { if (signs[i] == 0) { glp_set_row_bnds(lp, i + 1, GLP_DB, mins[i], maxs[i]); } if (signs[i] == -1) { glp_set_row_bnds(lp, i + 1, GLP_LO, mins[i], mins[i]); } if (signs[i] == 1) { glp_set_row_bnds(lp, i + 1, GLP_UP, maxs[i], maxs[i]); } } glp_add_cols(lp, objectives.size()); for (unsigned int i = 0; i < objectives.size(); i++) { if (i < objectives.size() - 4) { glp_set_obj_coef(lp, i + 1, 0.0); if (i_mins[i] != i_maxs[i]) { glp_set_col_bnds(lp, i + 1, GLP_DB, i_mins[i], i_maxs[i]); } else { glp_set_col_bnds(lp, i + 1, GLP_FX, i_mins[i], i_mins[i]); } glp_set_col_kind(lp, i + 1, GLP_IV); } else { int tot = maxs.size() - 1; glp_set_obj_coef(lp, i + 1, 2/maxs[tot-6] * objectives[i]); glp_set_obj_coef(lp, i + 2, 1/maxs[tot-4] * objectives[i + 1]); glp_set_obj_coef(lp, i + 3, 1/maxs[tot-2] * objectives[i + 2]); glp_set_obj_coef(lp, i + 4, 1/maxs[tot] * objectives[i + 3]); glp_set_col_bnds(lp, i + 1, GLP_LO, 0.0, 0.0); glp_set_col_bnds(lp, i + 2, GLP_LO, 0.0, 0.0); glp_set_col_bnds(lp, i + 3, GLP_LO, 0.0, 0.0); glp_set_col_bnds(lp, i + 4, GLP_LO, 0.0, 0.0); break; } } long long fields_c; fields_c = left_parts.size() * objectives.size(); ia = (int *) malloc((fields_c + 1) * sizeof(int)); ja = (int *) malloc((fields_c + 1) * sizeof(int)); ar = (double *) malloc((fields_c + 1) * sizeof(double)); if (ia == NULL || ja == NULL || ar == NULL) { free(ia); free(ja); free(ar); } long long counter = 1; for (unsigned int i = 0; i < left_parts.size(); i++) { for (unsigned int j = 0; j < objectives.size(); j++) { if (left_parts[i][j] != 0) { ia[counter] = i+1; ja[counter] = j+1; ar[counter] = left_parts[i][j]; counter++; } } } glp_load_matrix(lp, counter-1, ia, ja, ar); glp_write_lp(lp, 0, "problem.lp"); glp_iocp parm; glp_init_iocp(&parm); parm.msg_lev = GLP_MSG_OFF; parm.presolve = GLP_ON; // upon calling glp_intopt here, the program loops forever if (glp_intopt(lp, &parm) == 0) { for (unsigned int i = 0; i < objectives.size() - 4; i++) { results.push_back(glp_mip_col_val(lp, i + 1)); } glp_write_sol(lp, "solution.sol"); } glp_delete_prob(lp); free(ia); free(ja); free(ar); return results; } int main() { solveSimplex(); return 0; }