Beispiel #1
0
    def test_infeasible(self):
        for i in range(self.num_infeas):
            data = genInfeasible(self.K, n=self.m // 3)

            yield check_infeasible, scs.solve(data, self.K, **self.opts)
            yield check_infeasible, scs.solve(
                data, self.K, use_indirect=True, **self.opts)
Beispiel #2
0
def solveUnbounded():
    K = {'f':10, 'l':15, 'q':[5, 10], 's':[3, 4], 'ep':10, 'ed':10}
    m = getConeDims(K)
    data = genUnbounded(K, n = m/3)
    params = {'EPS':1e-4, 'NORMALIZE':1, 'SCALE':0.5, 'CG_RATE':2}
    sol_i = scs.solve(data, K, params, USE_INDIRECT=True)
    sol_d = scs.solve(data, K, params)
Beispiel #3
0
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 = scs.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 = scs.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)
Beispiel #4
0
def solveInfeasible():
    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 = genInfeasible(K, n = m//3)
    params = {'eps':1e-4, 'normalize':True, 'scale':0.5, 'cg_rate':2}
    sol_i = scs.solve(data, K, use_indirect=True, **params)
    sol_d = scs.solve(data, K, use_indirect=False, **params)
Beispiel #5
0
def solveUnbounded():
    K = {'f':10, 'l':15, 'q':[5, 10], 's':[3, 4], 'ep':10, 'ed':10}
    m = getConeDims(K)
    data = genUnbounded(K, n = m/3)
    params = {'eps':1e-4, 'normalize':True, 'scale':0.5, 'cg_rate':2}
    sol_i = scs.solve(data, K, use_indirect=True, **params)
    sol_d = scs.solve(data, K, **params)
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 = scs.solve(data, K, use_indirect=True, **params)
    sol_d = scs.solve(data, K, **params)
Beispiel #7
0
    def test_unbounded(self):
        for i in range(self.num_unb):
            data = genUnbounded(self.K, n=self.m // 2)

            yield check_unbounded, scs.solve(data, self.K, **self.opts)
            yield check_unbounded, scs.solve(
                data, self.K, use_indirect=True, **self.opts)
Beispiel #8
0
def test_feasible():
    for i in range(num_feas):
        data, p_star = genFeasible(K, n = m // 3, density = 0.1)
        
        sol = scs.solve(data, K, **opts)
        yield check_solution, dot(data['c'],sol['x']), p_star
        yield check_solution, dot(-data['b'],sol['y']), p_star

        sol = scs.solve(data, K, use_indirect=True, **opts)
        yield check_solution, dot(data['c'],sol['x']), p_star
        yield check_solution, dot(-data['b'],sol['y']), p_star
Beispiel #9
0
    def test_feasible(self):
        for i in range(self.num_feas):
            data, p_star = genFeasible(self.K, n = self.m // 3, density = 0.1)

            sol = scs.solve(data, self.K, **self.opts)
            yield check_solution, dot(data['c'],sol['x']), p_star
            yield check_solution, dot(-data['b'],sol['y']), p_star

            sol = scs.solve(data, self.K, use_indirect=True, **self.opts)
            yield check_solution, dot(data['c'],sol['x']), p_star
            yield check_solution, dot(-data['b'],sol['y']), p_star
def test_feasible():
  for i in range(num_feas):
    data, p_star = tools.gen_feasible(K, n=m // 3, density=0.1)

    sol = scs.solve(data, K, use_indirect=False, **opts)
    yield check_solution, np.dot(data['c'], sol['x']), p_star
    yield check_solution, np.dot(-data['b'], sol['y']), p_star

    sol = scs.solve(data, K, use_indirect=True, **opts)
    yield check_solution, np.dot(data['c'], sol['x']), p_star
    yield check_solution, np.dot(-data['b'], sol['y']), p_star
Beispiel #11
0
def test_feasible():
    for i in range(num_feas):
        data, p_star = genFeasible(K, n = m/3, density = 0.01)
        
        sol = scs.solve(data, K, opts)
        yield check_solution, dot(data['c'],sol['x']), p_star
        yield check_solution, dot(-data['b'],sol['y']), p_star

        sol = scs.solve(data, K, opts=dict({'USE_INDIRECT':True}, **opts))
        yield check_solution, dot(data['c'],sol['x']), p_star
        yield check_solution, dot(-data['b'],sol['y']), p_star
Beispiel #12
0
  def test_problems_with_longs():
    new_cone = {'q': [], 'l': long(2)}
    sol = scs.solve(data, new_cone)
    yield check_solution, sol['x'][0], 1
    sol = scs.solve(data, new_cone, use_indirect=True )
    yield check_solution, sol['x'][0], 1

    new_cone = {'q':[long(2)], 'l': 0}
    sol = scs.solve(data, new_cone)
    yield check_solution, sol['x'][0], 0.5
    sol = scs.solve(data, new_cone, use_indirect=True )
    yield check_solution, sol['x'][0], 0.5
    def test_problems_with_longs():
        new_cone = {'q': [], 'l': long(2)}
        sol = scs.solve(data, new_cone, use_indirect=False)
        yield check_solution, sol['x'][0], 1
        sol = scs.solve(data, new_cone, use_indirect=True)
        yield check_solution, sol['x'][0], 1

        new_cone = {'q': [long(2)], 'l': 0}
        sol = scs.solve(data, new_cone, use_indirect=False)
        yield check_solution, sol['x'][0], 0.5
        sol = scs.solve(data, new_cone, use_indirect=True)
        yield check_solution, sol['x'][0], 0.5
Beispiel #14
0
  def test_problems_with_longs():
    new_cone = {'q': [], 'l': long(2)}
    sol = scs.solve(data, new_cone)
    yield check_solution, sol['x'][0], 1
    sol = scs.solve(data, new_cone, opts={'USE_INDIRECT':True})
    yield check_solution, sol['x'][0], 1

    new_cone = {'q':[long(2)], 'l': 0}
    sol = scs.solve(data, new_cone)
    yield check_solution, sol['x'][0], 0.5
    sol = scs.solve(data, new_cone, opts={'USE_INDIRECT':True})
    yield check_solution, sol['x'][0], 0.5
Beispiel #15
0
def test_problems():
  sol = scs.solve(data, cone)
  yield check_solution, sol['x'][0], 1

  new_cone = {'q':[2], 'l': 0}
  sol = scs.solve(data, new_cone)
  yield check_solution, sol['x'][0], 0.5

  sol = scs.solve(data, cone, use_indirect = True )
  yield check_solution, sol['x'][0], 1

  sol = scs.solve(data, new_cone, use_indirect = True )
  yield check_solution, sol['x'][0], 0.5
def test_problems():
    sol = scs.solve(data, cone, use_indirect=False)
    yield check_solution, sol['x'][0], 1

    new_cone = {'q': [2], 'l': 0}
    sol = scs.solve(data, new_cone, use_indirect=False)
    yield check_solution, sol['x'][0], 0.5

    sol = scs.solve(data, cone, use_indirect=True)
    yield check_solution, sol['x'][0], 1

    sol = scs.solve(data, new_cone, use_indirect=True)
    yield check_solution, sol['x'][0], 0.5
Beispiel #17
0
def test_problems():
  sol = scs.solve(data, cone)
  yield check_solution, sol['x'][0], 1

  new_cone = {'q':[2], 'l': 0}
  sol = scs.solve(data, new_cone)
  yield check_solution, sol['x'][0], 0.5

  sol = scs.solve(data, cone, opts={'USE_INDIRECT':True})
  yield check_solution, sol['x'][0], 1

  sol = scs.solve(data, new_cone, opts={'USE_INDIRECT':True})
  yield check_solution, sol['x'][0], 0.5
Beispiel #18
0
 def solve(_solver_opts):
     if scs_version.major < 3:
         _results = scs.solve(args,
                              cones,
                              verbose=verbose,
                              **_solver_opts)
         _status = self.STATUS_MAP[_results["info"]["statusVal"]]
     else:
         _results = scs.solve(args,
                              cones,
                              verbose=verbose,
                              **_solver_opts)
         _status = self.STATUS_MAP[_results["info"]["status_val"]]
     return _results, _status
Beispiel #19
0
    def solve_via_data(self,
                       data,
                       warm_start,
                       verbose,
                       solver_opts,
                       solver_cache=None):
        """Returns the result of the call to the solver.

        Parameters
        ----------
        data : dict
            Data generated via an apply call.
        warm_start : Bool
            Whether to warm_start SCS.
        verbose : Bool
            Control the verbosity.
        solver_opts : dict
            SCS-specific solver options.

        Returns
        -------
        The result returned by a call to scs.solve().
        """
        import scs
        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])
        # Default to eps = 1e-4 instead of 1e-3.
        solver_opts["eps"] = solver_opts.get("eps", 1e-4)

        results = scs.solve(args, cones, verbose=verbose, **solver_opts)
        status = self.STATUS_MAP[results["info"]["status"]]

        if (status == s.OPTIMAL_INACCURATE
                and "acceleration_lookback" not in solver_opts):
            # anderson acceleration is sometimes unstable; retry without it
            results = scs.solve(args,
                                cones,
                                verbose=verbose,
                                acceleration_lookback=0,
                                **solver_opts)

        if solver_cache is not None:
            solver_cache[self.name()] = results
        return results
def solve_unbounded():
    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 = get_scs_cone_dims(K)
    data = gen_unbounded(K, n=m // 3)
    params = {'normalize': True, 'scale': 0.5, 'cg_rate': 2}
    sol_i = scs.solve(data, K, use_indirect=True, **params)
    sol_d = scs.solve(data, K, use_indirect=False, **params)
Beispiel #21
0
def solveInfeasible():
    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 = genInfeasible(K, n=m // 3)
    params = {'eps': 1e-4, 'normalize': True, 'scale': 0.5, 'cg_rate': 2}
    sol_i = scs.solve(data, K, use_indirect=True, **params)
    sol_d = scs.solve(data, K, use_indirect=False, **params)
Beispiel #22
0
def test_solve_infeasible(use_indirect, gpu):
    data = tools.gen_infeasible(K, n=m // 3)
    sol = scs.solve(data, K, use_indirect=use_indirect, gpu=gpu, **params)
    y = sol['y']
    np.testing.assert_array_less(np.linalg.norm(data['A'].T @ y), 1e-3)
    np.testing.assert_array_less(data['b'].T @ y, -0.1)
    np.testing.assert_almost_equal(y, tools.proj_dual_cone(y, K), decimal=4)
Beispiel #23
0
    def solve_via_data(self, data, warm_start, verbose, solver_opts, solver_cache=None):
        """Returns the result of the call to the solver.

        Parameters
        ----------
        data : dict
            Data generated via an apply call.
        warm_start : Bool
            Whether to warm_start SCS.
        verbose : Bool
            Control the verbosity.
        solver_opts : dict
            SCS-specific solver options.

        Returns
        -------
        The result returned by a call to scs.solve().
        """
        import scs
        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])
        results = scs.solve(
            args,
            cones,
            verbose=verbose,
            **solver_opts)
        if solver_cache is not None:
            solver_cache[self.name()] = results
        return results
Beispiel #24
0
    def solve(self, objective, constraints, cached_data, 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.
        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)
        """
        (data, dims), obj_offset = self.get_problem_data(objective,
                                                         constraints,
                                                         cached_data)
        # Set the options to be VERBOSE plus any user-specific options.
        solver_opts["verbose"] = verbose
        results_dict = scs.solve(data, dims, **solver_opts)
        return self.format_results(results_dict, dims, obj_offset)
Beispiel #25
0
    def test_unpack_results(self):
        """Test unpack results method.
        """
        with self.assertRaises(Exception) as cm:
            Problem(Minimize(exp(self.a))).unpack_results("blah", None)
        self.assertEqual(str(cm.exception), "Unknown solver.")

        prob = Problem(Minimize(exp(self.a)), [self.a == 0])
        args = prob.get_problem_data(s.SCS)
        results_dict = scs.solve(*args)
        prob = Problem(Minimize(exp(self.a)), [self.a == 0])
        prob.unpack_results(s.SCS, results_dict)
        self.assertAlmostEqual(self.a.value, 0, places=4)
        self.assertAlmostEqual(prob.value, 1, places=3)
        self.assertAlmostEqual(prob.status, s.OPTIMAL)

        prob = Problem(Minimize(norm(self.x)), [self.x == 0])
        args = prob.get_problem_data(s.ECOS)
        results_dict = ecos.solve(*args)
        prob = Problem(Minimize(norm(self.x)), [self.x == 0])
        prob.unpack_results(s.ECOS, results_dict)
        self.assertItemsAlmostEqual(self.x.value, [0, 0])
        self.assertAlmostEqual(prob.value, 0)
        self.assertAlmostEqual(prob.status, s.OPTIMAL)

        prob = Problem(Minimize(norm(self.x)), [self.x == 0])
        args = prob.get_problem_data(s.CVXOPT)
        results_dict = cvxopt.solvers.conelp(*args)
        prob = Problem(Minimize(norm(self.x)), [self.x == 0])
        prob.unpack_results(s.CVXOPT, results_dict)
        self.assertItemsAlmostEqual(self.x.value, [0, 0])
        self.assertAlmostEqual(prob.value, 0)
        self.assertAlmostEqual(prob.status, s.OPTIMAL)
Beispiel #26
0
    def test_unpack_results(self):
        """Test unpack results method.
        """
        with self.assertRaises(Exception) as cm:
            Problem(Minimize(exp(self.a))).unpack_results("blah", None)
        self.assertEqual(str(cm.exception), "Unknown solver.")

        prob = Problem(Minimize(exp(self.a)), [self.a == 0])
        args = prob.get_problem_data(s.SCS)
        results_dict = scs.solve(*args)
        prob = Problem(Minimize(exp(self.a)), [self.a == 0])
        prob.unpack_results(s.SCS, results_dict)
        self.assertAlmostEqual(self.a.value, 0, places=4)
        self.assertAlmostEqual(prob.value, 1, places=3)
        self.assertAlmostEqual(prob.status, s.OPTIMAL)

        prob = Problem(Minimize(norm(self.x)), [self.x == 0])
        args = prob.get_problem_data(s.ECOS)
        results_dict = ecos.solve(*args)
        prob = Problem(Minimize(norm(self.x)), [self.x == 0])
        prob.unpack_results(s.ECOS, results_dict)
        self.assertItemsAlmostEqual(self.x.value, [0,0])
        self.assertAlmostEqual(prob.value, 0)
        self.assertAlmostEqual(prob.status, s.OPTIMAL)

        prob = Problem(Minimize(norm(self.x)), [self.x == 0])
        args = prob.get_problem_data(s.CVXOPT)
        results_dict = cvxopt.solvers.conelp(*args)
        prob = Problem(Minimize(norm(self.x)), [self.x == 0])
        prob.unpack_results(s.CVXOPT, results_dict)
        self.assertItemsAlmostEqual(self.x.value, [0,0])
        self.assertAlmostEqual(prob.value, 0)
        self.assertAlmostEqual(prob.status, s.OPTIMAL)
Beispiel #27
0
def test_feasible(use_indirect):
    for i in range(num_feas):
        data, p_star = tools.gen_feasible(K, n=m // 3, density=0.1)

        sol = scs.solve(data, K, use_indirect=use_indirect, **opts)
        assert_almost_equal(np.dot(data['c'], sol['x']), p_star, decimal=2)
        assert_almost_equal(np.dot(-data['b'], sol['y']), p_star, decimal=2)
Beispiel #28
0
def test_failures():
    yield assert_raises, TypeError, scs.solve
    yield assert_raises, ValueError, scs.solve, data, {'q': [4], 'l': -2}
    yield check_keyword, ValueError, 'max_iters', -1
    yield check_keyword, TypeError, 'max_iters', 1.1

    yield check_failure, scs.solve(data, {'q': [1], 'l': 0})
Beispiel #29
0
def test_failures():
  yield assert_raises, TypeError, scs.solve
  yield assert_raises, ValueError, scs.solve, data, {'q':[4], 'l':-2}
  yield check_keyword, ValueError, 'max_iters', -1
  yield check_keyword, TypeError, 'max_iters', 1.1

  yield check_failure, scs.solve( data, {'q':[1], 'l': 0} )
Beispiel #30
0
def solveFeasible():
    # cone:
    K = {
        'f': 10,
        'l': 15,
        'q': [5, 10, 0, 1],
        's': [3, 4, 0, 0, 1],
        'ep': 10,
        'ed': 10
    }
    K = validateCone(K)

    density = 0.01  # A matrix density
    m = getConeDims(K)
    n = round(m / 3)
    params = {'EPS': 1e-3, 'NORMALIZE': 1, 'SCALE': 5, 'CG_RATE': 1.5}

    z = randn(m, )
    z = symmetrizeSDP(z, K)  # for SD cones
    y = proj_dual_cone(z, K)  # y = s - z;
    s = y - z  # s = proj_cone(z,K)

    A = sparse.rand(m, n, density, format='csc')
    A.data = randn(A.nnz)
    x = randn(n)
    c = -transpose(A).dot(y)
    b = A.dot(x) + s

    data = {'A': A, 'b': b, 'c': c}

    # indirect
    sol_i = scs.solve(data, K, params, USE_INDIRECT=True)
    xi = sol_i['x']
    yi = sol_i['y']
    print 'c\'x*  = ', dot(c, x)
    print '% error = ', (dot(c, xi) - dot(c, x)) / dot(c, x)
    print 'b\'y*  = ', dot(b, y)
    print '% error = ', (dot(b, yi) - dot(b, y)) / dot(b, y)
    # direct:
    sol_d = scs.solve(data, K, params)
    xd = sol_d['x']
    yd = sol_d['y']
    print 'c\'x*  = ', dot(c, x)
    print '% error = ', (dot(c, xd) - dot(c, x)) / dot(c, x)
    print 'b\'y*  = ', dot(b, y)
    print '% error = ', (dot(b, yd) - dot(b, y)) / dot(b, y)
Beispiel #31
0
def test_solve_unbounded(use_indirect, gpu):
    data = tools.gen_unbounded(K, n=m // 3)
    sol = scs.solve(data, K, use_indirect=use_indirect, gpu=gpu, **params)
    x = sol['x']
    s = sol['s']
    np.testing.assert_array_less(np.linalg.norm(data['A'] @ x + s), 1e-3)
    np.testing.assert_array_less(data['c'].T @ x, -0.1)
    np.testing.assert_almost_equal(s, tools.proj_cone(s, K), decimal=4)
def test_solve_feasible(use_indirect, gpu):
    data, p_star = tools.gen_feasible(K, n=m // 3, density=0.01)

    sol = scs.solve(data, K, use_indirect=use_indirect, gpu=gpu, **params)
    x = sol['x']
    y = sol['y']
    print('p*  = ', p_star)
    print('pri error = ', (np.dot(data['c'], x) - p_star) / p_star)
    print('dual error = ', (-np.dot(data['b'], y) - p_star) / p_star)
def test_failures():
    yield assert_raises, TypeError, scs.solve
    yield assert_raises, ValueError, scs.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, scs.solve(data, {'q': [1], 'l': 0})
Beispiel #34
0
def test_failures():
  yield assert_raises, TypeError, scs.solve
  yield assert_raises, ValueError, scs.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, scs.solve( data, {'q':[1], 'l': 0} )
Beispiel #35
0
def sdp(omega, fmin, warm_start=True):
    '''
    Solves the following SDP with the first-order primal-dual solver SCS:
    ---------Primal Form----------
    minimize <M, \omega>
    s.t.     M - C_i = positive semidefinite for all i = 0...k
    Dual Form:
    ---------Dual Form------------
    minimize \sum_{i=1}^{k} <Y_i, C_i> - fmin
    s.t.     Y_i positive semidefinite for all i = 0...k
                \sum_{i=0}^{k} Y_i = \omega
    ------------------------------
    in order to get the value and the gradient of the acquisiton function.
    Inputs:
        omega: Second order moment matrix
        fmin: min value achieved so far, i.e. min(y0)
        warm_start: Whether or not to warm-start the solution
    Outpus:
        opt_val: Optimal value of the SDP
        M: Optimizer of the SDP
        Y: List of the Optimal Lagrange Multipliers (Dual Optimizers) of the SDP
        C: List of C_i, i = 0 ... k
    '''
    omega = (omega + omega.T)/2

    # Express the problem in the format required by scs
    data = create_scs_data(omega, fmin)
    k_ = omega.shape[0]
    cone = {'s': [k_]*k_}
    if not 'past_omegas' in globals() or len(past_omegas) == 0 or \
        past_omegas[0].shape[0] != k_ or warm_start == False:
        # Clear the saved solutions, as they are of different size
        # than the one we are currently trying to solve.
        reset_warm_starting()
    else:
        # Update data with warm-started solution
        data = get_warm_start(omega, data)

    # Call SCS
    sol = scs.solve(data, cone, eps=1e-5, use_indirect=False, verbose=OUTPUT_LEVEL==1)
    # print(sol['info']['solveTime']) # Prints solution time for SDP.
    if sol['info']['status'] != 'Solved':
        logging.getLogger('opt').warning(
            'SCS solution status:' + sol['info']['status']
        )

    # Extract solution from SCS' structures
    M, Y = unpack_solution(sol['x'], sol['y'], k_)
    objective = -sol['info']['pobj']
    sol['C'] = data['C']

    if warm_start:
        # Save solution for warm starting
        past_solutions.append(sol); past_omegas.append(omega)

    return objective, M, Y, data['C']
Beispiel #36
0
def _solve_relaxation(A, B, eps=1e-9, max_iters=2500, verbose=False):
    """Given the linear system formed by the problem's geometric constraints,
    computes all possible poses.

    Arguments:
    A -- the matrix defining the homogeneous linear system formed from the problem's
    geometric constraints, such that A r = 0.
    B -- an auxiliary matrix which allows to retrieve the translation vector
    from an optimal rotation t = - B @ r.
    eps -- numerical precision of the solver
    max_iters -- maximum number of iterations the solver is allowed to perform
    verbose -- print additional information to the console
    """
    # Solve the QCQP using shor's relaxation
    # Construct Q
    Q = np.block([[A.T @ A, np.zeros((9, 1))], [np.zeros((1, 9)), 0]])

    # Invoke solver
    results = scs.solve(
        {"A": _A, "b": _b, "c": _vech10(Q, 2)},  # data
        {"f": 22, "l": 0, "q": [], "ep": 0, "s": [10]},  # cones
        verbose=verbose,
        eps=eps,
        max_iters=max_iters,
    )
    # Invoke solver
    Z = _vech10_inv(results["x"])
    if np.any(np.isnan(Z)):
        if verbose:
            warnings.warn(
                "The SDP solver did not return a valid solution. Increasing max_iters might solve the issue."
            )
        return [(np.full((3, 3), np.nan), np.full(3, np.nan))]
    vals, vecs = np.linalg.eigh(Z)

    # check for rank
    rank = np.sum(vals > 1e-3)
    r_c = None
    if rank == 1:
        r_c = (vecs[:-1, -1] / vecs[-1, -1])[None, :]
    else:
        r_c = _constraint_ortho_det(vecs, rank)

    # project to valid rotation spaces
    U, _, Vh = np.linalg.svd(r_c.reshape(-1, 3, 3))
    R = U @ Vh
    r = R.reshape(-1, 9)
    t = -r @ B.T

    # Optimality guarantees
    res = r @ A.T
    if np.any(np.abs(np.sum(res * res, axis=-1) - results["info"]["dobj"]) > eps):
        not_certifiable = "The solution is not certifiably optimal."
        warnings.warn(not_certifiable)
    return list(zip(R.transpose(0, 2, 1), t))
Beispiel #37
0
def solveInfeasible():
    K = {'f':10, 'l':15, 'q':[5, 10], 's':[3, 4], 'ep':10, 'ed':10}
    K = validateCone(K)
    m = getConeDims(K)
    n = round(m / 3)
    params = {'EPS':1e-4, 'NORMALIZE':1, 'SCALE':0.5, 'CG_RATE':1.5}

    z = randn(m,)
    z = symmetrizeSDP(z, K)  # for SD cones
    y = proj_dual_cone(z, K)  # y = s - z;
    A = randn(m, n)
    A = A - outer(y, transpose(A).dot(y)) / linalg.norm(y) ** 2  # dense...

    b = randn(m);
    b = -b / dot(b, y);

    data = {'A':sparse.csc_matrix(A), 'b':b, 'c':randn(n)}

    sol_i = scs.solve(data, K, params, USE_INDIRECT=True)
    sol_d = scs.solve(data, K, params)
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 = scs.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 = scs.solve(data, K, **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)
Beispiel #39
0
def solveInfeasible():
    K = {'f': 10, 'l': 15, 'q': [5, 10], 's': [3, 4], 'ep': 10, 'ed': 10}
    K = validateCone(K)
    m = getConeDims(K)
    n = round(m / 3)
    params = {'EPS': 1e-4, 'NORMALIZE': 1, 'SCALE': 0.5, 'CG_RATE': 1.5}

    z = randn(m, )
    z = symmetrizeSDP(z, K)  # for SD cones
    y = proj_dual_cone(z, K)  # y = s - z;
    A = randn(m, n)
    A = A - outer(y, transpose(A).dot(y)) / linalg.norm(y)**2  # dense...

    b = randn(m)
    b = -b / dot(b, y)

    data = {'A': sparse.csc_matrix(A), 'b': b, 'c': randn(n)}

    sol_i = scs.solve(data, K, params, USE_INDIRECT=True)
    sol_d = scs.solve(data, K, params)
Beispiel #40
0
def solveUnbounded():
    K = {'f':10, 'l':15, 'q':[5, 10], 's':[3, 4], 'ep':10, 'ed':10}
    K = validateCone(K)
    m = getConeDims(K)
    n = round(m / 3)
    params = {'EPS':1e-4, 'NORMALIZE':1, 'SCALE':0.5, 'CG_RATE':1.5}

    z = randn(m);
    z = symmetrizeSDP(z, K);  # for SD cones
    s = proj_cone(z, K);
    A = randn(m, n);
    x = randn(n);
    A = A - outer(s + A.dot(x), x) / linalg.norm(x) ** 2;  # dense...
    c = randn(n);
    c = -c / dot(c, x);

    data = {'A':sparse.csc_matrix(A), 'b':randn(m), 'c':c}

    sol_i = scs.solve(data, K, params, USE_INDIRECT=True)
    sol_d = scs.solve(data, K, params)
Beispiel #41
0
def solveFeasible():
    # cone:
    K = {'f':10, 'l':15, 'q':[5, 10, 0 ,1], 's':[3, 4, 0, 0, 1], 'ep':10, 'ed':10}
    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 = scs.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 = scs.solve(data, K, **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)
Beispiel #42
0
def solveFeasible():
    # cone:
    K = {'f':10, 'l':15, 'q':[5, 10, 0 ,1], 's':[3, 4, 0, 0, 1], 'ep':10, 'ed':10}
    m = getConeDims(K) 
    data, p_star = genFeasible(K, n = m/3, density = 0.01)
    params = {'EPS':1e-3, 'NORMALIZE':1, 'SCALE':5, 'CG_RATE':2}
    
    sol_i = scs.solve(data, K, params, USE_INDIRECT=True)
    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 = scs.solve(data, K, 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 solveFeasible():
    # cone:
    K = {'f':10, 'l':50000, '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 = scs.solve(data, K, gpu=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)
Beispiel #44
0
def solveUnbounded():
    K = {'f': 10, 'l': 15, 'q': [5, 10], 's': [3, 4], 'ep': 10, 'ed': 10}
    K = validateCone(K)
    m = getConeDims(K)
    n = round(m / 3)
    params = {'EPS': 1e-4, 'NORMALIZE': 1, 'SCALE': 0.5, 'CG_RATE': 1.5}

    z = randn(m)
    z = symmetrizeSDP(z, K)
    # for SD cones
    s = proj_cone(z, K)
    A = randn(m, n)
    x = randn(n)
    A = A - outer(s + A.dot(x), x) / linalg.norm(x)**2
    # dense...
    c = randn(n)
    c = -c / dot(c, x)

    data = {'A': sparse.csc_matrix(A), 'b': randn(m), 'c': c}

    sol_i = scs.solve(data, K, params, USE_INDIRECT=True)
    sol_d = scs.solve(data, K, params)
Beispiel #45
0
def solveFeasible():
    # cone:
    K = {'f':10, 'l':15, 'q':[5, 10, 0 ,1], 's':[3, 4, 0, 0, 1], 'ep':10, 'ed':10}
    K = validateCone(K)

    density = 0.01  # A matrix density
    m = getConeDims(K)
    n = round(m / 3)
    params = {'EPS':1e-3, 'NORMALIZE':1, 'SCALE':5, 'CG_RATE':1.5}

    z = randn(m,)
    z = symmetrizeSDP(z, K)  # for SD cones
    y = proj_dual_cone(z, K)  # y = s - z;
    s = y - z  # s = proj_cone(z,K)

    A = sparse.rand(m, n, density, format='csc')
    A.data = randn(A.nnz)
    x = randn(n)
    c = -transpose(A).dot(y)
    b = A.dot(x) + s

    data = {'A': A, 'b': b, 'c': c}

    # indirect
    sol_i = scs.solve(data, K, params, USE_INDIRECT=True)
    xi = sol_i['x']
    yi = sol_i['y']
    print 'c\'x*  = ', dot(c, x)
    print '% error = ', (dot(c, xi) - dot(c, x)) / dot(c, x)
    print 'b\'y*  = ', dot(b, y)
    print '% error = ', (dot(b, yi) - dot(b, y)) / dot(b, y)
    # direct:
    sol_d = scs.solve(data, K, params)
    xd = sol_d['x']
    yd = sol_d['y']
    print 'c\'x*  = ', dot(c, x)
    print '% error = ', (dot(c, xd) - dot(c, x)) / dot(c, x)
    print 'b\'y*  = ', dot(b, y)
    print '% error = ', (dot(b, yd) - dot(b, y)) / dot(b, y)
Beispiel #46
0
def solve_unbounded(use_indirect, gpu):
    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 = tools.get_scs_cone_dims(K)
    data = tools.gen_unbounded(K, n=m // 3)
    params = {'normalize': True, 'scale': 0.5, 'cg_rate': 2}
    sol = scs.solve(data, K, use_indirect=use_indirect, gpu=gpu, **params)
def solve_infeasible():
    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 = get_scs_cone_dims(K)
    data = gen_infeasible(K, n=m // 3)
    params = {'eps': 1e-4, 'normalize': True, 'scale': 0.5, 'cg_rate': 2}
    sol_i = scs.solve(data, K, gpu=True, **params)
Beispiel #48
0
def solve_unbounded(use_indirect, gpu):
    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 = tools.get_scs_cone_dims(K)
    data = tools.gen_unbounded(K, n=m // 3)
    params = {"normalize": True, "scale": 0.5}
    sol = scs.solve(data, K, use_indirect=use_indirect, gpu=gpu, **params)
Beispiel #49
0
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 = scs.solve(data, K, gpu=True, **params)
Beispiel #50
0
def do_prox(data, indmap, solmap, x0_vals, rho):    
    # don't modify original dict
    x0_vals = dict(x0_vals)
    # set tau in x0_vals
    x0_vals['__tau'] = rho/2.0
    
    restuff(data, indmap, x0_vals)
    
    out = scs.solve(data, data['dims'], verbose=False)
    scs_x = out['x']
    
    x_vals = extract_sol(scs_x, solmap)
    
    return x_vals
Beispiel #51
0
def scs_solve(A, b, c, dim_dict, init_z=None, **kwargs):
    """Wraps scs.solve for convenience."""
    scs_cones = {
        'l': dim_dict['l'] if 'l' in dim_dict else 0,
        'q': dim_dict['q'] if 'q' in dim_dict else [],
        's': dim_dict['s'] if 's' in dim_dict else [],
        'ep': dim_dict['ep'] if 'ep' in dim_dict else 0,
        'ed': dim_dict['ed'] if 'ed' in dim_dict else 0,
        'f': dim_dict['z'] if 'z' in dim_dict else 0
    }
    #print('scs_cones', scs_cones)
    sol = scs.solve({'A': A, 'b': b, 'c': c}, cone=scs_cones, **kwargs)
    info = sol['info']

    if info['statusVal'] > 0:
        z = xsy2z(sol['x'], sol['s'], sol['y'], tau=1., kappa=0.)

    if info['statusVal'] < 0:
        x = np.zeros_like(sol['x']) \
            if np.any(np.isnan(sol['x'])) else sol['x']

        s = np.zeros_like(sol['s']) \
            if np.any(np.isnan(sol['s'])) else sol['s']

        y = np.zeros_like(sol['y']) \
            if np.any(np.isnan(sol['y'])) else sol['y']

        if np.allclose(y, 0.) and c @ x < 0:
            obj = c @ x
            # assert obj < 0
            x /= -obj
            s /= -obj
            # print('primal res:', np.linalg.norm(A@x + s))

        if np.allclose(s, 0.) and b @ y < 0:
            obj = b @ y
            # assert obj < 0
            y /= -obj
            # print('dual res:', np.linalg.norm(A.T@y))

        # print('SCS NONSOLVED')
        # print('x', x)
        # print('s', s)
        # print('y', y)

        z = xsy2z(x, s, y, tau=0., kappa=1.)

    return z, info
Beispiel #52
0
    def _scs_solve(self, objective, constr_map, dims,
                   var_offsets, x_length,
                   verbose, opts):
        """Calls the SCS solver and returns the result.

        Parameters
        ----------
            objective: LinExpr
                The canonicalized objective.
            constr_map: dict
                A dict of the canonicalized constraints.
            dims: dict
                A dict with information about the types of constraints.
            var_offsets: dict
                A dict mapping variable id to offset in the stacked variable x.
            x_length: int
                The height of x.
            verbose: bool
                Should the solver show output?
            opts: dict
                A dict of the solver parameters passed to scs

        Returns
        -------
        tuple
            (status, optimal objective, optimal x,
             optimal equality constraint dual,
             optimal inequality constraint dual)
        """
        prob_data = self._scs_problem_data(objective, constr_map, dims,
                                           var_offsets, x_length)
        obj_offset = prob_data[1]
        # Set the options to be VERBOSE plus any user-specific options.
        opts = {k.upper():v for k, v in opts.items()}
        opts["VERBOSE"] = verbose
        use_indirect = opts.get("USE_INDIRECT", False)
        results = scs.solve(*prob_data[0], opts=opts, USE_INDIRECT = use_indirect)
        status = s.SOLVER_STATUS[s.SCS][results["info"]["status"]]
        if status in s.SOLUTION_PRESENT:
            primal_val = results["info"]["pobj"]
            value = self.objective._primal_to_result(primal_val - obj_offset)
            eq_dual = results["y"][0:dims["f"]]
            ineq_dual = results["y"][dims["f"]:]
            return (status, value, results["x"], eq_dual, ineq_dual)
        else:
            return (status, None, None, None, None)
Beispiel #53
0
def time_scs(mapper, **kwargs):
    """ Map `scs.solve` over the global `data` and return timing results

    Maps with `mapper`, which may be a parallel map, such as
    `concurrent.futures.ThreadPoolExecutor.map`

    Pass `kwargs` onto `scs.solve` to, for example, toggle verbose output
    """
    ts = []
    for _ in range(repeat):
        start = time.time()
        # `mapper` will usually return a generator instantly
        # need to consume the entire generator to find the actual compute time
        # calling `list` consumes the generator. an empty `for` loop would also work
        a = list(mapper(lambda x: scs.solve(*x, **kwargs), data))
        end = time.time()
        ts.append(end-start)
    return min(ts)
Beispiel #54
0
    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 scs
        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 = scs.solve(scs_args, data[s.DIMS], **solver_opts)
        return self.format_results(results_dict, data[s.DIMS],
                                   data[s.OFFSET], cached_data)
Beispiel #55
0
    def solve(self, objective, constraints, cached_data, 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.
        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)
        """
        (data, dims), obj_offset = self.get_problem_data(objective,
                                                         constraints,
                                                         cached_data)
        # Set the options to be VERBOSE plus any user-specific options.
        solver_opts["verbose"] = verbose
        results = scs.solve(data, dims, **solver_opts)
        status = s.SOLVER_STATUS[s.SCS][results["info"]["status"]]
        if status in s.SOLUTION_PRESENT:
            primal_val = results["info"]["pobj"]
            value = primal_val + obj_offset
            eq_dual = results["y"][0:dims["f"]]
            ineq_dual = results["y"][dims["f"]:]
            return (status, value, results["x"], eq_dual, ineq_dual)
        else:
            return (status, None, None, None, None)
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 = scs.solve(data, K, gpu=True, **params)
Beispiel #57
0
def test_unbounded():
    for i in range(num_unb):
        data = genUnbounded(K, n = m // 2)
        
        yield check_unbounded, scs.solve(data, K, **opts)
        yield check_unbounded, scs.solve(data, K, use_indirect=True, **opts)