0
import pandas as pd
import numpy as np
import re
import cvxpy as cvx

data = pd.read_excel('Optimality_V3.xlsx', encoding='latin-1')

enter image description here

As u can see I just imported a csv file as a dataframe. Now I want to solve a maximixation function using the CVXPY library to identify the optimal values of row data['D'] such that the sum of values of data['B'] is maximum.

My objective function is quadratic as my decision variable data['D'] and the function is something like this:

data['B'] = data['C'] * data['D']**2 / data['E'].

The constraints I want to assign to every row of data['D']:

data['D'] * 0.8 <= data['D'] <= data['D'] * 1.2

decision_variables = []
variable_constraints = []

for rownum, row in data.iterrows():
    var_ind = str('x' + str(rownum))
    var_ind = cvx.Variable()
    con_ind = var_ind * 0.8 <= var_ind <= var_ind * 1.2 
    decision_variables.append(str(var_ind))
    variable_constraints.append(str(con_ind))

The above code is my attempt at doing this. I am new to CVXPY and trying to figure out how I can create variables named var_ind with constraints con_ind.

Nitish Gaddam
  • 101
  • 1
  • 11
  • `data['D'] * 0.8 <= data['D'] <= data['D'] * 1.2` is really strange. You may want to check your math. – Erwin Kalvelagen Jan 24 '19 at 11:33
  • data['D'] is a variable with bounds between 0.8 times itself and 1.2 times itself. I should have framed the question better, u can see an updated version below! – Nitish Gaddam Jan 24 '19 at 22:35

2 Answers2

2

Look at documentation for many examples: https://www.cvxpy.org/index.html

data = pd.DataFrame(data={
    'A': [1, 2, 3, 4, 5],
    'B': [0, 50, 40, 80, 20],
    'C': [1200, 600, 900, 6500, 200],
    'D': [0.4, 1.2, 0.8, 1.6, 1.1],
    'E': [0.4, 0.5, 0.6, 0.4, 0.5],
    'F': [0.8, 0.4, 1.2, 1.6, 1],
})

x = cvx.Variable(data.index.size)

constraints = [
    x * 0.8 <= x,
    x <= x * 1.2
]
objective = cvx.Minimize(
    cvx.sum(
        cvx.multiply((data['C']/data['E']).tolist(), x**2)
    )
)
prob = cvx.Problem(objective, constraints)
prob.solve()
print x.value
dave-cz
  • 413
  • 4
  • 22
  • Hey, Dave. Thanks for the reply...love how u framed the equations. I used your code and added a further constraint and am hitting bugs even though I tried reframing this constraint multiple ways. You can see my updated question below – Nitish Gaddam Jan 24 '19 at 22:30
0

The goal of my optimizer is to calculate new value's for column D such that the new values are always (D*0.8 <= new_D(or x below) <= D*1.2, lets call these bounds of x. Apart from these,

The maximization function is:

cvx.sum[cvx.multiply((data['C']*data['F']/data['D']).tolist(), x)]

I have a further constraint:

cvx.sum[cvx.multiply((data['F']*data['E']*data['C']/data['D']).tolist(), x**2)] == data['C'].sum()

import pandas as pd
import numpy as np
import re
import cvxpy as cvx

data = pd.DataFrame(data={
    'A': [1, 2, 3, 4, 5],
    'B': [100, 50, 40, 80, 20],
    'C': [1200, 600, 900, 6500, 200],
    'D': [0.4, 1.2, 0.8, 1.6, 1.1],
    'E': [0.4, 0.5, 0.6, 0.4, 0.5],
    'F': [0.8, 0.4, 1.2, 1.6, 1],
})

x = cvx.Variable(data.index.size)

Now, I want to add a third additional quadratic constraint that says the total sum of column C is always constant.

constraints = [
    x * 0.8 <= x,
    x <= x * 1.2,
    cvx.sum(
        cvx.multiply((data['F']*data['E']*data['C']/data['D']).tolist(), x**2)
    ) == data['C'].sum()
]

The minimization function as you can see is pretty simple and is linear. How do I convert this to a maximization function though?

objective = cvx.Minimmize(
    cvx.sum(
        cvx.multiply((data['C']*data['F']/data['D']).tolist(), x)
    )
)

prob = cvx.Problem(objective, constraints)
prob.solve()
print(x.value)

I am going through the CVXPY documentation and its helping me a lot! But I don't see any examples that have a 3rd constraint designed similar to mine, and I am getting bugs 'DCPError: Problem does not follow DCP rules.'

Nitish Gaddam
  • 101
  • 1
  • 11
  • Constraints have to be linear, you can't add quadratic constraint. If it's not possible to rewrite problem with absolute value (like `[x >= 1, x <= -1]`) you have to use nonlinear solver like [Ipopt](https://projects.coin-or.org/Ipopt). And you should update the question rather than write it like answer. – dave-cz Jan 25 '19 at 08:05
  • The constraint `0.8x ≤ x ≤ 1.2x` is really not correct. It just means `x≥0`. – Erwin Kalvelagen Jan 26 '19 at 15:48