This task appears to be harder than it seems to me.

I want to create a continuous variable $x \in [0,1]$.

To test this, I did use the open-source Python-MIP interface which uses the CBC-Solver out of the box.

I wrote a simple code example to see if the variable $x$ will get the float value of $0.4$ by doing the following: \begin{align}\max&\quad x\\\text{s.t.}&\quad x\in[0,1]\\&\quad a=2\\&\quad b=5\\&\quad x\le a/b\end{align}

    from mip import Model, maximize, CONTINUOUS, CBC

    model = Model(solver_name=CBC)

    a = 2
    b = 5

    x = model.add_var('x',lb = 0, ub =1, var_type=CONTINUOUS)

    model += x <= a/b

    model.objective = maximize(x)


    if model.num_solutions:
            for v in model.vars:
               # if v.x > 0:
                    print('{v.name} = {v.x}'.format(**locals()))
                    print('          ', end='')

Instead, I am getting a value of $x=0.0$.

Using Python-MIP package version 1.6.6
Welcome to the CBC MILP Solver 
Version: Trunk
Build Date: Dec 26 2019 

Starting solution of the Linear programming problem using Primal Simplex

x = 0.0
          Coin0506I Presolve 0 (-1) rows, 0 (-1) columns and 0 (-1) elements
Clp0000I Optimal - objective value 0
Coin0511I After Postsolve, objective 0, infeasibilities - dual 0 (0), primal 0 (0)
Clp0032I Optimal objective 0 - 0 iterations time 0.012, Presolve 0.00, Idiot 0.00
  • $\begingroup$ What happens if instead of x<= a/b you actually write x<=0.4? Will it give you the same answer? $\endgroup$ – EhsanK Feb 17 at 19:35
  • $\begingroup$ Unfortunately, yes. I also tested it with the Gurobi solver within this interface, getting the same result. $\endgroup$ – Georgios Feb 17 at 19:36
  • $\begingroup$ Did you try defining a=2.0? $\endgroup$ – independentvariable Feb 17 at 20:52
  • $\begingroup$ Also which python version do you have? If you have Py2 you need to use a//b $\endgroup$ – independentvariable Feb 17 at 20:55
  • $\begingroup$ @independentvariable I tried $a=2.0$ but got the same result. I used Python 3.7 $\endgroup$ – Georgios Feb 17 at 21:07

I don't know much about Python-mip but looking at the code, maximize expects a LinExpr, so I tried:

model.objective = maximize(1*x)

which gives the expected output.

Edit: I also opened a PR to allow maximize(var) and minimize(var).

Edit: The PR has been merged, this shouldn't be a problem in >1.7.2.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.