DGradeci
DGradeci

Reputation: 39

CPLEX Python- creating an objective function with quadratic term and linear term

CPLEX novice here. Sorry!

I am running CPLEX, through the python API and I want to set the objective function that contains a quadratic term and a linear term ,and minimise.

Example below:

obj = aQ+bV

where a & b are scalar constants, Q is the quadratic term V is a vector.

Below is a specific example using dummy data. I want to find x1,x2,x3 that minimises the following equation:

              Quadratic Part                                               Linear Part

Quadratic and linear terms in objective function

So far I can solve the quadratic part by explicitly writing out the quadratic matrix and running

p.objective.set_quadratic(Q)

How would I add the second linear term to this objective function?

Thanks,

Daniel

Upvotes: 0

Views: 860

Answers (2)

DGradeci
DGradeci

Reputation: 39

For those interested:

The solution was really straight forward.

  1. p.objective.set_quadratic(q) - this function sets the quadratic part of the objective, but it is not exsaustive, meaning you can add a linear term to the objective by simply including the line 2.

  2. p.objective.set_linear() or as in my case adding the linear term in the variables section:

  3. p.variables.add(obj= my_linear_objective, ub=upper_bounds, lb=lower_bounds, names=my_names)

Essentially here what youre doing is creating an objective that is a combination of your quadratic part set in line #1 + your linear part set in line 2 or 3.

Upvotes: 1

Alex Fleischer
Alex Fleischer

Reputation: 10062

With docplex python you can directly use quadratic terms

See

https://github.com/AlexFleischerParis/zoodocplex/blob/master/zooquadratic.py

For tiny example

Upvotes: 0

Related Questions