Var Already defined in AMPL - 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.

Related

How to constraint at most one element of variable vector could be non-zero in mosek?

I'm solving a problem like this:
Variable X = {x0, x1}
maximize AX + B
subject to 0 <= x0 <= c0,
0 <= x1 <= c1,
x0 * x1 <= 0
It's to say that, I want to get a solution of X with at most one non-zero element.
I'm not sure if problems like this could be solved with mosek.
Could someone give me some help?
Thanks a lot!

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.

Solving Least Squares with orthogonality constraint using Matlab

I need to solve the following Least Squares Problem where A and B and X are all matrices:
cvx_begin quiet;
variable X(len_x) nonnegative;
minimize ( norm(X * A - B , 2));
subject to
X >= 0;
for i=1: size(X,2)
for j= i + 1: size(X,2)
transpose(X(:,i)) * X(:,j) <= epsilon
end
end
cvx_end
I choose CVX, but it doesn't require me to transform the problem into standard form. But with CVX, I get the following error:
Error using cvx/quad_form (line 230)
The second argument must be positive or negative semidefinite.
Error in * (line 261)
[ z2, success ] = quad_form( xx, P, Q, R );
Error in sanaz_opt (line 28)
transpose(X(:,i)) * X(:,j) <= 0.1
I'm wondering how I can solve this problem? I'm trying to use Gurobi or least squares function in Matlab, but it seems they can't handle the transpose(X(:,i)) * X(:,j) constraint.

Variable not defined in AMPL

I keep running into an error with AMPL wherein whenever I try to model my mod file I get an error: Y1 is already defined, this is first time I am using AMPL and not sure where I am going wrong, following is my code and I would really appreciate any help with this. I tried changing the variable name from Y1 to something else then I started getting same error with other variable:
#Creating Variables
var Y1;
var Y2;
var Y3;
#writing the objective fincations
maximize Throughput:500 * Y1 + 450 * Y2 + 600 * Y3;
#writing constraints
subject to 1_limit: 8 * Y1 + 5 * Y2 + 8 * Y3 <=60;
subject to 2_limit: 10 * Y1 + 20 * Y2 + 10 * Y3 <=150;
subject to 3_limit: 0 <= Y1 <=8;
Put the line reset; at the front of your program.
AMPL remembers code previously run, and is getting confused because it remembers that you have already defined Y1.

"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.