Reputation:
i have the code and i try to make x as constraint but when i run i always get an error said that Undefined function or variable 'x'. below is the code and how can i get the sum of the z value?
clc;
clear;
%sum sum sum sum(fik*djq*xij*xkq)
%i,k= facilities
%j,q= location
%f(i,k)= flow between facilities i and k
%d(j,q)= distance between locations j and q
%xij = 1 if facility i is assigned to location j and if otherwise, xij = 0
% Flow matrix: flow assigning facility i (column) to facility k (row)
f = [0 5 7 9;
5 0 4 6;
7 4 0 3;
9 6 3 0];
%Distance matrix: distance assigning location j (column) to location q (row)
d = [0 6 8 9;
6 0 5 1;
8 5 0 2;
9 1 2 0];
z= 0;
nf= 4;%no of facilities
nd= 4;%no of locations
for i=1:nf
for j=1:nf
for k=1:nd
for q=1:nd
z = min('z','sum(sum(f(i,k)*d(j,q)*x(i,j)*x(k,q)))');
%if x(i,j)==1;
%else x(k,q)==0;
end
end
end
end
%Constraints
%x as binary 0 1,
%x(i,j) = 1 if facility i is assigned to location j 0r otherwise, x(i,j) = 0
Constraints.constr1 = (x(i,j))==1;
%The first set of constraints requires that each facility gets exactly one
%location, that is for each facility, the sum of the location values
%corresponding to that facility is exactly one
Constraints.constr2 = sum(x,2) == 1;
%The second set of constraints are inequalities. These constraints specify
%that each office has no more than one facility in it.
Constraints.constr3 = sum(x,1) == 1;
disp (z);
are the constraint i code is wrong?
Upvotes: 0
Views: 131
Reputation: 26
In the attached code, as I can see, you are using matrix x
4 times.
The first use is inside the for-loops and other 3 uses are the one-liners following the loops.
First of all, I suspect you have used function min() in a wrong way. As the arguments should not be characters. Remove '' and use the following line:
z = min(z,sum(sum(f(i,k)*d(j,q)*x(i,j)*x(k,q))));
Now, coming to your question, you have not told what are the contents of your matrix x, so the code doesn't know what value is stored at say, x(1,1) or x(2,3).
It is, therefore, expected to throw an error.
Upvotes: 1