def format_results(self, results_dict, data, cached_data): """Converts the solver output into standard form. Parameters ---------- results_dict : dict The solver output. data : dict Information about the problem. cached_data : dict A map of solver name to cached problem data. Returns ------- dict The solver output in standard form. """ new_results = {} status = self.STATUS_MAP[results_dict['status']] new_results[s.STATUS] = status if new_results[s.STATUS] in s.SOLUTION_PRESENT: # No dual variables. new_results[s.PRIMAL] = intf.cvxopt2dense(results_dict['x']) primal_val = (data[s.C].T * results_dict['x'])[0] new_results[s.VALUE] = primal_val + data[s.OFFSET] return new_results
def format_results(self, results_dict, data, cached_data): """Converts the solver output into standard form. Parameters ---------- results_dict : dict The solver output. data : dict Information about the problem. cached_data : dict A map of solver name to cached problem data. Returns ------- dict The solver output in standard form. """ import cvxopt new_results = {} status = self.STATUS_MAP[results_dict['status']] new_results[s.STATUS] = status if new_results[s.STATUS] in s.SOLUTION_PRESENT: primal_val = results_dict['primal objective'] new_results[s.VALUE] = primal_val + data[s.OFFSET] new_results[s.PRIMAL] = results_dict['x'] new_results[s.EQ_DUAL] = results_dict['y'] if data[s.DIMS][s.EXP_DIM]: new_results[s.INEQ_DUAL] = results_dict['zl'] else: new_results[s.INEQ_DUAL] = results_dict['z'] # Need to multiply duals by Q and P_leq. if "Q" in data: y = results_dict['y'] # Test if all constraints eliminated. if y.size[0] == 0: dual_len = data["Q"].size[0] new_results[s.EQ_DUAL] = cvxopt.matrix(0., (dual_len, 1)) else: new_results[s.EQ_DUAL] = data["Q"] * y if "P_leq" in data: leq_len = data[s.DIMS][s.LEQ_DIM] P_rows = data["P_leq"].size[0] new_len = P_rows + new_results[s.INEQ_DUAL].size[0] - leq_len new_dual = cvxopt.matrix(0., (new_len, 1)) z = new_results[s.INEQ_DUAL][:leq_len] # Test if all constraints eliminated. if z.size[0] == 0: new_dual[:P_rows] = 0 else: new_dual[:P_rows] = data["P_leq"] * z new_dual[P_rows:] = new_results[s.INEQ_DUAL][leq_len:] new_results[s.INEQ_DUAL] = new_dual for key in [s.PRIMAL, s.EQ_DUAL, s.INEQ_DUAL]: new_results[key] = intf.cvxopt2dense(new_results[key]) return new_results
def format_results(self, results_dict, data, cached_data): """Converts the solver output into standard form. Parameters ---------- results_dict : dict The solver output. data : dict Information about the problem. cached_data : dict A map of solver name to cached problem data. Returns ------- dict The solver output in standard form. """ import cvxopt new_results = {} status = self.STATUS_MAP[results_dict['status']] new_results[s.STATUS] = status if new_results[s.STATUS] in s.SOLUTION_PRESENT: primal_val = results_dict['primal objective'] new_results[s.VALUE] = primal_val + data[s.OFFSET] new_results[s.PRIMAL] = results_dict['x'] new_results[s.EQ_DUAL] = results_dict['y'] if data[s.DIMS][s.EXP_DIM]: new_results[s.INEQ_DUAL] = results_dict['zl'] else: new_results[s.INEQ_DUAL] = results_dict['z'] # Need to multiply duals by Q and P_leq. if "Q" in data: y = results_dict['y'] # Test if all constraints eliminated. if y.size[0] == 0: dual_len = data["Q"].size[0] new_results[s.EQ_DUAL] = cvxopt.matrix(0., (dual_len, 1)) else: new_results[s.EQ_DUAL] = data["Q"]*y if "P_leq" in data: leq_len = data[s.DIMS][s.LEQ_DIM] P_rows = data["P_leq"].size[0] new_len = P_rows + new_results[s.INEQ_DUAL].size[0] - leq_len new_dual = cvxopt.matrix(0., (new_len, 1)) z = new_results[s.INEQ_DUAL][:leq_len] # Test if all constraints eliminated. if z.size[0] == 0: new_dual[:P_rows] = 0 else: new_dual[:P_rows] = data["P_leq"]*z new_dual[P_rows:] = new_results[s.INEQ_DUAL][leq_len:] new_results[s.INEQ_DUAL] = new_dual for key in [s.PRIMAL, s.EQ_DUAL, s.INEQ_DUAL]: new_results[key] = intf.cvxopt2dense(new_results[key]) return new_results
def solve_via_data(self, data, warm_start: bool, verbose: bool, solver_opts, solver_cache=None): import cvxopt.solvers # Save original cvxopt solver options. old_options = cvxopt.solvers.options.copy() # Silence cvxopt if verbose is False. if verbose: cvxopt.solvers.options["msg_lev"] = "GLP_MSG_ON" else: if "glpk" in solver_opts: solver_opts["glpk"]["msg_lev"] = "GLP_MSG_OFF" else: solver_opts["glpk"] = {"msg_lev": "GLP_MSG_OFF"} data = self._prepare_cvxopt_matrices(data) # Apply any user-specific options. # Rename max_iters to maxiters. if "max_iters" in solver_opts: solver_opts["maxiters"] = solver_opts["max_iters"] for key, value in solver_opts.items(): cvxopt.solvers.options[key] = value try: results_dict = cvxopt.solvers.lp(data[s.C], data[s.G], data[s.H], data[s.A], data[s.B], solver="glpk") # Catch exceptions in CVXOPT and convert them to solver errors. except ValueError: results_dict = {"status": "unknown"} # Restore original cvxopt solver options. self._restore_solver_options(old_options) # Convert CVXOPT results to solution format. solution = {} status = self.STATUS_MAP[results_dict['status']] solution[s.STATUS] = status if solution[s.STATUS] in s.SOLUTION_PRESENT: primal_val = results_dict['primal objective'] solution[s.VALUE] = primal_val solution[s.PRIMAL] = results_dict['x'] solution[s.EQ_DUAL] = results_dict['y'] solution[s.INEQ_DUAL] = results_dict['z'] for key in [s.PRIMAL, s.EQ_DUAL, s.INEQ_DUAL]: solution[key] = intf.cvxopt2dense(solution[key]) return solution
def solve_via_data(self, data, warm_start: bool, verbose: bool, solver_opts, solver_cache=None): import cvxopt import cvxopt.solvers # Save original cvxopt solver options. old_options = cvxopt.glpk.options.copy() # Silence cvxopt if verbose is False. if verbose: cvxopt.glpk.options["msg_lev"] = "GLP_MSG_ON" else: cvxopt.glpk.options["msg_lev"] = "GLP_MSG_OFF" data = self._prepare_cvxopt_matrices(data) # Apply any user-specific options. # Rename max_iters to maxiters. if "max_iters" in solver_opts: solver_opts["maxiters"] = solver_opts["max_iters"] for key, value in solver_opts.items(): cvxopt.glpk.options[key] = value try: results_tup = cvxopt.glpk.ilp( data[s.C], data[s.G], data[s.H], data[s.A], data[s.B], set(int(i) for i in data[s.INT_IDX]), set(int(i) for i in data[s.BOOL_IDX])) results_dict = {} results_dict["status"] = results_tup[0] results_dict["x"] = results_tup[1] # Catch exceptions in CVXOPT and convert them to solver errors. except ValueError: results_dict = {"status": "unknown"} # Restore original cvxopt solver options. self._restore_solver_options(old_options) # Convert CVXOPT results to solution format. solution = {} status = self.STATUS_MAP[results_dict['status']] solution[s.STATUS] = status if solution[s.STATUS] in s.SOLUTION_PRESENT: # No dual variables. solution[s.PRIMAL] = intf.cvxopt2dense(results_dict['x']) primal_val = (data[s.C].T * results_dict['x'])[0] solution[s.VALUE] = primal_val return solution
def solve_via_data(self, data, warm_start, verbose, solver_opts, solver_cache=None): import cvxopt.solvers # Save original cvxopt solver options. old_options = cvxopt.solvers.options.copy() # Save old data in case need to use robust solver. data[s.DIMS] = dims_to_solver_dict(data[s.DIMS]) # User chosen KKT solver option. kktsolver = self.get_kktsolver_opt(solver_opts) # Cannot have redundant rows unless using robust LDL kktsolver. if kktsolver != s.ROBUST_KKTSOLVER: # Will detect infeasibility. if self.remove_redundant_rows(data) == s.INFEASIBLE: return {s.STATUS: s.INFEASIBLE} # Convert A, b, G, h, c to CVXOPT matrices. data[s.C] = intf.dense2cvxopt(data[s.C]) var_length = data[s.C].size[0] if data[s.A] is None: data[s.A] = np.zeros((0, var_length)) data[s.B] = np.zeros((0, 1)) data[s.A] = intf.sparse2cvxopt(data[s.A]) data[s.B] = intf.dense2cvxopt(data[s.B]) if data[s.G] is None: data[s.G] = np.zeros((0, var_length)) data[s.H] = np.zeros((0, 1)) data[s.G] = intf.sparse2cvxopt(data[s.G]) data[s.H] = intf.dense2cvxopt(data[s.H]) # Apply any user-specific options. # Silence solver. solver_opts["show_progress"] = verbose # Rename max_iters to maxiters. if "max_iters" in solver_opts: solver_opts["maxiters"] = solver_opts["max_iters"] for key, value in solver_opts.items(): cvxopt.solvers.options[key] = value # Always do 1 step of iterative refinement after solving KKT system. if "refinement" not in cvxopt.solvers.options: cvxopt.solvers.options["refinement"] = 1 try: if kktsolver == s.ROBUST_KKTSOLVER: # Get custom kktsolver. kktsolver = get_kktsolver(data[s.G], data[s.DIMS], data[s.A]) results_dict = cvxopt.solvers.conelp(data[s.C], data[s.G], data[s.H], data[s.DIMS], data[s.A], data[s.B], kktsolver=kktsolver) # Catch exceptions in CVXOPT and convert them to solver errors. except ValueError: results_dict = {"status": "unknown"} # Restore original cvxopt solver options. self._restore_solver_options(old_options) # Construct solution. solution = {} status = self.STATUS_MAP[results_dict['status']] solution[s.STATUS] = status if solution[s.STATUS] in s.SOLUTION_PRESENT: primal_val = results_dict['primal objective'] solution[s.VALUE] = primal_val solution[s.PRIMAL] = results_dict['x'] solution[s.EQ_DUAL] = results_dict['y'] solution[s.INEQ_DUAL] = results_dict['z'] # Need to multiply duals by Q and P_leq. if "Q" in data: y = results_dict['y'] # Test if all constraints eliminated. if y.size[0] == 0: dual_len = data["Q"].size[0] solution[s.EQ_DUAL] = cvxopt.matrix(0., (dual_len, 1)) else: solution[s.EQ_DUAL] = data["Q"]*y if "P_leq" in data: leq_len = data[s.DIMS][s.LEQ_DIM] P_rows = data["P_leq"].size[0] new_len = P_rows + solution[s.INEQ_DUAL].size[0] - leq_len new_dual = cvxopt.matrix(0., (new_len, 1)) z = solution[s.INEQ_DUAL][:leq_len] # Test if all constraints eliminated. if z.size[0] == 0: new_dual[:P_rows] = 0 else: new_dual[:P_rows] = data["P_leq"] * z new_dual[P_rows:] = solution[s.INEQ_DUAL][leq_len:] solution[s.INEQ_DUAL] = new_dual for key in [s.PRIMAL, s.EQ_DUAL, s.INEQ_DUAL]: solution[key] = intf.cvxopt2dense(solution[key]) return solution
def solve_via_data(self, data, warm_start: bool, verbose: bool, solver_opts, solver_cache=None): import cvxopt.solvers # Save original cvxopt solver options. old_options = cvxopt.solvers.options.copy() # Save old data in case need to use robust solver. data[s.DIMS] = dims_to_solver_dict(data[s.DIMS]) # Do a preliminary check for a certain, problematic KKT solver. kktsolver = self.get_kktsolver_opt(solver_opts) if isinstance(kktsolver, str) and kktsolver == 'chol': if self.remove_redundant_rows(data) == s.INFEASIBLE: return {s.STATUS: s.INFEASIBLE} data = self._prepare_cvxopt_matrices(data) c, G, h, dims = data[s.C], data[s.G], data[s.H], data[s.DIMS] A, b = data[s.A], data[s.B] # Apply any user-specific options. # Silence solver. solver_opts["show_progress"] = verbose # Rename max_iters to maxiters. if "max_iters" in solver_opts: solver_opts["maxiters"] = solver_opts["max_iters"] for key, value in solver_opts.items(): cvxopt.solvers.options[key] = value # Always do 1 step of iterative refinement after solving KKT system. if "refinement" not in cvxopt.solvers.options: cvxopt.solvers.options["refinement"] = 1 # finalize the KKT solver. if isinstance(kktsolver, str) and kktsolver == s.ROBUST_KKTSOLVER: kktsolver = setup_ldl_factor(c, G, h, dims, A, b) elif not isinstance(kktsolver, str): kktsolver = kktsolver(c, G, h, dims, A, b) try: results_dict = cvxopt.solvers.conelp(c, G, h, dims, A, b, kktsolver=kktsolver) # Catch exceptions in CVXOPT and convert them to solver errors. except ValueError: results_dict = {"status": "unknown"} # Restore original cvxopt solver options. self._restore_solver_options(old_options) # Construct solution. solution = {} status = self.STATUS_MAP[results_dict['status']] solution[s.STATUS] = status if solution[s.STATUS] in s.SOLUTION_PRESENT: primal_val = results_dict['primal objective'] solution[s.VALUE] = primal_val solution[s.PRIMAL] = results_dict['x'] solution[s.EQ_DUAL] = results_dict['y'] solution[s.INEQ_DUAL] = results_dict['z'] # Need to multiply duals by Q and P_leq. if "Q" in data: y = results_dict['y'] # Test if all constraints eliminated. if y.size[0] == 0: dual_len = data["Q"].size[0] solution[s.EQ_DUAL] = cvxopt.matrix(0., (dual_len, 1)) else: solution[s.EQ_DUAL] = data["Q"]*y if "P_leq" in data: leq_len = data[s.DIMS][s.LEQ_DIM] P_rows = data["P_leq"].size[0] new_len = P_rows + solution[s.INEQ_DUAL].size[0] - leq_len new_dual = cvxopt.matrix(0., (new_len, 1)) z = solution[s.INEQ_DUAL][:leq_len] # Test if all constraints eliminated. if z.size[0] == 0: new_dual[:P_rows] = 0 else: new_dual[:P_rows] = data["P_leq"] * z new_dual[P_rows:] = solution[s.INEQ_DUAL][leq_len:] solution[s.INEQ_DUAL] = new_dual for key in [s.PRIMAL, s.EQ_DUAL, s.INEQ_DUAL]: solution[key] = intf.cvxopt2dense(solution[key]) return solution