2015-05-28 44 views
0

我想實現分支和界限算法的遞歸函數。每次遞歸調用我的算法時,我都會在遞歸調用中更改我的lb和ub值。它顯示錯誤UB must be a real valued nx by 1 column vertor error in MATLAB。我的代碼如下附:UB必須是一個真正的價值nx由1列垂直誤差在MATLAB

function [cost] = BB(c, A, b, lb, ub, ctype, vartype, s, xmin, fmin, fid, NumberofNodes,min_cost, val); 



[xmin_last, fmin_last, status] = glpk (c, A, b, lb, ub, ctype, vartype, s)  %GLPK function 
frac_value_1= find(xmin_last > 0.999); 

xmin(frac_value_1)=1; 

frac_value_2= find(xmin_last < 0.0001); 

xmin(frac_value_2)=0; 


frac_value=intersect(find (xmin_last > 0) , find(xmin_last < 1)) ; %positions of fractional variables 

size(frac_value,1); 
    if (isempty(frac_value)) 
    fprintf(fid,'no fractional value'); 
    fprintf(fid,'\n\n'); 

     cost = fmin_last 
     return 

else 
round_value=frac_value(1) 

one_value_cost=find (xmin_last== 1)  %variables with value 1 


    zero_value_cost=find (xmin_last==0)  %variables with value 0 


ub(zero_value_cost)=0   %changing ub for 0 values 

%val=0 
[cost_0] = BB(c, A, b, lb, ub(round_value)=0, ctype, vartype, s, xmin_last(round_value)=0, fmin_last, fid, NumberofNodes,min_cost,val=0); 

%val=1 
[cost_1] = BB(c, A, b, lb(round_value)=1, ub, ctype, vartype, s, xmin_last(round_value)=1, fmin_last, fid, NumberofNodes,min_cost, val=1); 



end 

min_cost = min(cost_0, cost_1) 
endfunction; 

它示出了用於ub(round_value)=0lb(round_value)=1錯誤。任何幫助將不勝感激

+1

請提供完整的錯誤和堆棧跟蹤。我還建議使用[MATLAB的調試器](http://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html)遍歷代碼並評估變量的值工作區導致錯誤。另見['dbstop if error'](http://www.mathworks.com/help/matlab/ref/dbstop.html) – excaza

回答

0

如果查找函數找到多個值,它將返回所有條目等於零或1的向量。您應該嘗試選擇zero_value_cost的第一個元素。

如果find函數沒有找到一個或一個零,它將返回一個空向量。你確定這些值是你正在搜索的向量嗎?

相關問題