5

I need to use CBC solver for mixed integer optimization problem, however in the target environment I cannot use CBC solver installed as an outsource software, it has to be a part of the python library. To overcome this problem I found mip library https://pypi.org/project/mip/ https://docs.python-mip.com/en/latest/index.html which comes with build-in CBC solver, which can be used only having this library imported, with no need for CBC solver installed separately. My problem is, I already have tons of code written with cvxpy (using this separate CBC solver). Now the question is, is there any possibility to use CBC built in mip library, but use it from regular cvxpy interface? Without changing the code, rewriting everything to mip sytax etc.

Sample code I would need to rewrite to mip syntax:

import numpy as np
import cvxpy as cp
import cvxopt 
import mip

def run_sample_optimization():
    demand = np.array([[100, 500, 30], [20, 200, 50], [150, 15, 35], [10, 5, 25]])
    product_supply = np.array([550, 200, 170, 40])

    allocation = cp.Variable(demand.shape, integer=True)

    objective = cp.Maximize(cp.sum(allocation/demand))


    constraints =[cp.sum(allocation, axis=1) <= product_supply,
                allocation <= demand,
                allocation >= 0]                        


    problem = cp.Problem(objective, constraints)

    optimal_value = problem.solve(solver=cp.GLPK_MI) # <-- it would be perfect to link somehow from this place to CBC implemented in mip library

    print('product supply:', product_supply)
    print('demand:\n', demand)
    print('allocation:\n', allocation.value)
    print('calculated score:', optimal_value)
    return product_supply, demand, allocation.value, optimal_value

Many thanks in advance!

Joanna
  • 51
  • 1
  • Someone would need to write a wrapper. This howewer implies some knowledge of cvxpys codebase. It's not too hard though. – sascha Mar 05 '21 at 16:47

1 Answers1

1

Use this package https://pypi.org/project/mip-cvxpy/ I have used it and it works.

This package allows you to solve CVXPY problems using the python-mip package as a backend solver. It works for mixed integer linear problems.

This allows you to use CBC from CVXPY without needing to manually install CBC. By default, CVXOPT calls CyLP to use CBC and requires CBC to be manually installed. python-mip, on the other hand, comes with CBC bundled through pypi.

Roland Pihlakas
  • 4,246
  • 2
  • 43
  • 64