Semi continuous section in LP format file provided to cbc crashes - solver

I am using version 2.9.9 of cbc in ubuntu 17.10 docker image. My test.lp file has following content:
Maximize
obj: x1 + 2 x2 + 3 x3 + x4
Subject To
c1: - x1 + x2 + x3 + 10 x4 <= 20
c2: x1 - 3 x2 + x3 <= 30
c3: x2 - 3.5 x4 = 0
Bounds
0 <= x1 <= 40
2 <= x4 <= 3
General
x4
Semis
x1 x2 x3
When trying with semis section i get error "terminate called after throwing an instance of 'CoinError?' Aborted"
on mac i get: libc++abi.dylib: terminating with uncaught exception of type CoinError? Abort trap: 6
However if I comment out Semis it works fine. I was hoping that Semis are supported. Am I doing something wrong?
My command is : cbc -presolve on -import test.lp solve solu out.txt
On further analysis i found out when in cbc prompt i type "import test.lp" it fails and shows same error is

The CBC MPS file reader seems not to accept SC bounds either. I think CBC actually supports semi-continuous variables (I tested with a small GAMS model) but it seems difficult to pass it on in an LP or MPS file. As a work around, I would suggest to use binary variables to model semi-continuous behavior:
b * L ≤ x ≤ b * U
b in {0,1}

Related

How can i fix errors with pulp Value Error: too many values to unpack?

I need help with this homework. i just can't find the error. please help me
A company produces five different types of beer and wants to maximise the possible contribution margin by optimising the production quantities. The contribution margin that can be achieved by the different types of beer is shown in the following table:
Variety DB per litre
A 1,9 €
B 1,9 €
C 1,8 €
D 1,4 €
E 1,8 €
The table below shows both the filling time (AD) and the raw material consumption (RV) for one litre of the respective beer varieties:
Variety AD RV1 RV2 RV3
A 2 1,3 1,65 1
B 1,45 1,8 1 1
C 1,2 1,55 1,3 1,4
D 1 1,2 2,1 1,5
E 1,75 1 1 1,55
The filling of the different varieties takes place on the same equipment. Theoretically, a daily bottling quantity of 4000 l of beer type A is possible while maintaining the time capacity, if no other type is produced. The daily supply of raw materials 1, 2 and 3 is as follows:
Raw material units
1 7400
2 7100
3 5900
The maximum daily demand for the products is constant and only differs between the different varieties. The maximum demand for each variety is shown in the table below:
Variety max. demand
A 2100 l
B 950 l
C 1100 l
D 1500 l
E 1550 l
Since the beer types B and C as well as the types A and D each address the same target group, a maximum of 1400 l of B and C or 2700 l of A and D should be produced in total in order to reduce the entrepreneurial risk due to a drop in sales.
Task: Describe the production problem using a linear optimisation model and determine the optimal daily production quantities of the individual beer types.
!pip install pulp
import pulp
solver_list = pulp.listSolvers(onlyAvailable=True)
print(solver_list)
lp_problem = pulp.LpProblem("LPProblem", pulp.LpMaximize)
x1 = pulp.LpVariable('x1', lowBound=0, cat='Continuous')
x2 = pulp.LpVariable('x2', lowBound=0, cat='Continuous')
x3 = pulp.LpVariable('x3', lowBound=0, cat='Continuous')
x4 = pulp.LpVariable('x4', lowBound=0, cat='Continuous')
x5 = pulp.LpVariable('x5', lowBound=0, cat='Continuous')
lp_problem += 1,9 * x1 + 1,9 * x2 + 1,8 * x3 + 1,4 * x4 + 1,8 * x5, "Z"
lp_problem += 2 * x1 <= 4000
lp_problem += 1,3 * x1 + 1,8 * x2 + 1,55 * x3 + 1,2 * x4 + 1 * x5 <= 7400
lp_problem += 1,65 * x1 + 1 * x2 + 1,3 * x3 + 2,1 * x4 + 1 * x5 <= 7100
lp_problem += 1 * x1 + 1 * x2 + 1,4 * x3 + 1,5 * x4 + 1,55 * x5 <= 5900
lp_problem += x1 <= 2100
lp_problem += x2 <= 950
lp_problem += x3 <= 1100
lp_problem += x4 <= 1500
lp_problem += x5 <= 1550
lp_problem += x1 + x4 <= 2700
lp_problem += x2 + x3 <= 1400
print(lp_problem)
lp_problem.solve()
print("Status:", pulp.LpStatus[lp_problem.status])
for variable in lp_problem.variables():
print("{} = {}".format(variable.name, variable.varValue))
print("Deckungsbeitrag =",pulp.value(lp_problem.objective))
Error Message:

Why Do i receive "Singleton variable" warning and incorrect result?

i'm writing a simple function in prolog (beginer). Function calculates a specific type of quadratic equation (but this is not important, calculations are correct).
liniowa(A, B, R, W) :-
Delta is 4*A*A*R - 4*B*B + 4*R,
( Delta < 0
-> false
; ( Delta == 0
-> X is -B/(2*A),
Y is A*X + B,
W = punkt(X, Y)
; X1 is (-B + sqrt(Delta)) / (2*A),
Y1 is A*X1 + B,
X2 is (-B - sqrt(Delta)) / (2*A),
Y2 is A*X2 + B,
writeln(X1), writeln(Y1), writeln(X2), writeln(Y2),
W = punkt(X1, Y1) ; W = punkt(X2, Y2)
)
).
When i run this function, i receive warnings:
Singleton variable in branch: X2
Singleton variable in branch: Y2
In the result i receive strange things. In writeln(X2), writeln(Y2) everything is ok, but then there is sth strange in punkt(X2, Y2):
1.4142135623730951
1.4142135623730951
-1.4142135623730951
-1.4142135623730951
W = punkt(1.4142135623730951, 1.4142135623730951)
W = punkt(_1344, _1346)
What's happening? How should I make it?
The syntax of ->/2 is horrendous at the best of times. That no official better syntax has been developed in 30 years is a sad statement, but of what? I won't say anything more about this.
It is best to separate out the program parts into separate predicates.
(A further refinement could be to separate out the case "Delta == 0" into a dedicated delta_positive(A,B,W), thus avoiding having to make a decision on delta_positive/4 and thus cutting and guarding.
liniowa(A, B, R, W) :-
Delta is 4*A*A*R - 4*B*B + 4*R,
( Delta < 0 -> false ; delta_positive(Delta,A,B,W) ).
delta_positive(0,A,B,W) :-
!, % commit to this clause
X is -B/(2*A),
Y is A*X + B,
W = punkt(X,Y).
delta_positive(Delta,A,B,W) :-
Delta \== 0, % superfluous, but makes things clear
!, % commit is also superfluous
X1 is (-B + sqrt(Delta)) / (2*A),
Y1 is A*X1 + B,
X2 is (-B - sqrt(Delta)) / (2*A),
Y2 is A*X2 + B,
format("X1=~q, Y1=~q, Y2=~q, Y2=~q\n",[X1,Y1,X2,Y2]),
(W = punkt(X1, Y1) ; W = punkt(X2, Y2)).
This immediately shows that parantheses are missing around the expression:
(W = punkt(X1, Y1) ; W = punkt(X2, Y2)).
I tried solving it:-
liniowa(A, B, R, W) :-
Delta is 4*A*A*R - 4*B*B + 4*R,
( Delta < 0
-> false
; ( Delta == 0
-> X is -B/(2*A),
Y is A*X + B,
W = punkt(X, Y)
; X1 is (-B + sqrt(Delta)) / (2*A),
Y1 is A*X1 + B,
X2 is (-B - sqrt(Delta)) / (2*A),
Y2 is A*X2 + B,
writeln(X1), writeln(Y1), writeln(X2), writeln(Y2),
W = punkt(X1, Y1) ); X2 is (-B - sqrt(Delta)) / (2*A),
Y2 is A*X2 + B, W = punkt(X2, Y2)
).
?-liniowa(2, 3, 4, W).
0.9083123951776999
4.8166247903554
-2.4083123951777
-1.8166247903553998
W = punkt(0.9083123951776999, 4.8166247903554)
W = punkt(-2.4083123951777, -1.8166247903553998)
I believe that in your code the calculation is being done in the previous step, that's why it is unable to pick it in the last case.

Lost Robot program

This is an ICPC online round question. I checked sample input and my own imaginary inputs.
This is link to question
Here is my code. This code is in Python.
for _ in range(int(input())):
x1,y1,x2,y2=map(int,input().split())
if (x2-x1==y2-y1)or((x2-x1!=0)and(y2-y1!=0)):
print('sad')
elif (x2-x1==0 and y2-y1>0):
print('up')
elif (x2-x1==0 and y2-y1<0):
print('down')
elif (y2 - y1 == 0 and x2 - x1 > 0):
print('right')
elif (y2 - y1 == 0 and x2 - x1 < 0):
print('left')
Can anyone suggest inputs for that code will not work?? If code is correct for all inputs , any modification will be welcomed?.
Your code already does not parse the input correctly. Have a look:
python /tmp/test.py
1
0 0 0 1
Traceback (most recent call last):
File "/tmp/test.py", line 3, in <module>
x1, y1, x2, y2 = map(int, input().split())
File "<string>", line 1
0 0 0 1
^
Besides from that, it seems to be logical correct.
However, your coding style is improvable. Have a look at my version:
for _ in range(int(input())):
x1, y1, x2, y2 = map(int, input().split())
if x1 != x2 and y1 != y2:
print('sad')
elif x1 == x2 and y1 < y2:
print('up')
elif x1 == x2 and y1 > y2:
print('down')
elif y1 == y2 and x1 < x2:
print('right')
elif y1 == y2 and x1 > x2:
print('left')
The only thing I changed was to improve the conditions. Furthermore, I removed the redundant condition at the bottom.
As a former participant of NWERC and ACM-ICPC I can only suggest to have an eye on readable code. It is not as important as in production code bases, but it helps a lot when you have to debug printed code, as happens at real competitions.

Print GLPK objective/constraints in human readable format

I'm using GLPK C API for a mixed integer programming problem. Is there some way to print the objective/constraints in human readable format for debugging?
Perhaps the nicest format is the CPLEX LP format. It looks something like this:
Maximize
obj: x1 + 2 x2 + 3 x3 + x4
Subject To
c1: - x1 + x2 + x3 + 10 x4 <= 20
c2: x1 - 3 x2 + x3 <= 30
c3: x2 - 3.5 x4 = 0
Bounds
0 <= x1 <= 40
2 <= x4 <= 3
General
x4
End
You can write your model in this format by calling:
int glp_write_lp(glp_prob *P, const glp_cpxcp *parm, const char *fname);
See also glp_write_lp — write problem data in CPLEX LP format in the documentation that comes with GLPK.

Some linear constraints seem to be ignored in function NMinimize with Mathematica 8

I'm trying to minimize a non-linear function of four variables with some linear constraints. Mathematica 8 is unable to find a good solution giving complex values of the function at some point in the iteration. This implies that one or some contraints are not being enabled in the process. Is this a bug or limitation of the optimization function ?
Function to minimize is
ff[lxw_, lwz_, c_, d_] := - J1 (lxw + lwz) - 2 J2 c +
T (-Log[2] - 1/2 (1 - lxw) Log[(1 - lxw)/4] -
1/2 (1 + lxw) Log[(1 + lxw)/4] -
1/2 (1 - lwz) Log[(1 - lwz)/4] -
1/2 (1 + lwz) Log[(1 + lwz)/4] + 1/2 (1 - d) Log[(1 - d)/16] +
1/8 (1 + 2 c + d - 2 lwz - 2 lxw) Log[
1/16 (1 + 2 c + d - 2 lwz - 2 lxw)])
where
T = 10;
J1 = 1;
J2 = -0.2;
are constant parameters. Then I try
NMinimize[{ff[lxw, lwz, c, d],
2 c + d - 2 lwz - 2 lxw >= -0.999 &&
-0.999 <= lxw <= 0.999 &&
-0.999 <= lwz <= 0.999 &&
-0.999 <= c <= 0.999 &&
d <= 0.9999}, {lxw, lwz, c, d}]
with the result
NMinimize::nrnum: "The function value 5.87777[VeryThinSpace]-4.87764\ I\n
is not a real number at {c,d,lwz,lxw} = {-0.718817,-1.28595,0.69171,-0.932461}.
I would appreciate if someone can give a hint at what is happening here.
Try this:
Clear[ff];
ff[lxw_, lwz_, c_, d_] /; 2 c + d - 2 lwz - 2 lxw >= -0.999 :=
< your function def >
This will cause the cause the function to be unevaluated in case NMinimize takes an excursion out of bounds. Sorry i cant test this from here.. If that doesn't do try asking on mathematica.stackexchange.com
Aside, why use <=.999 instead of simply < 1 ?
It just might help if you fix that too ( use integer 1, not 1. )
The warning is appearing because at the values given in the warning the last term in ff is complex, due to taking the log of a negative number, i.e.
{c, d, lwz, lxw} = {
-0.7188174745559741`,
-1.2859482844800894`,
0.6917100913968041`,
-0.9324611085040573`};
Log[1/16 (1 + 2 c + d - 2 lwz - 2 lxw)]
-2.5558 + 3.14159 i
1/16 (1 + 2 c + d - 2 lwz - 2 lxw)
-0.0776301
In Mathematica 9 a result is produced in addition to the warning :-
{-4.90045, {c -> 0.94425, d -> -0.315633, lwz -> 0.900231, lxw -> -0.191476}}
I.e.
{c, d, lwz, lxw} = {
0.9442497691706085`,
-0.31563295950647885`,
0.900230825707721`,
-0.1914760216875171`};
ff[lxw, lwz, c, d]
-4.90045

Resources