Initialize 3 dimension variable in AMPL - ampl

I have a variable called Rest defined as:
var Rest{I,J,T} >= 0;
where T is the set of time periods and I and J the arcs. I need to define that every value for I and J where T = 0 must be 0. I is the set of supply nodes, and J the set of demand nodes.
I've tried:
let Rest[*,*,0] default 0;
but it got me syntax error. I tried this in both the .dat and .mod file using both := and :
I also tried to put this in the .dat file
var Rest default 0:=
[*,*,0] 1 City1 0;
but it gave me the error
Error at _cmdno 3 executing "solve" command
(file amplin, line 286, offset 11443):
error processing constraint Constraint1[1,'Leveaniemi',1]:
invalid subscript Rest[1,'City1',0]
Thanks in advance!
EDIT:
I now use:
var Rest default 0 :=
[*,*,0] 1 Leveaniemi 0;
which give me the error
Error at _cmdno 3 executing "solve" command
(file amplin, line 286, offset 11438):
error processing constraint Constprocessing commands.
Executing on neos-3.neos-server.org
Error (2) in /opt/ampl/ampl -R amplin
(I am using NEOS server, Gurobi solver). What does this even mean? Also if I declare a Variable Rest like that will it cause every Rest solution to become 0? Or does the compiler interpret it as a start value?
EDIT:
I've tried to implement the solution provided by vitaut. It did not work however, as expressed in the comments below that reply. I figured that since I've defined T as:
set T := 1 2 3 ... 15;
and since I wanted to do a let statement at t = 0, I have to account for that and define Rest as:
var Rest{I,J,TimeT};
where TimeU is T union a set with only a 0 element, i.e. TimeU is interpreted as:
TimeU := 0 1 2 3 ... 15;
With these fixed however, the compiler complains that all my variables and parameters are already defined.

The correct syntax of a let command is
let {i in I, j in J} Rest[i, j, 0] := 0;
However, it will assign starting values to the variables which can change during the optimization process. If you want to make Rest[i, j, 0] always equal to zero, then you should use a constraint instead:
s.t. c{i in I, j in J} Rest[i, j, 0] = 0;

Related

Index out of range error in Verilog, although the register is declared correctly

I am trying to learn Verilog and I have this simple code
module division (
output reg [14:0] A,
input [14:0] D);
reg[4:0] i;
always #(*) begin
for (i = 14; i >= 0; i = i-1) begin
A[0] = D[i];
end
end
endmodule
This returns the error : Index out of range for D.. I have no idea why, since D is declared on that interval. Can you please help me?
I know the code might not make any sense, but I only included the relevant part to the issue.
You have declared i as unsigned, so the expression i >= 0 will always be true.
When i reaches 0, the next iteration is 5'b11111, which is out of range. You should declare i as an integer or add the signed keyword.

OCaml Syntax Error fixed by double semicolon

I'm sorry for asking such a basic question here, but I'm getting a syntax error when trying to compile the following code,
let sum_of_squares_of_two_largest x y z =
let a :: b :: _ = List.sort (fun x y -> -(compare x y)) [x; y; z] in
a * a + b * b;
let rec factorial n =
if n = 0 then 1 else n * factorial (n - 1);
let e_term n = 1.0 /. float_of_int (factorial n);
let rec e_approximation n =
if n = 0 then (e_term 0) else (e_term n) +. (e_approximation (n - 1));
let rec is_even x = if x = 0 then true else is_odd (x - 1);
and is_odd x = not (is_even x);
let rec f_rec n =
if n < 3 then n else f_rec(n - 1) + 2 * f_rec(n - 2) + 3 * f_rec(n - 3);
The uninformative compiler tells me there is syntax error on line 19, which is the last line of the file.
File "source.ml", line 19, characters 0-0:
Error: Syntax error
That is, line 19 is a blank line, only with a new-line character.
I can "fix" this syntax error by adding ;; at the end of each function definition instead of the ;.
Am I missing a semicolon somewhere?
As has been pointed out in the comments, ; is not a statement terminator like in many other (Algol-inspired) languages, but a sequence operator. It takes two values, throws away the first (but warns if it is not unit) and returns the second. a; b is therefore roughly equivalent to let _ = a in b.
You say you would have expected the error to say something like ';' is missing the second operand., but the thing is: it doesn't. Not until it reaches the end of the file (at which point the error message certainly could have been more intelligent, but probably not very accurate anyway).
What follows each ; in your code looks like a perfectly valid expression that might yield a value. For example, if let rec factorial n = ...; had been let rec factorial n = ... in 2 The value of the expression would have been 2. The problem here, from the compiler's point of view, is that it runs out of file before the expression is finished.
As you've also found out, ;; actually is a statement terminator. Or perhaps a toplevel definition terminator is a better term (I don't know if it even has an official name). It's used in the REPL to terminate input, but isn't needed in normal OCaml code unless you mix toplevel definitions and expressions, which you never should.
;; can still be useful for beginners as a "bulkhead", however. If you put just one ;; in place of a ; in the middle of your file, you'll find the compiler now points the error location there. That's because ;; terminates the definition without the preceding expression being complete. So you now know there's an error before that. (Actually in the preceding expression, but since your entire file is one single expression, "before that" is really the best we can do).

AMPL: Subscript out of bound

Hello fellow optimizers!
I'm having some issues with the following constraint:
#The supply at node i equals what was present at the last time period + any new supply and subtracted by what has been extracted from the node.
subject to Constraint1 {i in I, t in T, c in C}:
l[i,t-1,c] + splus[i,t] - sum{j in J, v in V, a in A} x[i,j,v,t,c,a]= l[i,t,c];
which naturally causes this constraint to provide errors the first time it loops, as the t-1 is not defined (for me, l[i,0,c] is not defined. Where
var l{I,T,C} >= 0; # Supply at supply node I in time period T for company C.
param splus{I,T}; # Additional supply at i.
var x{N,N,V,T,C,A} integer >= 0; #Flow from some origin within N to a destination within N using vehicle V, in time T, for company C and product A.
and set T; (in the .mod) is a set defined as:
set T := 1 2 3 4 5 6 7; in the .dat file
I've tried to do:
subject to Constraint1 {i in I, t in T: t >= 2, c in C}:
all else same
which got me a syntax error. I've also tried to include "let l[1,0,1] := 0" for all possible combinations, which got me the error
error processing var l[...]:
no data for set I
I've also tried
subject to Constraint1 {i in I, t in T, p in TT: p>t, c in C}:
l[i,t,c] + splus[i,p] - sum{j in J, v in V, a in A} x[i,j,v,p,c,a]= l[i,p,c];
where
set TT := 2 3 4 5 6;
in the .dat file (and merely set TT; in the .mod) which also gave errors. Does someone have any idea of how to do this?
One way to fix this is to specify the condition t >= 2 at the end of the indexing expression:
subject to Constraint1 {i in I, t in T, c in C: t >= 2}:
...
See also Section A.3 Indexing expressions and subscripts for more details on syntax of indexing expressions.

Variable created from another variable in AMPL

In AMPL I have a set of variables x[e], for some calculations I need a binary variable w[e] which equals 1 when x[e] > 0 and 0 if x[e] = 0. I tried a lot of stuff to make this constraint, but I failed to come up with something. Is this possible?
I have solved your problem the following way:
var u binary;
this is our binary variable that will be 0 or 1.
Then we put following constraint:
subject to U_constraint :
x <= 999999 * u;
Now when x = 0 then AMPL will make u = 0 and when x != 0 obviously u = 1.

"Out of domain" error in MathProg (GLPK)

I am struggling with a seemingly simple model in MathProg. The model is as follows:
set W;
set V;
param b {W, V} binary;
param p;
var w {j in W} <= 0, >= 1;
minimize obj: 0;
subject to within_radius_of {i in V}:
sum {j in W} b[i,j] * w[j] >= 1;
subject to p_limit:
sum {j in W} w[j] <= p;
end;
When I run it, it gives me the error feasibility.glp:11: b[v1,w1] out of domain. I have no idea what is going wrong. Even more strange to me, if I change the relevant line to b[j,i] it keeps giving the exact same error (not b[w1,v1] as I expected).
I inspected the AMPL Diet Example carefully, and despite me seeing no difference in the relevant part of my model it still doesn't work. What is wrong?
Parameter b is declared as binary so it can only take values 0 or 1. You haven't provided a data file, but the error message suggests that the data for b is out of domain (not 0 or 1), for example:
data;
set W := w1;
set V := v1;
param b := w1 v1 0.5;
AMPL gives a more detailed error message in this case:
error processing param b['w1','v1']:
failed check: param b['w1','v1'] = 0.5
is not binary (0 or 1);
The reason why the order of indices doesn't matter in this case is that the data for b is checked completely before the model is actually instantiated. So it seems that w1 and v1 may be swapped in the data file.