0

I am working on a minimum variance optimisation problem in Python using CVXPY that takes in constraints in the form of

constraints = [
                sum_entries(w) == 1, 
                w[0:5] >0.05,
                w[1] > 0.05,
                w[6] == 0,
                sum_entries(w[country_mappings['France']]) == 0.39,
                w >= 0,positive
                w[country_mappings['France']] > 0.12
             ]

With w being in the form of

w = Variable(n)

To run this more efficiently I want to create my list of constraints dynamically based on a file where I will store my settings. Reading in and creating a constraints list works fine, and by using

type(constraints) 

it shows

<type 'list'>

But looking at the actual entries it contains

[EqConstraint(Expression(AFFINE, UNKNOWN, (1, 1)), Constant(CONSTANT, 
POSITIVE, (1, 1))), LeqConstraint(Constant(CONSTANT, POSITIVE, (1, 
1)), Expression(AFFINE, UNKNOWN, (5, 1))), 
LeqConstraint(Constant(CONSTANT, POSITIVE, (1, 1)), 
Expression(AFFINE, UNKNOWN, (1, 1))), EqConstraint(Expression(AFFINE, 
UNKNOWN, (1, 1)), Constant(CONSTANT, ZERO, (1, 1))), 
EqConstraint(Expression(AFFINE, UNKNOWN, (1, 1)), Constant(CONSTANT, 
POSITIVE, (1, 1))), LeqConstraint(Constant(CONSTANT, ZERO, (1, 1)), 
Variable(10, 1)), LeqConstraint(Constant(CONSTANT, POSITIVE, (1, 1)),
Expression(AFFINE, UNKNOWN, (3L, 1L)))]

whereas mine are in this format

['sum_entries(w) == 1', 
 'w[0:5] > 0.05', 
 'w[1] > 0.05', 
 'w[6] == 0', 
 'sum_entries(w[country_mappings['France']]) == 0.39', 
 'w >= 0', 
 'w[country_mappings['France']] > 0.12'
]

The code used to read in the data is

def read_in_config(filename):
    with open(filename) as f:
        content = f.read().splitlines()
    return content

Does anyone know how this can be done ? The problem is getting w in the variable format of CVXPY before it can be used.

Rodrigo de Azevedo
  • 1,097
  • 9
  • 17
ThatQuantDude
  • 759
  • 1
  • 9
  • 26
  • (1) You did not show the code responsible for reading/writing. Maybe you should. (2) The actual entries are actually exactly what you formulated. It's the internal representation. (3) These constraints / internal-representation might be dependent on the variable objects, so i hope you are saving them too. Might be fetched automatically if you have used pickle. (4) Tell more about what you actually trying to do? Replacing *w* after reading the constraints? [cvxpy](www.cvxpy.org) supports parameters to do something like that. Other possible approaches are based on python's eval, which is scary! – sascha Aug 05 '16 at 16:46
  • 1.) I have added the code that reads in my data. 2.) The question in how do I cast my string list elements into the CVXOPT variables ? 4.) I am solving a minimum variance problem, w is a vector of weights which tells me the optimal proportion of a stock, all the weights add up to 1. Hope that clarifies a bit more what I am trying to do – ThatQuantDude Aug 05 '16 at 17:16
  • This looks funny, are you reading your constraints like that? I was sure you would have used pickle. So i'm still not getting all the points. I think you want to build your constraints once, read in the constraints, select w and solve. So why do you want to do this like that? Any real reasons (instead of just formulating the problem depending on a given w)? Depending on the nature of w, there will be not really a speedup. And you surely should change your text as it seems to me you are using cvxpy (which uses cvxopt internally), not cvxopt. This drives me crazy! – sascha Aug 05 '16 at 18:33
  • No pickle involved the reason I want to do it like this is because the constraints will change for every case I am running and I do not want to change them in the source code. I changed the title you are right I use cvxpy – ThatQuantDude Aug 05 '16 at 18:40
  • I don't see the use-case here. Maybe because of misunderstandings, but maybe you got a somewhat other opinion on how do these things. Just formulate your problem, like you have done, but this time use cvxpy's [parameter](http://www.cvxpy.org/en/latest/tutorial/intro/#parameters) for the w-vector. Then, for each run, replace the w-parameter with your new w-vector. **Remark:** This ```w[0:5] >0.05, w[1] > 0.05``` does not make sense. – sascha Aug 05 '16 at 18:43
  • I will give this a try but if you have a quick code snipped I would appreciate it I am quite new to cvxpy. Yes I know this constraints overlapp I was testing all different kinds of constraints. Use case, think about you have a user that can't access the code just formulate the constraints and run the solver. – ThatQuantDude Aug 05 '16 at 18:48

1 Answers1

2

Ok so i have found a solution that works.

One can read in the constraints and concatenate a string to get s.th like

'constraints = [sum_entries(w) == 1,w[0:5] > 0.05,w[1] > 0.05,           
w[6] == 0, sum_entries(w[country_mappings['France']]) == 0.39, 
w >= 0, w[country_mappings['France']] > 0.12 ]'

Then just use

exec 'string from above'

I know exec is not the safest option to use but it works. W has to be defined in the code

w = Variable(n)
ThatQuantDude
  • 759
  • 1
  • 9
  • 26