我有代码,我尝试将x作为约束但是当我运行时我总是得到一个错误,表示未定义的函数或变量'x'。下面是代码,我如何得到z值的总和?
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);
我的代码是错误的约束吗?
答案 0 :(得分:1)
在附带的代码中,正如我所看到的,您使用矩阵x
4次。
第一个用途是在for循环中,其他3个用途是循环之后的单行。
首先,我怀疑你以错误的方式使用了函数min()。因为参数不应该是字符。删除''并使用以下行:
z = min(z,sum(sum(f(i,k)*d(j,q)*x(i,j)*x(k,q))));
现在,回答你的问题,你还没有告诉矩阵x的内容是什么,所以代码不知道存储的值是什么,例如x(1,1)或x(2, 3)。
因此,预计会抛出错误。