GAMS: Setting one variable to be equal to the second smallest of another vector variable - gams-math

SET i /i1 * i10/ ;
VARIABLES
x(i)
y
;
I have an optimization (mip) problem where i need my control variable y to be equal to the second smallest number of x. How can I create an equation to do that?
EQUATIONS
myconstraint ;
myconstraint .. y =E= (second smallest element of x) ;

Related

Inline index addition in gams

I want to use an index equation to iterate over a tensors, whereas I always want to extract the value at index i and index i+1. An example:
Variable x; x.up = 10;
Parameter T /1=1,2=2,3=3,4=4,5=5/;
Set a /1,2,4/;
equation eq(a); eq(a).. x =g= T[a+1];
*x ist restricted by the values of T at the indices 2,3 and 5.
Model dummy /all/;
solve dummy min x use lp;
I am aware that gams sees the indices as string-keys rather than numerical ones, so the addition is not intended. Is this possible anyway? This e.g. can be solved by defining another tensor, unfortunaly my given conditions require the index operation inline (i.e. I am not allowed to define additional parameters or sets.
Does this work for you?
Variable x; x.up = 10;
Set aa /1*6/;
Parameter T(aa) /1=1,2=2,3=3,4=4,5=5/;
Set a(aa) /1,2,4/;
equation eq(a); eq(a(aa)).. x =g= T[aa+1];
*x ist restricted by the values of T at the indices 2,3 and 5.
Model dummy /all/;
solve dummy min x use lp;

How to set the lower and upper bounds of a single variable in a single equation in gams

Is this possible?:
boundary_of_x..
19.0 =l= x =g= 22.1;
where x is a positive variable and boundary_of_x is an equation. Or do I have to do this in two equations?
You cannot do this in one equation, you need two, or (better) use the .lo and .up attribute of the variable:
x.lo = 19.0; x.up = 22.1;

Using the sum function in GAMS to sum over a subset of variables

I am working with maximazation problems in GAMS where I will choose
X=(x_1,x2,...,x_n) such that f(X)=c_1*x_1+...c_n*x_n is maximized. The c's are known scalars and I know n (10 in my case). I want my constraints to be such that the first (n-1)=9 x's should sum up to one and the last one should be less than 10. How do I use the sum to do so?
This is what I have tried:
SET C / c1 .... c2 /;
ALIAS(Assets,i)
Parameter Valuesforc(i) 'C values'/
*( here are my values typed in for all the C1)
POSITIVE VARIABLES
x(i);
EQUATIONS
Const1 First constraint
Const1 Second constraint
Obj The Object;
* here comes the trouble:
Const1 .. x(10) =l= 10
Const2 .. sum((i-1),x(i)) =e= 1
The code is not done all the way but I believe the essential setup is typed in. How do you make the summation to find x_1+x_1 + .... x_(n-1) and how do you refer to x_10?
Try this:
Const1 .. x('10') =l= 10;
Const2 .. sum(i$(ord(i)<card(i)),x(i)) =e= 1;
Edit: Here are some notes to explain what happens in Const2, especially in the "$(ord(i) < card(i))" part.
The "$" starts a condition, so it excludes certain elements of i from the sum (see: https://www.gams.com/latest/docs/UG_CondExpr.html#UG_CondExpr_TheDollarCondition)
The operator ord returns the relative position of a member in a set (see: https://www.gams.com/latest/docs/UG_OrderedSets.html#UG_OrderedSets_TheOrdOperator)
The operator card returns the number of elements in a set (see: https://www.gams.com/latest/docs/UG_OrderedSets.html#UG_OrderedSets_TheCardOperator)
So, all in all, there is a condition saying that all elements of i should be included in the sum except for the last one.

Max number of consecutive values (Minizinc)

I'm trying to model the next constraint in Minizinc:
Suppose S is an array of decision variables of size n. I want my decision variables to take a value between 1-k, but there is a maximum 'Cons_Max' on the number of consecutive values used.
For example, suppose Cons_Max = 2, n = 8 and k = 15, then the sequence [1,2,4,5,7,8,10,11] is a valid sequence , while e.g. [1,2,3,5,6,8,9,11] is not a valid sequence because the max number of consecutive values is equal to 3 here (1,2,3).
Important to mention is that sequence [1,3,5,7,9,10,12,14] is also valid, because the values don't need to be consecutive but the max number of consectuive values is fixed to 'Cons_Max'.
Any recommendations on how to model this in Minizinc?
Here's a model with a approach that seems to work. I also added the two constraints all_different and increasing since they are probably assumed in the problem.
include "globals.mzn";
int: n = 8;
int: k = 15;
int: Cons_Max = 2;
% decision variables
array[1..n] of var 1..k: x;
constraint
forall(i in 1..n-Cons_Max) (
x[i+Cons_Max]-x[i] > Cons_Max
)
;
constraint
increasing(x) /\
all_different(x)
;
%% test cases
% constraint
% % x = [1,2,4,5,7,8,10,11] % valid solution
% % x = [1,3,5,7,9,10,12,14] % valid valid solution
% % x = [1,2,3,5,6,8,9,11] % -> not valid solution (-> UNSAT)
% ;
solve satisfy;
output ["x: \(x)\n" ];
Suppose you use array x to represent your decision variable.
array[1..n] of var 1..k: x;
then you can model the constraint like this.
constraint not exists (i in 1..n-1)(
forall(j in i+1..min(n, i+Cons_Max))
(x[j]=x[i]+1)
);

mathematica getting input from user

'm new to mathematica. i have a small project: get an equation , a number n, a number x and a number y and a number h, then using euler formula calculate nTh iteration ... My code is :
f[x_,y_]=input["Please input f(x,y):"]
n=input["Please input number of iterations:"]
x0=input["Please input initial value x0:"]
y0=input["Please input initial value y0:"]
h=input["please input h:"]
For[i=0,i<n,i++,y0=y0+f[x0,y0]*h;x0=x0+h]
but when i copy this code in mathematica 9; it just print some texts and end . it did not get any input from user.just print this:
input["Please input f(x,y):"]
input["Please input number of iterations:"]
input["Please input initial value x0:"]
input["Please input initial value y0:"]
input["please input h:"]
and then end!
whould you help me ?
You should write it in two separate parts.
I would write an initialize variables part then the for loop.
Functions in Mathematica need a delayed equals := and to you recieved the outputs you did because Mathematica doesn't allow you to input while the code is running. you also should suppress the output with a ;
f[x_,y_]:= ; %%write in f(x,y)
n= ; %%input number of iterations
x0= ; %%input initial value x0
y0= ; %%input initial value y0
h= ; %%input h
An euler form of the solution is
euler:= Module[{ans, i, x, y, nsteps},
ans = {{x0,y0}};x=x0;y=y0;nsteps=n;
Do[(y=y+h*f[x,y];
x=x+h; ans=Append[and,{x,y}]),{i,1,nsteps}];ans]
To view the euler forumal result do...
eulerans1 = euler