8

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)

    model.optimize()

    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
Georgios
  • 1,193
  • 5
  • 21

1 Answers1

12

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.

Stradivari
  • 1,414
  • 6
  • 14