Minimum between two boolean decision variables in Pyomo - linear-programming

I need to do something like this:
d1 == min(d2,d3)
where d is a decision variable. I need to use Pyomo. In cplex the solution is achieved by the funnction minl, how can do this in Pyomo or in an equivalent linear form?
I searched for a solution on Google and found that I could assert that d1 must be less or equal to d2 and d3. But this do not fit my proble, because if d2 and d3 is equal to 1, d1 <= 1 while I need d1 == 1.
Thanks for replies.

When the d variables are binary variables,
d1 = min(d2,d3)
is really the same as multiplication
d1 = d2*d3
This is often linearized as
d1 <= d2
d1 <= d3
d1 >= d2+d3-1

Related

Is it possible to define a multi bound optimization problem in GLPK?

Typical optimization problem are of the type:
minimize ax + by
x1 <= x <= x2
y1 <= y <= y2
where we can think of a and b as the costs related to the two variables.
Is it possible to solve an optimization problem where variables have multiple possible interval bounds?
Ex.:
minimize x + y
x1 <= x <= x2 OR x3 <= x <= x4
y1 <= y <= y2 OR y3 <= y <= y4
possibly with different costs in the different ranges? I don't know how to express this latter requirement in a formula.
In the documentation I found only a single lower bound and a single upper bound and costs related to variables
Math tells us this is not convex and this we will never be able to formulate this as a pure LP. We can however introduce binary variables (the problem will become a MIP) and write
c1 ≤ x ≤ c2 OR c3 ≤ x ≤ c4
as
δ1 * c1 ≤ x1 ≤ δ1 * c2
δ2 * c3 ≤ x2 ≤ δ2 * c4
δ1 + δ2 = 1
x1 + x2 = x
δ1, δ2 ∈ {0,1}
I assumed the c's are constants for the problem (if not, i.e. they are decision variables, the reformulation is similar but slightly more complicated).
Of course if the c's are ordered we can also do:
c1 ≤ x ≤ c4 (these are bounds)
x <= c2 OR x >= c3
I'll leave that as an exercise (hint: again a binary variable is needed)

sympy dsolve returns incorrect answer

I'm using sympy.dsolve to solve a simple ODE for a decay chain.
The answer I get for different decay rates (e.g. lambda_1 > lambda_2) is wrong. After substituting C1=0, I get a simple exponential
-N_0*lambda_1*exp(-lambda_1*t)/(lambda_1 - lambda_2)
instead of the correct answer which has:
(exp(-lambda_1*t)-exp(-lambda_2*t)).
What am I doing wrong?
Here is my code
sp.var('lambda_1,lambda_2 t')
sp.var('N_0')
daughter = sp.Function('N2',Positive=True)(t)
stage1 = N_0*sp.exp(-lambda_1*t)
eq = sp.Eq(daughter.diff(t),stage1*lambda_1 - daughter*lambda_2)
sp.dsolve(eq,daughter)
Your differential equation is (using shorter variable identifiers)
y' = A*N0*exp(-A*t) - B*y
Apply the integrating factor exp(B*t) to get the equivalent
(exp(B*t)*y(t))' = A*N0*exp((B-A)*t)
Integrate to get
exp(B*t)*y(t) = A*N0*exp((B-A)*t)/(B-A) + C
y(t) = A*N0*exp(-A*t)/(B-A) + C*exp(-B*t)
which is exactly what the solver computed.
Did you plug in C1 = 0, expecting to get a solution such that y(0) = 0? That's not how it works. C1 is an arbitrary constant in the formula, setting it to 0 is no guarantee that the expression will evaluate to 0 when t = 0.
Here is a correct approach, step by step
sol1 = sp.dsolve(eq, daughter)
This returns a Piecewise because it's not known if two lambdas are equal:
Eq(N2(t), (C1 + N_0*lambda_1*Piecewise((t, Eq(lambda_2, lambda_1)), (-exp(lambda_2*t)/(lambda_1*exp(lambda_1*t) - lambda_2*exp(lambda_1*t)), True)))*exp(-lambda_2*t))
We can clarify that they are not:
sol2 = sol1.subs(Eq(lambda_2, lambda_1), False)
getting
Eq(N2(t), (C1 - N_0*lambda_1*exp(lambda_2*t)/(lambda_1*exp(lambda_1*t) - lambda_2*exp(lambda_1*t)))*exp(-lambda_2*t))
Next, we need C1 such that the right hand side turns into 0 when t = 0. So, take the right hand side, plug 0 for t, solve for C1:
C1 = Symbol('C1')
C1val = solve(sol2.rhs.subs(t, 0), C1, dict=True)[0][C1]
(It's not necessary to include dict=True but I like it, because it enforces uniform output of solve: it's an array of dictionaries.)
By the way, C1val is now N_0*lambda_1/(lambda_1 - lambda_2). Put it in:
sol3 = sol2.subs(C1, C1val).simplify()
and there you have it:
Eq(N2(t), N_0*lambda_1*(exp(lambda_1*t) - exp(lambda_2*t))*exp(-t*(lambda_1 + lambda_2))/(lambda_1 - lambda_2))
The expression is equivalent to N_0*lambda_1*(exp(-lambda_2*t) - exp(-lambda_1*t))/(lambda_1 - lambda_2) although SymPy seems loathe to combine the exponentials here.

Predict data values given history and constraints

If I have data series and a set of constraints and want to predict the most likely values, what is the right algorithm or approach? For example, given the data table as follows:
The first three rows illustrate typical data values. Imagine we have dozens or hundreds of such rows. The constraints on the system are as follows:
G1 + G2 + G3 + G4 == D1 + D2 + D3
G1 + G2 = D1 - C1
G3 = D2 + C1 - C2
G4 = D3 + C2
So, given D1, D2 and D3 we need to predict G1, G2, G3, G4, C1, and C2. Note that there may not necessarily be enough information to solve the system by linear programming alone and so some kind of trend analysis or probability distribution might need to be made.
What is the right algorithm or approach to solve a problem like this?

Bresenham's algorithm

How to find the decision parameter for drawing different functions like parabola, sine curve, bell curve?
Please tell me about the approach why do we sometimes multiply by constant?
For Example
in case of ellipse, p = a^2(d1 - d2),p = b^2(d1 - d2) for upper and lower half region
respectively where a, b constants
in case of line, p = deltax(d1 - d2) where p is decision parameter d1,d2 are
distances,deltax is constant and is equal to xend - xstart
why not only take (d1 -d2) as parameter
Bresenham's algorithm as stated by the OP is a bit amiss, but I assume the following.
The decision parameter could adjust d1 - d2 and not scale by some constant as you suggest were it not for the initialization of the decision parameter. It is not generally scalable by that constant.
// code from http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
plotLine(x0,y0, x1,y1)
dx=x1-x0
dy=y1-y0
D = 2*dy - dx // Not scalable by 2
plot(x0,y0)
y=y0
for x from x0+1 to x1
if D > 0
y = y+1
plot(x,y)
D = D + (2*dy-2*dx) // Scalable by 2
else
plot(x,y)
D = D + (2*dy) // Scalable by 2

What is C2 surface in CAD, what does C2 mean?

C2 surface/mesh is well-known surface in CAD/OpenGL. But I just want to know that what is C2 means ?
C means what ?
2 means what ?
C2 = acronym ???????
Continuous in the second derivative.
Generally you'll see it used when discussing various splines.