def solveFeasible(): # cone: K = { 'f': 10, 'l': 15, 'q': [5, 10, 0, 1], 's': [3, 4, 0, 0, 1], 'ep': 10, 'ed': 10, 'p': [-0.25, 0.5, 0.75, -0.33] } m = getConeDims(K) data, p_star = genFeasible(K, n=m // 3, density=0.01) params = {'eps': 1e-3, 'normalize': True, 'scale': 5, 'cg_rate': 2} sol_i = superscs.solve(data, K, use_indirect=True, **params) xi = sol_i['x'] yi = sol_i['y'] print('p* = ', p_star) print('pri error = ', (dot(data['c'], xi) - p_star) / p_star) print('dual error = ', (-dot(data['b'], yi) - p_star) / p_star) # direct: sol_d = superscs.solve(data, K, use_indirect=False, **params) xd = sol_d['x'] yd = sol_d['y'] print('p* = ', p_star) print('pri error = ', (dot(data['c'], xd) - p_star) / p_star) print('dual error = ', (-dot(data['b'], yd) - p_star) / p_star)
def test_problems_with_longs(): new_cone = {'q': [], 'l': long(2)} sol = superscs.solve(data, new_cone, use_indirect=False) yield check_solution, sol['x'][0], 1 sol = superscs.solve(data, new_cone, use_indirect=True) yield check_solution, sol['x'][0], 1 new_cone = {'q': [long(2)], 'l': 0} sol = superscs.solve(data, new_cone, use_indirect=False) yield check_solution, sol['x'][0], 0.5 sol = superscs.solve(data, new_cone, use_indirect=True) yield check_solution, sol['x'][0], 0.5
def test_problems(): sol = superscs.solve(data, cone, use_indirect=False) yield check_solution, sol['x'][0], 1 new_cone = {'q': [2], 'l': 0} sol = superscs.solve(data, new_cone, use_indirect=False) yield check_solution, sol['x'][0], 0.5 sol = superscs.solve(data, cone, use_indirect=True) yield check_solution, sol['x'][0], 1 sol = superscs.solve(data, new_cone, use_indirect=True) yield check_solution, sol['x'][0], 0.5
def solveUnbounded(): K = { 'f': 10, 'l': 15, 'q': [5, 10, 0, 1], 's': [3, 4, 0, 0, 1], 'ep': 10, 'ed': 10, 'p': [-0.25, 0.5, 0.75, -0.33] } m = getConeDims(K) data = genUnbounded(K, n=m // 3) params = {'eps': 1e-4, 'normalize': True, 'scale': 0.5, 'cg_rate': 2} sol_i = superscs.solve(data, K, use_indirect=True, **params) sol_d = superscs.solve(data, K, use_indirect=False, **params)
def test_failures(): yield assert_raises, TypeError, superscs.solve yield assert_raises, ValueError, superscs.solve, data, {'q': [4], 'l': -2} yield check_keyword, ValueError, 'max_iters', -1 # python 2.6 and before just cast float to int if platform.python_version_tuple() >= ('2', '7', '0'): yield check_keyword, TypeError, 'max_iters', 1.1 yield check_failure, superscs.solve(data, {'q': [1], 'l': 0})
def test_superscs_aa(self): x_correct = np.array([0., 0., 1., 1.]) ij = np.array([[0, 1, 2, 3] ,[0, 1, 2, 3]]) A = sparse.csc_matrix(([-1., -1., 1., 1.], ij), (4, 4)) b = np.array([0., 0., 1, 1]) c = np.array([1., 1., -1, -1]) cone = {'l': 4} data = {'A': A, 'b': b, 'c': c} tolerance = 1e-8 sol = superscs.solve(\ data, cone, do_super_scs = True, \ direction = 150, \ use_indirect = False, memory = 3, \ verbose = 0, eps = tolerance) self.assertEquals('Solved',sol['info']['status']) self.assertEquals(1, sol['info']['statusVal']) self.assertTrue(sol['info']['resPri'] < tolerance) self.assertTrue(sol['info']['resDual'] < tolerance) self.assertTrue(npla.norm(x_correct - sol['x']) < 1e-12)
def solve_via_data(self, data, warm_start, verbose, solver_opts, solver_cache=None): """Returns the result of the call to SuperSCS. Parameters ---------- data : dict Data generated via an apply call. warm_start : Bool Whether to warm_start SuperSCS. verbose : Bool Control the verbosity. solver_opts : dict SuperSCS-specific options. Returns ------- The result returned by a call to superscs.solve(). """ import superscs args = {"A": data[s.A], "b": data[s.B], "c": data[s.C]} if warm_start and solver_cache is not None and \ self.name in solver_cache: args["x"] = solver_cache[self.name()]["x"] args["y"] = solver_cache[self.name()]["y"] args["s"] = solver_cache[self.name()]["s"] cones = dims_to_solver_dict(data[ConicSolver.DIMS]) # settings user_opts = list(solver_opts.keys()) for k in list(SuperSCS.DEFAULT_SETTINGS.keys()): if k not in user_opts: solver_opts[k] = SuperSCS.DEFAULT_SETTINGS[k] results = superscs.solve(args, cones, verbose=verbose, **solver_opts) if solver_cache is not None: solver_cache[self.name()] = results return results
def solve(self, objective, constraints, cached_data, warm_start, verbose, solver_opts): """Returns the result of the call to the solver. Parameters ---------- objective : LinOp The canonicalized objective. constraints : list The list of canonicalized cosntraints. cached_data : dict A map of solver name to cached problem data. warm_start : bool Should the previous solver result be used to warm_start? verbose : bool Should the solver print output? solver_opts : dict Additional arguments for the solver. Returns ------- tuple (status, optimal value, primal, equality dual, inequality dual) """ import superscs data = self.get_problem_data(objective, constraints, cached_data) # Set the options to be VERBOSE plus any user-specific options. solver_opts["verbose"] = verbose scs_args = {"c": data[s.C], "A": data[s.A], "b": data[s.B]} # If warm_starting, add old primal and dual variables. solver_cache = cached_data[self.name()] if warm_start and solver_cache.prev_result is not None: scs_args["x"] = solver_cache.prev_result["x"] scs_args["y"] = solver_cache.prev_result["y"] scs_args["s"] = solver_cache.prev_result["s"] results_dict = superscs.solve(scs_args, data[s.DIMS], **solver_opts) return self.format_results(results_dict, data, cached_data)
import numpy as np from scipy import sparse ij = np.array([[0, 1, 2, 3], [0, 1, 2, 3]]) A = sparse.csc_matrix(([-1., -1., 1., 1.], ij), (4, 4)) b = np.array([0., 0., 1, 1]) c = np.array([1., 1., -1, -1]) cone = {'l': 4} print c print b print A print cone data = {'A': A, 'b': b, 'c': c} sol = superscs.solve(data, cone, use_indirect=False) print sol sol = superscs.solve(data, cone, use_indirect=True) print sol sol = superscs.solve(data, cone, max_iters=500, eps=1e-6, normalize=False, use_indirect=False) print sol sol = superscs.solve(data, cone, max_iters=500, eps=1e-6, use_indirect=True) print sol