4
$\begingroup$
line1 line2 line3 line4
A 2.3 0 3.1 0
B 0 4 2.2 0
C 1.1 0 0 4.6

Let's say after optimization with certain constraints, my model will generate an optimal production allocation table similar to above. A, B, C are the three product types and the matrix displays the number of hours needed for each production line in order to finish the demand.

I need to add another constraint which allows only one non-integer hour amount for each of the products. what is the easiest way or logic to do this? Please show by model.addVars() and model.addConstrs() if possible.

Update: This is what I wrote according to the answer by Rob:

y = edm.addVars(lines, products, vtype=GRB.INTEGER)
z = edm.addVars(lines, products, vtype=GRB.BINARY)
for line in lines:
    for product in products:
        constraint1_1 = edm.addConstr(
            (
              #blocks[line, product] - y[line, product] >= - z[line, product]
              y[line, product] - blocks[line, product] <= z[line, product]
            ), 
            name = 'constraint 1.1'
        )
        
        constraint1_2 = edm.addConstr(
            (
              blocks[line, product] - y[line, product] <= z[line, product]
            ), 
            name = 'constraint 1.2'
        )

constraint2 = edm.addConstrs(
    (
      quicksum(z[line, product] for line in lines) <= 1 for product in products
      ),
    name = 'constraint 2'
    )   

But my model optimization keeps running and has not given a solution for 2 hours. Any ideas what could be the issue? Thanks

$\endgroup$
1
  • $\begingroup$ It might help to add explicit bounds on $y$. $\endgroup$
    – RobPratt
    Sep 3, 2021 at 13:16

1 Answer 1

11
$\begingroup$

Let $x_{p,\ell}$ be the continuous variables in your table. Introduce integer variables $y_{p,\ell}$ and binary variables $z_{p,\ell}$, and impose linear constraints \begin{align} -z_{p,\ell} \le x_{p,\ell} - y_{p,\ell} &\le z_{p,\ell} &&\text{for all $p$ and $\ell$} \tag1 \\ \sum_\ell z_{p,\ell} &\le 1 &&\text{for all $p$} \tag2 \end{align} Constraint $(1)$ enforces $x_{p,\ell} \not= y_{p,\ell} \implies z_{p,\ell} = 1$. Constraint $(2)$ allows this to happen at most once per $p$.

If your solver supports indicator constraints, you can replace $(1)$ with $z_{p,\ell} = 0 \implies x_{p,\ell} = y_{p,\ell}$.

$\endgroup$
2
  • $\begingroup$ Hi thanks for your answer. If you don't mind could you check my code above $\endgroup$
    – Tracy
    Sep 3, 2021 at 4:21
  • $\begingroup$ With respect to solve time, can you post log or any details on solver stats? $\endgroup$
    – anjikum
    Sep 3, 2021 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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