scs-rs 0.1.0

Bindings to the SCS convex optimization solver
Documentation
import cvxpy
import random
import numpy
from scipy import sparse

(x, y, z) = (cvxpy.Variable(name="x", shape=3), cvxpy.Variable(name="y", shape=3), cvxpy.Variable(name="z", shape=3))

ellipsoids = [ (((1, 0, 0), (2.,0.,0.), 0.0), -5.)]#((1, 0, 0), (3.,2.,5.), 0.7), ((1, 0, 0), (5.,6.,7.), 6.0), ((1, 0, 0), (5.,6.,7.), 0.0), ((1, 0, 0), (5.,6.,7.), 0.0)
#]*10#(((1, 0, 0), [2.,3.,4.], 0.0), ((1, 0, 0), [5.,6.,7.], 0.0), -50.),
#(((1, 1, 0), [2.,3.,4.], 0.0), ((1, 0, -1), [5.,6.,7.], 0.0),((1, 0, 0), [5.,6.,7.], 0.0), -50.),
#(((1, 0, 0), [8.,9.,10.], 1.5), ((0, 1, 0), [14,15.,16.], 1.7), ((1, -1, 1), [17.,18.,19.], 1.6), -70.)]


a = cvxpy.Variable(shape=2)
problem = cvxpy.Problem(cvxpy.Maximize(a[0]), [cvxpy.norm(a + [2,0]) <= 50])

probdata, chain, inverse_data = problem.get_problem_data(cvxpy.SCS)
soln = chain.solve_via_data(problem, probdata)
print(problem)
print(probdata['A'].todense())
print(probdata['A'].__dict__)
print(probdata['b'])
print(probdata['c'])
cone_dims = probdata['dims']
cones = {
    "f": cone_dims.zero,
    "l": cone_dims.nonpos,
    "q": cone_dims.soc,
    "ep": cone_dims.exp,
    "s": cone_dims.psd,
}

print(cones)
print(soln)

exit()

objective = cvxpy.Maximize(808)

constraints = []
seen_foci = {}

for e in ellipsoids:
    expr = e[-1]
    for focus in e[:-1]:
        if focus in seen_foci:
            expr += seen_foci[focus]
            continue

        seen_foci[focus] = cvxpy.Variable(name="f" + str(len(seen_foci)))
        expr += seen_foci[focus]

        nonzero = False
        mom = 0
        # do custom simplifications
        for sign, v in zip(focus[0], (x, y, z)):
            if sign != 0:
                r = v if sign == 1 else -v
                if not nonzero:
                    nonzero = True
                    mom = r
                else:
                    if sign == 1:
                        mom += v
                    else:
                        mom -= v

        if focus[2] != 0.:
            constraints.append(cvxpy.norm(cvxpy.hstack([focus[2], mom + list(focus[1])]), 2) <= seen_foci[focus])
        else:
            constraints.append(cvxpy.norm(mom + list(focus[1]), 2) <= seen_foci[focus])

    constraints.append(expr <= 0)


# manually predict A and b
b = [-e[-1] for e in ellipsoids] + [b for a in [[0.] + ([f[2]] if f[2] != 0. else []) + list(f[1]) for e in ellipsoids for f in e[:-1]] for b in a]

varcount = sum(1 if any(f[0][i] != 0 for e in ellipsoids for f in e[:-1]) else 0 for i in range(3))
width = 3 * varcount + sum(len(e[:-1]) for e in ellipsoids)
a = [ [0. for _ in range(width)] for _ in range(len(b))]

# set the first row: first entry 1 , then last entries to 1


focus_row_counter = len(ellipsoids)
for row_a, e in enumerate(ellipsoids):
    pos = 3 * varcount + sum(len(ee[:-1]) for ee in ellipsoids[:row_a])
    range_len = len(e[:-1])

    for ii in range(range_len):
        a[row_a][pos + ii] = 1.0
    
    for foc_index, (variables, shift, mass) in enumerate(e[:-1]):
        a[focus_row_counter][pos + foc_index] = -1.

        focus_row_counter += 1
        if mass != 0.:
            # mass is an empty line
            focus_row_counter += 1

        for dir_index in range(3):
            for var_index, v in enumerate(variables):
                if v != 0:
                    a[focus_row_counter][3 * var_index + dir_index] = -float(v)

            focus_row_counter += 1



problem = cvxpy.Problem(objective, constraints)

probdata, chain, inverse_data = problem.get_problem_data(cvxpy.SCS)
soln = chain.solve_via_data(problem, probdata)
print(soln)

print(probdata['A'].todense())
print(probdata['b'])
print(problem)

import time

accumtime = 0
N = 1
for _ in range(N):
    start = time.time()
    problem.solve(cvxpy.SCS, verbose=True)#, eps=1e-2)
    print(x.value, y.value, z.value)
    end = time.time()
    #print('time', end - start)
    accumtime += end - start

print('avg time', accumtime / N)

"""
#print('v', x.value)
probdata, chain, inverse_data = problem.get_problem_data(cvxpy.SCS)
#print(probdata)
#print(probdata['G'].todense())  # for ECOS and CVXOPT

import scs
data = {
  'A': probdata['A'],
  'b': probdata['b'],
  'c': probdata['c'],
}
cone_dims = probdata['dims']
cones = {
    "f": cone_dims.zero,
    "l": cone_dims.nonpos,
    "q": cone_dims.soc,
    "ep": cone_dims.exp,
    "s": cone_dims.psd,
}

print(cones)

print('width of A', data['A'].shape, (len(a), len(a[0])))

#assert((len(a), len(a[0])) == data['A'].shape)

for aa in numpy.array(a).tolist():
    print(aa)
print("")
for aa in data['A'].todense().tolist():
    print(aa)

print('a recons=\n{}'.format(numpy.array(a).tolist()))
print('a=\n{}'.format(data['A'].todense().tolist()))
print('b=', data['b'])

#assert(numpy.array_equal(a, data['A'].todense()))
#assert(numpy.array_equal(b, probdata['b']))

# if we override the matrix, we create the wrong mapping
#probdata['A'] = sparse.csc_matrix(numpy.array(a))


# calls SCS using `data`
soln = chain.solve_via_data(problem, probdata)
print(soln)
# unpacks the solution returned by SCS into `problem`
problem.unpack_results(soln, chain, inverse_data)

print('val', x.value)
"""

#print(problem)