def findSolutionValues(self, lp): prob = lp.solverModel if self.mip and self.hasMIPConstraints(lp.solverModel): solutionStatus = glpk.glp_mip_status(prob) else: solutionStatus = glpk.glp_get_status(prob) glpkLpStatus = { glpk.GLP_OPT: LpStatusOptimal, glpk.GLP_UNDEF: LpStatusUndefined, glpk.GLP_FEAS: LpStatusNotSolved, glpk.GLP_INFEAS: LpStatusInfeasible, glpk.GLP_NOFEAS: LpStatusInfeasible, glpk.GLP_UNBND: LpStatusUnbounded } #populate pulp solution values for var in lp.variables(): if self.mip and self.hasMIPConstraints(lp.solverModel): var.varValue = glpk.glp_mip_col_val(prob, var.glpk_index) else: var.varValue = glpk.glp_get_col_prim(prob, var.glpk_index) var.dj = glpk.glp_get_col_dual(prob, var.glpk_index) #put pi and slack variables against the constraints for constr in lp.constraints.values(): if self.mip and self.hasMIPConstraints(lp.solverModel): row_val = glpk.glp_mip_row_val(prob, constr.glpk_index) else: row_val = glpk.glp_get_row_prim(prob, constr.glpk_index) constr.slack = -constr.constant - row_val constr.pi = glpk.glp_get_row_dual(prob, constr.glpk_index) lp.resolveOK = True for var in lp.variables(): var.isModified = False lp.status = glpkLpStatus.get(solutionStatus, LpStatusUndefined) return lp.status
def solve_problem(self, problem, solverConfig): if (solverConfig["simplex"] in ["True", "true", "1", "TRUE"]): lp_solver_str = "using simplex" simplex = True else: lp_solver_str = "using interior point" simplex = False if (solverConfig["presolve"] in ["True", "true", "1", "TRUE"]): presolv_str = "with ILP presolver" lp_solver_str = "" presolve = True else: presolv_str = "no ILP presolver" presolve = False logging.info("CSR Optimizer solving problem using %s %s", presolv_str, lp_solver_str) if (False == presolve): if (True == simplex): logging.info("CSR Optimizer solving problem using simplex...") glpk_lp_param = glpki.glp_smcp() glpki.glp_init_smcp(glpk_lp_param) glpk_lp_param.msg_lev = glpki.GLP_MSG_OFF glpki.glp_simplex(problem, glpk_lp_param) res = glpki.glp_get_status(problem) else: logging.info( "CSR Optimizer solving problem using interior point...") glpk_lp_param = glpki.glp_iptcp() glpki.glp_init_iptcp(glpk_lp_param) glpk_lp_param.msg_lev = glpki.GLP_MSG_OFF glpki.glp_interior(problem, glpk_lp_param) res = glpki.glp_ipt_status(problem) if (res == glpki.GLP_OPT): logging.info("Linear optimal") else: logging.warning("Problem is linear unfeasible") return problem glpk_ilp_param = glpki.glp_iocp() glpki.glp_init_iocp(glpk_ilp_param) if (True == presolve): glpk_ilp_param.presolve = glpki.GLP_ON else: glpk_ilp_param.presolve = glpki.GLP_OFF glpk_ilp_param.msg_lev = glpki.GLP_MSG_OFF glpki.glp_intopt(problem, glpk_ilp_param) res = glpki.glp_mip_status(problem) if (res == glpki.GLP_OPT): logging.info("Integer optimal") else: logging.warning("Problem is integer unfeasible") return problem return problem