How to define parameters which change value depending on variable in CXYPY

Viewed 325

I'm trying to solve a convex optimisation problem using CVXPY. I've values x, y which are 150 and 60. These are the initial demand of 2 timeslots. The objective is to minimise the total cost.

Total cost = x * price1 + y * price2 + penalty
Penalty = (150 - x) * 6

So I want to find what are the optimal values of x and y. price1 and price 2 are based on the demand value.

My code is

import cvxpy as cp

def price_func(demand):
    prices = [14, 15, ......., 36.3]
    limits = [0.51, 0.61, ......, 1]
    for i in range(len(limits)):
        if limits[i] * 155 >= demand:
            return prices[i]


x = cp.Variable()
y = cp.Variable()
x.value = 150
y.value = 60

constraints = [x + y == 210,
               x >= y,
               y >= 60]

# Form objective.
obj = cp.Minimize((x*price_func(x.value) + y*price_func(y.value)) * 0.5 + (150 - x.value) * 6)

# Form and solve problem.
prob = cp.Problem(obj, constraints)
prob.solve(warm_start=True)

However, as I understand, my objective in cvxpy looks as

minimize (var0 @ 144.6 + var1 @ 14.0) @ 0.5 

This implies cvxpy doesn't use my price function. But rather it as a fixed value.

How can I fix that issue and make sure cvxpy pick my correct objective function?

0 Answers
Related