我如何创建二进制[0,1]?

时间:2018-05-18 11:08:41

标签: matlab optimization binary

我想声明x对于二进制0,1

是可变的

i和k表示流矩阵中的设施,j和q表示距离矩阵中的位置。

x(i,j)表示如果i(设施)在j(位置)中分配,则x将等于1.

如果将设施i分配给位置j,则

x(i,j)= 1,否则,xij = 0, 所以否则意味着如果x(k,q)= 1,则x(i,J)将为0 ......

手动计算的例子

Min =(f i1,k1 * d j1,q1 * x i1,j1 * x k1,q1)+(f i1,k1 * d j1,q2 * x i1,j1 * x k1,q2)+( f i1,k1 * d j1,q3 * x i1,j1 * x k1,q3)....

(0 * 0 * 1 * 1)+(0 * 6 * 1 * 0)+(0 * 8 * 1 * 0).....

我想* xi1,j1 * xk1,q1为0或1 ..如果我选择i1,j1 = 1则另一个将为0 ..例如i2,j1将等于0

下面是编码

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;
 nd= 4;
 for i=1:nf 
     for j=1:nf 
         for k=1:nd 
             for q=1:nd 
                 z = min('z','f(i,k)*d(j,q)*x(i,j)*x(k,q)'); 
             end
         end
     end
 end
%Constraints
%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.constr1 = 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.constr2 = sum(x,1) == 1;
 disp (z);

1 个答案:

答案 0 :(得分:0)

使用直肠

此reperm用于精炼编码中的0,1