2 years ago
#38080
Pranav
Convex Optimization: cvxpy solver issue
I'm solving a simple convex optimisation problem as follows:
import numpy as np
import cvxpy as cp
from cvxopt import solvers
#Problem: A diet problem where we minimize the cost of meal while keeping in mind the calories and vitamin constraints.
# Problem Data
vitamin_a = np.matrix([107,500,0])
calories = np.matrix([72,121,65])
cps = np.matrix([0.18,0.23,0.05]) # cost per serving
#Constructing the problem
s = cp.Variable((3,1), integer=True) # number of servings
objective = cp.Minimize(cp.matmul(cps, s))
constraints = [0<=s, 10>=s, 5000<=cp.matmul(vitamin_a, s), 50000>=cp.matmul(vitamin_a, s), 2000<=cp.matmul(calories, s), 2250>=cp.matmul(calories, s)]
prob = cp.Problem(objective, constraints)
result = prob.solve()
print(s.value)
There is this issue about the solver and I could not figure out what is wrong here. I understand that since we include "interger=True", this is a mixed integer problem as pointed in the error. Also, I get the correct solution when this condition is removed.
Error:
---------------------------------------------------------------------------
SolverError Traceback (most recent call last)
<ipython-input-64-b5e33f262d51> in <module>
17 prob = cp.Problem(objective, constraints)
18
---> 19 result = prob.solve()
20 print(s.value)
/usr/local/lib/python3.9/site-packages/cvxpy/problems/problem.py in solve(self, *args, **kwargs)
471 else:
472 solve_func = Problem._solve
--> 473 return solve_func(self, *args, **kwargs)
474
475 @classmethod
/usr/local/lib/python3.9/site-packages/cvxpy/problems/problem.py in _solve(self, solver, warm_start, verbose, gp, qcp, requires_grad, enforce_dpp, **kwargs)
964 return self.value
965
--> 966 data, solving_chain, inverse_data = self.get_problem_data(
967 solver, gp, enforce_dpp, verbose)
968
/usr/local/lib/python3.9/site-packages/cvxpy/problems/problem.py in get_problem_data(self, solver, gp, enforce_dpp, verbose)
579 if key != self._cache.key:
580 self._cache.invalidate()
--> 581 solving_chain = self._construct_chain(
582 solver=solver, gp=gp, enforce_dpp=enforce_dpp)
583 self._cache.key = key
/usr/local/lib/python3.9/site-packages/cvxpy/problems/problem.py in _construct_chain(self, solver, gp, enforce_dpp)
805 A solving chain
806 """
--> 807 candidate_solvers = self._find_candidate_solvers(solver=solver, gp=gp)
808 self._sort_candidate_solvers(candidate_solvers)
809 return construct_solving_chain(self, candidate_solvers, gp=gp,
/usr/local/lib/python3.9/site-packages/cvxpy/problems/problem.py in _find_candidate_solvers(self, solver, gp)
728 in incorrect solutions and is not recommended.
729 """
--> 730 raise error.SolverError(msg)
731 candidates['qp_solvers'] = [
732 s for s in candidates['qp_solvers']
SolverError:
You need a mixed-integer solver for this model. Refer to the documentation
https://www.cvxpy.org/tutorial/advanced/index.html#mixed-integer-programs
for discussion on this topic.
Quick fix 1: if you install the python package CVXOPT (pip install cvxopt),
then CVXPY can use the open-source mixed-integer solver `GLPK`.
Quick fix 2: you can explicitly specify solver='ECOS_BB'. This may result
in incorrect solutions and is not recommended.
I tried to use the 'ECOS_BB' solver but as warned, it didnt work( gave s.value = None). Also, when I tried to use solver='GLPK', it said solver is not installed. How do I install it?
python
cvxpy
convex-optimization
cvxopt
0 Answers
Your Answer