Why does AMPL not recognize degrees[j] as integer when 'degrees' is a set of integers? - ampl

Or is there another problem?
It is my first time trying AMPL. I am trying to solve a nonlinear dynamic program. Here are parts of the first draft:
#defining degree
param degree >= 0 integer; # degree of polynomial approximation
param degrees {alpha in 0..degree}; # for storing degrees
...
# defining variables for optimization
var b {alpha in 0..degree}; # coefficient of polynomial
...
# defining constraints
subject to Upper_bound {i in M}: v[i] <=
sum {k in 0..n} (comb[n,k]*(p^k)*((1-p)^(n-k))*((k/n)*((ch[k,i])^d)/(1-d)) +
((n-k)/n)*(cl[k,i]^d)/(1-d)+(1/(1+r))*(sum {j in 0..degree}
(b[j]*cos(degrees[j]*acos((2*Anew[i]-Amax)/(Amax)))))));
I get a syntax error when I pass the mod file:
model DP.mod;
DP.mod, line 41 (offset 1658):
syntax error
context: >>> (b[j]*cos(degrees[j]*acos((2*Anew[i]-Amax)/(Amax))))))) <<< ;
Please help.

It has been solved, thanks! There was an extra ending bracket.

Related

How to express exponential of a variable in docplex?

I need to define a constraint as follows:
mdl.add_constraints(p_pg[plan, segment] == np.exp(u_pg[plan, segment] for plan in range(1, p+1) for segment in range(1, g+1))
In this constraint both p_pg and u_pg are variable and are defined as mdl.continuous_var_dict. However I get the following error:
loop of ufunc does not support argument 0 of type Var which has no callable exp method
Can anyone help how to define this constraint?
exp is not linear so you could either try to do a piecewise linear approximation or use Constraint Programming within CPLEX.
See this example in Easy optimization with python
from docplex.cp.model import CpoModel
mdl = CpoModel(name='buses')
nbbus40 = mdl.integer_var(0,1000,name='nbBus40')
nbbus30 = mdl.integer_var(0,1000,name='nbBus30')
mdl.add(nbbus40*40 + nbbus30*30 >= 300)
#non linear objective
mdl.minimize(mdl.exponent(nbbus40)*500 + mdl.exponent(nbbus30)*400)
msol=mdl.solve()
print(msol[nbbus40]," buses 40 seats")
print(msol[nbbus30]," buses 30 seats")

Var Already defined in AMPL

I'm brand new to AMPL and can't seem to get past this issue.
The code I've written is very basic (again, just starting out) but I keep getting this error message "AMPLPrac.mod, line 2 (offset 6):
x1 is already defined
context: var >>> x1> <<< =0;"
Here is the code:
var x1>=0; var x2>=0;
maximize z: 2*x1 + 3*x2;
subject to c1: 2*x1 + x2 <=4; c2: x1 + 2*x2 <=5;
solve;
Any help would be appreciated. Thanks!
I think the problem may be that you are defining to variables on the same line i AMPL. I tried to run this code (who is the same as yours, only with one argument at every line:
var x1 >= 0;
var x2 >= 0;
maximize z: 2*x1 + 3*x2;
subject to c1: 2 * x1 + x2 <= 4;
subject to c2: x1 + 2 * x2 <= 5;
And I got no errors, and the following output:
x1 = 1
x2 = 2
So I think the problem must be that you have defines mulitply variables on the same line. So to next time, just one variables in every line.
The solution proposed by eriksen1110 is correct, but the problem with your model is that you omited the multiplication operator * from your math expressions, which is not possible in AMPL. It should be:
maximize z: 2*x1 + 3*x2;
# ^ ^
subject to c1: 2*x1 + x2 <= 4;
# ^
subject to c2: x1 + 2*x2 <= 5;
# ^
See it on PIFOP
However, you said that AMPL reports that x1 is already defined, which makes me suspect that you are reading the model file twice, and thus declaring the variable twice, which cannot be done.
Try reseting all declarations by putting this at the beggining of your model file:
reset;
Thanks for all your help! It turns out that the other issues I was having was that I had downloaded not the full version of AMPL and didn't have the correct solvers installed too so I was also getting error messages about not having Minos installed etc but it seems to be resolved now.

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;

Define the function for distance matrix in ampl. Keep getting "i is not defined"

I'm trying to set up a ampl model which clusters given points in a 2-dimensional space according to the model of Saglam et al(2005). For testing purposes I want to generate randomly some datapoints and then calculate the euclidian distance matrix for them (since I need this one). I'm aware that I could only make the distance matrix without the data points but in a later step the data points will be given and then I need to calculate the distances between each the points.
Below you'll find the code I've written so far. While loading the model I keep getting the error message "i is not defined". Since i is a subscript that should run over x1 and x1 is a parameter which is defined over the set D and have one subscript, I cannot figure out why this code should be invalid. As far as I understand, I don't have to define variables if I use them only as subscripts?
reset;
# parameters to define clustered
param m; # numbers of data points
param n; # numbers of clusters
# sets
set D := 1..m; #points to be clustered
set L := 1..n; #clusters
# randomly generate datapoints
param x1 {D} = Uniform(1,m);
param x2 {D} = Uniform(1,m);
param d {D,D} = sqrt((x1[i]-x1[j])^2 + (x2[i]-x2[j])^2);
# variables
var x {D, L} binary;
var D_l {L} >=0;
var D_max >= 0;
#minimization funcion
minimize max_clus_dis: D_max;
# constraints
subject to C1 {i in D, j in D, l in L}: D_l[l] >= d[i,j] * (x[i,l] + x[j,l] - 1);
subject to C2 {i in D}: sum{l in L} x[i,l] = 1;
subject to C3 {l in L}: D_max >= D_l[l];
So far I tried to change the line form param x1 to
param x1 {i in D, j in D} = ...
as well as
param d {x1, x2} = ...
Alas, nothing of this helped. So, any help someone can offer is deeply appreciated. I searched the web but I found nothing useful for my task.
I found eventually what was missing. The line in which I calculated the parameter d should be
param d {i in D, j in D} = sqrt((x1[i]-x1[j])^2 + (x2[i]-x2[j])^2);
Retrospectively it's clear that the subscripts i and j should have been mentioned on the line, I don't know how I could miss that.

GAMS to AMPL OPTIMIZATION

I wondered if someone could help me make this GAMS model to a AMPL model. I am trying to understand the language.
Before hand thanks! You can see the model below.
GAMS Model
set activity / A*G/;
alias (activity,i,j);
set prec(i,j) /
A.(B,C), (B,E).F, C.D, D.E, F.G /;
parameter duration(activity) / A 2, B 3, C 3, D 4, E 8, F 6, G 2 /;
free variable time;
nonnegative variable s(i);
equations ctime(i)
ptime(i,j) ;
ctime(i).. time =g= s(i) + duration(i);
ptime(prec(i,j)).. s(i) + duration(i) =l= s(j);
model schedule /all/;
solve schedule using lp minimizing time;
display time.l, s.l;
GAMS convert function with the option Ampl allows you to generate AMPL input file (*.mod) from a GAMS model file.