Linearize non-linear constraint - optimization

I have a problem which may be defined as:
max 5 x11 + 6 x12 + 2 x21 + 3 x22 st
st.
x11,x12,x21,x22 binary
x11 + x12 = 1
x21 + x22 = 1
-25 x1 x2 >= 0
I want to check only if both x11 and x21 are 1.
How can I linearize this constraint? Or is it possible to linearize?
Thank you very much.

"I want to check only if both x11 and x21 are 1." How about:
x11 + x21 >= 1
instead of
-25 x1 x2 >= 0

Related

Guidance needed | Optimization challenge. Would love to get some inputs 🙏🏻

I need some guidance on how to approach for this problem. I've simplified a real life example and if you can help me crack this by giving me some guidance, it'll be awesome.
I've been looking at public optimization algorithms here (https://www.cvxpy.org/) but I'm a noob and I'm not able to figure out which algorithm would help me (or if I really need it).
Problem:
x1 to x4 are items with certain properties (a,b,c,y,z)
I have certain needs:
Parameter My Needs
a 150
b 800
c 80
My goal is get all optimal coefficient sets for x1 to x4 (can be
fractions) so as to get as much of a, b and c as possible to satisfy
needs from the smallest possible y.
These conditions must always be met:
1)Individual values of z should stay within threshold (between maximum and minimum for x1, x2, x3 and x4)
2)And Total y should be maintained within limits (y <=1000 & y>=2000)
To illustrate an example:
x1
Each x1 has the following properties
a 20 Minimum z 10 Maximum z 50
b 200
c 0
y 300
z 20
x2
Each x2 has the following properties
a 30 Minimum z 60 Maximum z 160
b 5
c 20
y 50
z 40
x3
Each x3 has the following properties
a 20 Minimum z 100 Maximum z 200
b 200
c 15
y 200
z 40
x4
Each x4 has the following properties
a 5 Minimum z 100 Maximum z 300
b 30
c 20
y 500
z 200
One possible arrangement can be (not the optimal solution as I'm trying to keep y as low as possible but above 1000 but to illustrate output)
2x1+2x2+1x3+0.5x4
In this instance:
Coeff x1 2
Coeff x2 2
Coeff x3 3
Coeff x4 0.5
This set of coefficients yields
Optimal?
total y 1550 Yes
total a 162.5 Yes
total b 1025 Yes
total c 95 Yes
z of x1 40 Yes
z of x2 80 Yes
z of x3 120 Yes
z of x4 100 Yes
Lowest y? No
Can anyone help me out?
Thanks!

populate a value of a row into a new column partitioned by date

I have 5 columns i want to create a 6th column with value of 'x5 row' of $ column partitioned by date.
As of now only way i see is unpivot it and put each $ value into separate columns.
But is it possible to make column 6 using some reference?
date sort category % $ 6 th column(to be created)
1/31/2017 0 x1 5.27478 5,406,339,989.60 -31,567,300
1/31/2017 4 x2 5.03073 -21,611,583.57 -31,567,300
1/31/2017 3 x3 5.91494 4,122,330.91 -31,567,300
1/31/2017 1 x4 5.34965 5,754,418,975.28 -31,567,300
1/31/2017 5 x5 5.29412 -31,567,300 -31,567,300
1/31/2017 2.2 x6 7.99999 -250,488.32 -31,567,300
1/31/2017 6 x7 6.16547 -4,984,148.73 -31,567,300
2/28/2017 4 x1 5.04686 -20,182,279.41 5,807,539,814.96
2/28/2017 2.2 x2 5.33533 -176,195.29 5,807,539,814.96
2/28/2017 6 x3 6.13669 -3,455,249.32 5,807,539,814.96
2/28/2017 2.1 x4 5.48088 49,709,482.70 5,807,539,814.96
2/28/2017 1 x5 5.27017 5,807,539,814.96 5,807,539,814.96
2/28/2017 0 x6 5.34965 5,754,418,975.28 5,807,539,814.96
2/28/2017 3 x7 5.32231 23,327,653.12 5,807,539,814.96
You can use a window function (in this case MIN but MAX would work equally well) to select only the value of $ when category is x5:
SELECT *,
MIN(CASE WHEN category = 'x5' THEN Dollar END) OVER (PARTITION BY date) AS "6th Column"
FROM data
Note I've assumed your $ column is actually called Dollar, you can change as necessary.
Demo on SQLFiddle (for SQL Server but should work fine on Teradata)
You can try something like this to get what you require,
SELECT a.*,b.$2
FROM TABLE a
LEFT JOIN
(SELECT Date,sum($) as $2
FROM Table
WHERE category = 'x5'
GROUP BY Date) b
on a.Date=b.Date

Efficiently assigning values to multidimensional array based on indices list on one dimension

I have a matrix M of size [S1, S2, S3].
I have another matrix K that serves as the indices in the first dimension that I want to assign, with size [1, S2, S3].
And V is a [1, S2, S3] matrix which contains the values to be assigned correspondingly.
With for loops, this is how I did it:
for x2 = 1:S2
for x3 = 1:S3
M(K(1,x2,x3), x2, x3) = V(1, x2, x3)
endfor % x3
endfor % x2
Is there a more efficient way to do this?
Visualization for 2D case:
M =
1 4 7 10
2 5 8 11
3 6 9 12
K =
2 1 3 2
V =
50 80 70 60
Desired =
1 80 7 10
50 5 8 60
3 6 70 12
Test case:
M = reshape(1:24, [3,4,2])
K = reshape([2,1,3,2,3,3,1,2], [1,4,2])
V = reshape(10:10:80, [1,4,2])
s = size(M)
M = assign_values(M, K, V)
M =
ans(:,:,1) =
1 20 7 10
10 5 8 40
3 6 30 12
ans(:,:,2) =
13 16 70 22
14 17 20 80
50 60 21 24
I'm looking for an efficient way to implement assign_values there.
Running Gelliant's answer somehow gives me this:
key = sub2ind(s, K, [1:s(2)])
error: sub2ind: all subscripts must be of the same size
You can use sub2ind to use your individual subscripts to linear indices. These can then be used to replace them with the values in V.
M = [1 4 7 10 ;...
2 5 8 11 ;...
3 6 9 12];
s=size(M);
K = [2 1 3 2];
K = sub2ind(s,K,[1:s(2)])
V = [50 80 70 60];
M(K)=V;
You don't need reshape and M=M(:) for it to work in Matlab.
I found that this works:
K = K(:)'+(S1*(0:numel(K)-1));
M(K) = V;
Perhaps this is supposed to work the same way as Gelliant's answer, but I couldn't make his answer work, somehow =/

Gurobi LP file syntax

I try to use the GurobiTM Optimizer. Please find below my QP model :
The problems most commonly solved by the Gurobi Parallel Mixed Integer Programming solver are of the form:
Objective: minimize cT x
Constraints: A x = b (linear constraints)
l ≤ x ≤ u (bound constraints)
some or all xj must take integer values (integrality constraints)
Maximize
- x1 + .5 x2 + .5 x3 + x4 - x5 + .5 x6 + .5 x7 - x8
- .17 x1 * x2 + .66 * x1 * x3 + .66 x1 * x4
+ .56 x2 * x3 + .49 x2 * x4
- .17 x5 * x6 + .82 x5 * x7 + .66 x5 * x8
+ .16 x6 * x7 + .49 x6 * x8
Subject To
c1: x1 + x5 <=1
c2: x2 + x6 <=1,
c3: x3 + x7 <=1
c4: x4 + x8 <=1,
c5: x1 + x2 + x3 + x4 <= 2
c6: x5 + x6 + x7 + x8 <= 2
Bounds
0 <= x1 <= 1
0 <= x2 <= 1
0 <= x3 <= 1
0 <= x4 <= 1
0 <= x5 <= 1
0 <= x6 <= 1
0 <= x7 <= 1
0 <= x8 <= 1
Integers
PS PD JS JD AS AD MS MD
End
It seems that according to Gurobi Interactive Shell the syntax is not correct :
Error reading LP format file /.../toyproblem.lp at line 2
Malformed term in expression
Neighboring tokens: " - .17 x1 * x2 + .66 * x1 * "
Any idea ?
Thanks in advance for your help,
MM.
That's not a linear program (LP) - it's a quadratic program (QP) that isn't written in standard form. Additionally, it's very bad form to write a product of binary variables; you should introduce new binary variables to represent the logical condition. For example, replace w1 * w2 by a new variable z with constraints z <= w1, z <= w2, z >= w1 + w2 - 1.
The error was on the third line:
- .17 x1 * x2 + .66 * x1 * x3 + .66 x1 * x4
it should be:
- .17 x1 * x2 + .66 x1 * x3 + .66 x1 * x4 instead
For the record. Gurobi does handle QP and it calls it simplex... although we know it's not really simplex in the real sense. They used something called "active-set methods"
you may want to post questions in their google group. They answer within a day over there :)

differential equation by maxima

I am new in maxima, so I am really sorry if I ask simple question. I have a differential equation,
(%i1) -(x-x/2*sinh(x/2)+'diff(y,x))*(1/y+'diff(y,x)*x/y^2)+(x-x^2/sinh(x/2)+x^2*cosh(x/2)/(4*(sinh(x/2))^2)+'diff(y,x)*x+'diff(y,x,2)*x^2)/y+y^2-1-0.9*(x-x^2/(2*sinh(x/2)))=0;
2 x
2 2 x cosh(-)
2 d y dy x 2
x --- + x -- - ------- + ---------- + x
2 dx x 2 x
dx sinh(-) 4 sinh (-)
2 2
(%o1) ----------------------------------------
y
x dy
x sinh(-) x -- 2
dy 2 dx 1 2 x
+ (- -- + --------- - x) (---- + -) + y - 0.9 (x - ---------) - 1 = 0
dx 2 2 y x
y 2 sinh(-)
2
(%i2) ode2(%,y,x);
rat: replaced -0.9 by -9/10 = -0.9
(%o2) false
what should I do?
The equation you have is nonlinear. Maxima's ode2 can only solve a limited variety of differential equations, and it appears your equation doesn't fall into any of the categories it can handle.
I don't know if there is another symbolic diff eq solver in Maxima that you can try. If a numerical solution is enough, take a look at rk (a Runge-Kutta implementation).