2016-06-20 30 views
1

你好,我是Matlab優化的初學者。我想根據變量的兩個矩陣(x(1)-x(72)繼續變量和x(73)-x(90)二進制變量)使函數最小化。代碼如下。我不確定是否可以在fmincon函數中使用二進制和連續變量。我怎樣才能合併二進制變量?請注意,約束是線性的。任何幫助將不勝感激。在fmincon中可以有二進制和連續變量嗎?

function [c, ceq] = confun(x) 
dA1=5; 
dA2=9; 
dB1=7; 
dB2=7; 
dC1=10; 
dC2=5; 
k=2; 
% Nonlinear inequality constraints 
c = [ x(73)+x(74)+x(75)+x(76)+x(77)+x(78)-k; 
    x(79)+x(80)+x(81)+x(82)+x(83)+x(84)-k; 
    x(85)+x(86)+x(87)+x(88)+x(89)+x(90)-k; 
    x(1)-dA1*x(73); 
    x(2)-dA1*x(79); 
    x(13)-dA1*x(74); 
    x(14)-dA1*x(80); 
    x(25)-dA1*x(75); 
    x(26)-dA1*x(81); 
    x(37)-dA1*x(76); 
    x(38)-dA1*x(79); 
    x(49)-dA1*x(77); 
    x(50)-dA1*x(83); 
    x(61)-dA1*x(78); 
    x(62)-dA1*x(84); 
    x(3)-dB1*x(73); 
    x(4)-dB1*x(79); 
    x(15)-dB1*x(74); 
    x(16)-dB1*x(80); 
    x(27)-dB1*x(75); 
    x(28)-dB1*x(81); 
    x(39)-dB1*x(76); 
    x(40)-dB1*x(82); 
    x(51)-dB1*x(77); 
    x(52)-dB1*x(83); 
    x(63)-dB1*x(78); 
    x(64)-dB1*x(84); 
    x(5)-dC1*x(73); 
    x(6)-dC1*x(79); 
    x(17)-dC1*x(74); 
    x(18)-dC1*x(80); 
    x(29)-dC1*x(75); 
    x(30)-dC1*x(81); 
    x(41)-dC1*x(76); 
    x(42)-dC1*x(82); 
    x(53)-dC1*x(77); 
    x(54)-dC1*x(83); 
    x(65)-dC1*x(78); 
    x(66)-dC1*x(84); 
    x(7)-dA2*x(79); 
    x(8)-dA2*x(85); 
    x(19)-dA2*x(80); 
    x(20)-dA2*x(86); 
    x(31)-dA2*x(81); 
    x(32)-dA2*x(87); 
    x(43)-dA2*x(82); 
    x(44)-dA2*x(88); 
    x(55)-dA2*x(83); 
    x(56)-dA2*x(89); 
    x(67)-dA2*x(84); 
    x(68)-dA2*x(90); 
    x(9)-dB2*x(79); 
    x(10)-dB2*x(85); 
    x(21)-dB2*x(80); 
    x(22)-dB2*x(86); 
    x(33)-dB2*x(81); 
    x(34)-dB2*x(87); 
    x(45)-dB2*x(82); 
    x(46)-dB2*x(88); 
    x(57)-dB2*x(83); 
    x(58)-dB2*x(89); 
    x(69)-dB2*x(84); 
    x(70)-dB2*x(90); 
    x(11)-dC2*x(79); 
    x(12)-dC2*x(85); 
    x(23)-dC2*x(80); 
    x(24)-dC2*x(86); 
    x(35)-dC2*x(81); 
    x(36)-dC2*x(87); 
    x(47)-dC2*x(82); 
    x(48)-dC2*x(88); 
    x(59)-dC2*x(83); 
    x(60)-dC2*x(89); 
    x(71)-dC2*x(84); 
    x(72)-dC2*x(90); 
    x(1) + x(3) + x(5)-15; 
    x(7) + x(9) + x(11) + x(2) + x(4)+ x(6)-15; 
    x(8) + x(10) + x(12)-15; 
    x(13) + x(15) + x(17)-15; 
    x(19) + x(21) + x(23) + x(14) + x(16)+ x(18)-15; 
    x(20) + x(22) + x(24)-15; 
    x(25) + x(27) + x(29)-15; 
    x(31) + x(33) + x(35) + x(26) + x(28)+ x(30)-15; 
    x(32) + x(34) + x(36)-15; 
    x(37) + x(39) + x(41)-15; 
    x(43) + x(45) + x(47) + x(38) + x(40)+ x(42)-15; 
    x(44) + x(46) + x(48)-15; 
    x(49) + x(51) + x(53)-15; 
    x(55) + x(57) + x(59) + x(50) + x(52)+ x(54)-15; 
    x(56) + x(58) + x(60)-15; 
    x(61) + x(63) + x(65)-15; 
    x(67) + x(69) + x(71) + x(62) + x(64)+ x(66)-15; 
    x(68) + x(70) + x(72)-15; 
    x(1)+x(2)+ x(13)+x(14)+x(25)+x(26)+ x(37)+x(38)+ x(49)+x(50)+ x(61)+x(62)-dA1; 
    x(7)+x(8)+ x(19)+x(20)+x(31)+x(32)+ x(43)+x(44)+ x(55)+x(56)+ x(67)+x(68)-dA2; 
    x(3)+x(4)+ x(15)+x(16)+x(27)+x(28)+ x(39)+x(40)+ x(51)+x(52)+ x(63)+x(64)-dB1; 
    x(9)+x(10)+ x(21)+x(22)+x(33)+x(34)+ x(45)+x(46)+ x(57)+x(58)+ x(69)+x(70)-dB2 ]; 

    % Nonlinear equality constraints 
    ceq = []; 

    end 


    function f = objfun(x) 
    f = (339*x(1)*x(2))/6050 + (68*x(7)*x(8))/3969 + (339*x(1)*x(13))/6050 + (339*x(1)*x(14))/6050 + (339*x(2)*x(13))/6050 + (339*x(2)*x(14))/6050 + (68*x(7)*x(19))/3969 + (68*x(7)*x(8))/3969 + (68*x(8)*x(19))/3969 + (68*x(8)*x(20))/3969 + (339*x(1)*x(25))/6050 ... 

    end 
    clc; 
    clear; 
    close all; 
    ub = [Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf;Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf;Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf; Inf;Inf;Inf;1;1;1; 1;1;1; 1;1;1; 1;1;1; 1;1;1; 1;1;1]; 
    x0 = ones(90,1);  
    options = optimset('Algorithm','active-set'); 
    %options = optimoptions(@fmincon,'Algorithm','sqp'); 
    [x,fval] = fmincon(@objfun,x0,[],[],[],[],[],ub,@confun,options); 


    I get these warnings and errors: 
     Attempt to reference field of non-structure array. 
     Error in confun (line 10) 
     c = [ x(73)+x(74)+x(75)+x(76)+x(77)+x(78)-k; 
     Error in fmincon (line 651) 
     [ctmp,ceqtmp] = feval(confcn{3},X,varargin{:}); 
     Error in nonlinear (line 23) 
     [x,fval] = fmincon(@objfun,x0,[],[],[],[],[],ub,@confun,options); 
     Caused by: 
      Failure in initial user-supplied nonlinear constraint function evaluation. FMINCON cannot continue 
+1

您已經使用'.'試圖將'dA1'或'dB1'與'x'相乘,但'.'運算符用於訪問MATLAB中結構的字段。用'*'替換'.'代替2個標量的乘法並更新你的問題。 – edwinksl

+0

看起來像一個二次客觀和線性約束。這意味着你的問題是一個MIQP(混合整數二次規劃)問題。取決於問題是否凸起,是否有解決方案。 –

+0

@Erwin問題是凸的! – rezzz

回答

0

您將無法使用fmincon(),因爲它不處理整數參數。你需要一個求解'混合整數規劃'的解決方案,它涉及同時具有實數值和整數值的參數。如果您可以將您的問題制定爲混合整數線性程序(似乎並非如此),那麼您可以使用intlinprog()。請參閱herehere。遺傳算法(使用Mathworks全局優化工具箱)可能能夠解決這個問題。見here。否則,您可以查看外部工具箱。有用於CPLEX和GUROBI的Matlab接口。這些是商業軟件,但免費的學術許可證可用。

相關問題