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

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

Related

Optimization results from xpress do not follow the specified variable type

I found some problems where the optimization result from xpress does not follow the specified variable type. I set vartype=xp.binary when I create the xpress variables, but some of the results have values such as 0.13333, 0.36667, 0.5.
I found that this was caused by one of the constraints. When I disabled most of the constraints, the values are all binary. Then, I enabled the constraint one by one and found one set of constraints that is causing the value to be non-binary.
Has anyone observed this before? Any suggestion on how to enforce the variable value to be binary?
Thanks!
For sake of completeness, expanding Erwin Kalvelagen's comment into an answer:
Fractional values for integer variables usually happen when the problem is infeasible.
After solve() returns, you need to check attributes.mipstatus to make sure you actually have a solution available (see also the potential values for the MIP status here):
p = xp.problem()
...
p.solve()
print(p.getProbStatus(), p.getProbStatusString())
if p.attributes.mipstatus == xp.mip_solution or \
p.attributes.mipstatus == xp.mip_optimal:
print(p.getProbStatusString())
print(p.getSolution())
else:
print('No feasible solution', p.getProbStatusString())
Another way to get the problem status is function p.getProbStatus().

Octave: quadprog index issue?

I am trying to run several files of code for an assignment. I am trying to solve an optimization problem using the "quadprog" function from the "optim" package.
quadprog is supposed to solve the optimization problem in a certain format and takes inputs H,f, A,b, Aeq, Beq, lb, ub.
The issue I am having involves my f which is a column vector of constants. To clarify, f looks like c*[1,1,1,1,1,1] where c is a constant. Quadprog seems to run my code just fine for certain values of c, but gives me the error:
error: index (_,49): but object has size 2x2
error: called from
quadprog at line 351 column 32
for other values of c. So, for example, 1/3 works, but 1/2 doesn't. Does anyone have any experience with this?
Sorry for not providing a working example. My code runs over several files and I seem to only be having problems with a specific value set that is very big. Thanks!
You should try the qp native Octave function.
You mention f is: c*[1,1,1,1,1,1] but, if c is a scalar, that is not a column vector. It seems very odd that a scalar value might produce a dimensions error...

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

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

Is it possible to make Stata throw an error by default when a global macro is not defined, instead of a missing string?

A feature of Stata that is sometimes inconvenient is that calling a non-defined macro returns the missing value . [edit: Stata actually returns a missing string "", not a numerical missing value], instead of throwing an error.
A piece of code, whose correct execution requires the definition of the macro, may just run giving incorrect results if the macro name is misspelled.
E.g.: having defined
global $options = , vce(robust), when
afterwards one writes reg y x $opt instead of reg y x $options the program runs anyway and it may be difficult to realise that the vce() option was not considered.
Is there any way to force Stata to issue an error in this case or is there some useful trick/best practice that can be used to reduce the risk of incurring this sort of mistake?
The feature is described incorrectly. A macro that is undefined is evaluated as an empty string, conventionally written "", i.e. the delimiters " " contain nothing, or -- if you prefer -- nothing is contained between them.
A macro that is undefined is not ever evaluated as numeric system missing, written as a period . (call it dot or stop if you want).
You would see system missing if the macro were set to contain something else that was system missing, which is entirely different. Saved results from programs, for example, might be system missing.
One way to understand this is that macros in Stata contain strings, not numeric values; the fact that some macros have a numeric interpretation is something else. So, an undefined macro is evaluated as an empty string.
Stata programmers learn to use this feature constructively as a way of allowing defaults when macros are undefined and other choices when they are defined.
You are correct that the feature is a source of bugs, as when a spelling mistake leads Stata to see a name that isn't defined and just ignores the reference. The bug is still the programmer's bug, not Stata's.
So, what can you do, apart from check your code as usual? You can always check whether a macro is defined, as in
if "$options" == "" {
* do something
}
else {
* do something else
}
Conversely,
if "$options" != ""
is a test for content.
Alternatively, you could use string scalars. Here is an experiment:
. sysuse auto, clear
(1978 Automobile Data)
. scalar foo = ", meanonly"
. summarize mpg `=scalar(foo)'
. ret li
scalars:
r(N) = 74
r(sum_w) = 74
r(sum) = 1576
r(mean) = 21.2972972972973
r(min) = 12
r(max) = 41
. summarize mpg `=scalar(bar)'
bar not found
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
mpg | 74 21.2973 5.785503 12 41
In this case, there was an error message when an undefined scalar was referred to, but the command was executed any way.
Personally, as a long-term (1991- ) and high intensity Stata user, I just use macros routinely and regard being occasionally bitten by bugs of this kind as a very small price to pay for that. I have not ever used string scalars in this sense before trying to answer this question.
It's a different argument, but I regard using global macros in this way as poor programming style. There are general arguments across programming for minimizing the use of globally declared entities. Local macros are the beasts of choice.

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 .