How to set upper and lower bounds for each element in a set? - optimization

I am creating a GAMS model to solve a simple maximization problem. I have a set J with 3 elements (1,2,3) and a variable x(J) that encompasses all the elements.
I am wondering if there is a way in GAMS to set a lower bound of 0 and upper bound of 3 to each element in the set without having to set each element bound individually and without using the positive variable keyword for the lower bound.
I have tried using x.lo =e= 0 and x.up =e= 3 but none of these are working. I am guessing I am not using the correct syntax but for the life of me cannot seem to find anything on the official documentation about it specifically for sets.
What is the correct way of doing this?

Try
x.lo(J)=0;
x.up(J)=3;
See also here: https://www.gams.com/26/docs/UG_Variables.html#UG_Variables_AssigningValuesToVariableAttributes

Related

A way to restrict the domain of a variable?

I have a code snippet for a transport problem like:
set i /1*50/
d /1*10/
Alias(i,j,k)
parameter
edge(i,j)
distance(i,j)
possible(i,d) 'a collection of possible nodes for d'
possible_edge(i,j,d) 'a collection of possible edge for d';
binary variable x(i,j,d);
I import all the edges from an excel file. But to reduce the number of variables, I'd like to create another parameter like possible node and possible edge.
suppose we run a shortest path algorithm form a source node i and we define possible(i,d) to be all i where distance(i,j) is smaller than a predefined threshold.
In other words, when the network node become larger and larger, I want to find out if there's any possibility to not define x(i,j,d) for every (i,j) combination? Like forcing to only have x(i,j,d)$possible_edge(i,j,d)?? Is there anything like this??
Yes, you can limit the domain of variables in the model statement, like this
Model m / all, x(possible_edge) /;
So, here, the variable x would be limited by the set possible_edge wherever it occurs in model m. Note, that possible_edge must be a set here, not a parameter.
You can find more info about this concept here: https://www.gams.com/latest/docs/UG_ModelSolve.html#UG_ModelSolve_LimitedDomain

Error 148 Dimension different - The symbol is referenced with more/less

I am not able to sort out the Error 148 in GAMS Studio 39.3 for my problem.
148 Dimension different - The symbol is referenced with more/less indices as declared
following is the details:
Sets
EV 'EV unit'/ev1*ev1/
Asev1 'set ev index for values of sch' /1*2/
taev 'set of arrival'/taev1*taev1/
tdev 'set of departure'/tdev1*tdev1/
t ’time periods’ /t1*t2/
w ’scenarios’ /w1*w2/;
SCALAR
Pev ’EV power traded in the Energy market’/7.8/;
PARAMETER
Asev 'alphasch(ev) values of alphasch'
/1 0
2 1/;
PARAMETER
weigth(w) ’weight of scenarios’
/w1 0.05
w2 0.05/;
POSITIVE VARIABLES
As(ev,t,w) 'EV scheduling in time period t';
EQUATION
Pev1 'power output of EV'
Pev1(t,w).. Pev=e=sum(ev,Pev(ev,t,w)*Asev('ev,t,w'));
I am not sure where i am making mistake however i am getting Error 148.
Error 148 Dimension different - The symbol is referenced with more/less
**** indices as declared
Note: I got $148 error under w)*
Pev1(t,w).. Pev=e=sum(ev,Pev(ev,t,w)*Asev('ev,t,w'));
First, you probably don't want the quotes at the end, which makes ev,t,w one specific element. So, you should change it to this:
Pev1(t,w).. Pev=e=sum(ev,Pev(ev,t,w)*Asev(ev,t,w));
But still, this does not work. You defined Asev as 1-dimensional parameter with the elements 1 and 2 above. That does not fit your usage here. So, what do you actually intend? Either the definition of Asev must be wrong or your usage. Or did you actually want to use As here instead, like this?
Pev1(t,w).. Pev=e=sum(ev,Pev(ev,t,w)*As(ev,t,w));
This would resolve the problem for the last term, but give an error before, since also the use of Pev (with 3 indices here) does not match your definition as Scalar above.
Thanks for your feedback!
As or Asev is just the declaration.
Firstly, I removed the quotes at the end, which makes (ev,t,w)
and here it is
Pev1(t,w).. Pev=e=sum(ev,Pev(ev,t,w)*As(ev,t,w));
The problem for the last term is still existing.
I didn't get it you saying " the use of Pev (with 3 indices here) does not match your definition as Scalar above."
I think the Pev (with 3 indices here) match with the scalar above

how to decide if a element is in a set in GAMS

The situation is: I define a set in gams, like:
set n /n1*n100/;
And later in the code, I want to find a way to decide a if a element is in a set. For example, I want to have a function f, such that
(1) if a element in a set, it returns true(or '1'). Like, f('n1',n) = true(or '1')
(2) if a element not in a set, it returns false(or '0'). Like, f('n111',n) = false(or '0')
Does anyone know if there exists this kink of function? Also, if exists, does it also works for multiple dimensional set?
The question is a bit unclear in what you want to do. That being said, subsets seem one to do what you want, for instance:
set m /n1*n100/;
set n(m) /n1*n50/;
parameter test(m);
test(m)=0;
test(n)=1;
display test;
This is overly explicit, for instance, you do not need test(m)=0; as gams default value is 0.
So that you could use param(m)$test(m) = 3; to only set the values where test is positive.
Of course, it is much simpler to use param(n) = 3
Finally, strictly speaking, the instructions: sameas(set1,set2) or sameas(set1,"n101") do what you want.
Without a clearer question, it is hard to help beyond this point.

Mathematica- Solve when given random variables and set equations

I'm trying to figure out if there's a way in mathematica where I can solve for particular variables when given other variables and a set of equations. Essentially there are 6 variables, and I'm given 3 of them and have to calculate the others using these equations-
Variables-
B,Qs,f0,R,c,L
Equations-
f0=1/(2*Pi*Sqrt[L*c])
Qs=(w*L)/R
w=2*Pi*f0
B=f0/Qs
We are given the values of any 3 of those variables and have to figure out the rest using those values.
I was thinking perhaps using Eliminate but I'm not sure exactly how that would be structured as I've only used it previously with set variables that don't change and a single output.
When using the Solve function with Mathematica, you can specify for what variables you want Solve to specify the solutions. Note that Solve may not be able to find expressions in terms of these variables (if the equations you give it are contradictory or insufficient) or for all values as some functions have no inverse or only partial inverses.
Your question looks a lot like homework in Electromagnetics, but here is an example with your original problem. You will have to adapt these ideas to give to Solve the set of variables you are looking for. Also remember to use == to specify equality testing. A simple = is for immediate assignment to a variable.
Solve[{f0 == 1/(2*Pi*Sqrt[L*c]), Qs == (w*L)/R, w == 2*Pi*f0, B == f0/Qs}, {f0, B, c}]
{{f0->w/(2 [Pi]), B->w/(2 [Pi] Qs), c->L/(Qs^2 R^2)}}

can a variable have multiple values

In algebra if I make the statement x + y = 3, the variables I used will hold the values either 2 and 1 or 1 and 2. I know that assignment in programming is not the same thing, but I got to wondering. If I wanted to represent the value of, say, a quantumly weird particle, I would want my variable to have two values at the same time and to have it resolve into one or the other later. Or maybe I'm just dreaming?
Is it possible to say something like i = 3 or 2;?
This is one of the features planned for Perl 6 (junctions), with syntax that should look like my $a = 1|2|3;
If ever implemented, it would work intuitively, like $a==1 being true at the same time as $a==2. Also, for example, $a+1 would give you a value of 2|3|4.
This feature is actually available in Perl5 as well through Perl6::Junction and Quantum::Superpositions modules, but without the syntax sugar (through 'functions' all and any).
At least for comparison (b < any(1,2,3)) it was also available in Microsoft Cω experimental language, however it was not documented anywhere (I just tried it when I was looking at Cω and it just worked).
You can't do this with native types, but there's nothing stopping you from creating a variable object (presuming you are using an OO language) which has a range of values or even a probability density function rather than an actual value.
You will also need to define all the mathematical operators between your variables and your variables and native scalars. Same goes for the equality and assignment operators.
numpy arrays do something similar for vectors and matrices.
That's also the kind of thing you can do in Prolog. You define rules that constraint your variables and then let Prolog resolve them ...
It takes some time to get used to it, but it is wonderful for certain problems once you know how to use it ...
Damien Conways Quantum::Superpositions might do what you want,
https://metacpan.org/pod/Quantum::Superpositions
You might need your crack-pipe however.
What you're asking seems to be how to implement a Fuzzy Logic system. These have been around for some time and you can undoubtedly pick up a library for the common programming languages quite easily.
You could use a struct and handle the operations manualy. Otherwise, no a variable only has 1 value at a time.
A variable is nothing more than an address into memory. That means a variable describes exactly one place in memory (length depending on the type). So as long as we have no "quantum memory" (and we dont have it, and it doesnt look like we will have it in near future), the answer is a NO.
If you want to program and to modell this behaviour, your way would be to use a an array (with length equal to the number of max. multiple values). With this comes the increased runtime, hence the computations must be done on each of the values (e.g. x+y, must compute with 2 different values x1+y1, x2+y2, x1+y2 and x2+y1).
In Perl , you can .
If you use Scalar::Util , you can have a var take 2 values . One if it's used in string context , and another if it's used in a numerical context .