Skip to content

Using python-mip library with cvxpy syntax #1265

@Joanna-Wojdylo

Description

@Joanna-Wojdylo

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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions