I am getting the following error cvxpy.error.DCPError: Problem does not follow DCP rules. Specifically: The objective is not DCP

Question:

I have been working in python trying to create a simple optimization utilizing CVXPY.
I get the error:
cvxpy.error.DCPError: Problem does not follow DCP rules. Specifically:
The objective is not DCP. Its following subexpressions are not:
var2 @ var3 @ 1.0

I believe the problem has to do with my objective function. I tried simplifying it as well as trying static numbers to take the place of flow and pressure. Nothing seems to work.

I could use some advice please.

Thank you in advance.

#Importing Packages
import cvxpy as cp
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


#Define variables (cvxpy package) for the optimization 
Efficiency = cp.Variable()
Flow = cp.Variable()
Pressure = cp.Variable()
#Define constant and conversion variables in the optimization function 
BlowerFactorConstant = 229
# Conversion from 1 psig to 1 lb/in^2 = 1
Pressure_Conversion = 1
# 1 horsepower to kilowatt conversion for power = 0.7457
Power_Conversion = 0.7457
#cost of electricity in cents/kWh
Costofelectricity = 12.28

obj = Costofelectricity*(((Flow(Pressure*Pressure_Conversion))/(BlowerFactorConstant*Efficiency))*Power_Conversion)

#Defining constraints for optimization problem:
constraints = [Pressure >=0, Flow >=0,Efficiency >=0, Efficiency >=0.5, Efficiency <=0.7,Pressure >=2.53, Pressure <=7.6, Flow >= 550, Flow <=1650 ]


#Defining Objective function for optimization which is looking at miniziming electricity costs by including efficiency 
objective = cp.Minimize(obj)


#cvxpy code for solving optimization and displaying results in terminal 

prob = cp.Problem(objective, constraints)

results = prob.solve()

print(results)

I tried utilizing static numbers and changing solvers within CVXPY to no avail.
I reset the constraints and even tried to add more to no avail.
I was expecting a single answer which minimized the objective function.

Asked By: Slade

||

Answers:

Presuming that you wanted to solve it as Geometric Program (GP) (since it is not convex as a DCP), then you have to make some modifications: declare variables as positive rather than having nonnegative bounds, and use gp=True). Something like this:

#Importing Packages
import cvxpy as cp
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


#Define variables (cvxpy package) for the optimization 
Efficiency = cp.Variable(pos=True)
Flow = cp.Variable(pos=True)
Pressure = cp.Variable(pos=True)
#Define constant and conversion variables in the optimization function 
BlowerFactorConstant = 229
# Conversion from 1 psig to 1 lb/in^2 = 1
Pressure_Conversion = 1
# 1 horsepower to kilowatt conversion for power = 0.7457
Power_Conversion = 0.7457
#cost of electricity in cents/kWh
Costofelectricity = 12.28

obj = Costofelectricity*(((Flow*(Pressure*Pressure_Conversion))/(BlowerFactorConstant*Efficiency))*Power_Conversion)

#Defining constraints for optimization problem:
constraints = [ Efficiency >=0.5, Efficiency <=0.7,Pressure >=2.53, Pressure <=7.6, Flow >= 550, Flow <=1650 ]


#Defining Objective function for optimization which is looking at miniziming electricity costs by including efficiency 
objective = cp.Minimize(obj)


#cvxpy code for solving optimization and displaying results in terminal 

prob = cp.Problem(objective, constraints)

results = prob.solve(gp=True)

print(Efficiency.value)
print(Flow.value)
print(Pressure.value)
Answered By: Michal Adamaszek