Effacer les filtres
Effacer les filtres

Help me to run the code

2 vues (au cours des 30 derniers jours)
MINATI PATRA
MINATI PATRA le 29 Oct 2021
Commenté : MINATI PATRA le 13 Nov 2021
% file modified by sujogya %dated 9.1.2021
% min z=x1-3x2+2x3 % max z'=-x1+3x2-2x3 % s.t. %3x1-x2+2x3<=7, %-2x1+4x2+0x3<=12, %-4x1+3x2+8x3<=10
NVar = 3; C = [-1 3 2]; CMat = [3 -1 2;-2 4 0;-4 3 8]; b = [7; 12; 10;];
s = eye(size(CMat,1)); A = [CMat s b]; cost = zeros(1,size(A,2)); cost(1:NVar) = C;
%% Constraint
BV = NVar+1:1:size(A,2)-1; zjcj = cost(BV)*A - cost;
%% print the Table
zcj = [zjcj;A]; simpletable = array2table(zcj);
simpletable.Properties.VariableNames(1:size(zcj,2)) = {'x_1','x_2','x_3','s_1','s_2','s_3','sol'}
if any(zjcj<0);
fprintf('the current solution is not optimal');
fprintf('\n--------------next iteration----------')
disp('old basic variable(BV)=')
else('optimal solution reached');
end
disp(BV);
%% find entering variable
zc = zjcj(1:end-1); [entercol,pvt_col] = min(zc);
fprintf('the most negative element in zj-cj row is %d corresponding to column%d\n',entercol,pvt_col)
fprintf('entering variable is %d\n',pvt_col);
%% finding leaving variable
sol = A(:,end)
column = A(:,pvt_col);
if all(column <= 0)
printf(' solution is unbdd');
for i = 1:size(column,1)
if column(i)>0
ratio(i) = sol(i)./column(i);
else
ratio(i) = inf
end
end
[MinRatio, pvt_row] = min(ratio);
fprintf('min ratio correspond to pivot row is %d\n',pvt_row);
fprintf('leaving variable %d\n',BV(pvt_row));
end
%% Finding the minimum
BV(pvt_row) = pvt_col;
disp('New basic variables(BV)=');
disp(BV);
%% Pivot key
pivot_key = A(pvt_row,pvt_col);
%%% update the table
A(pvt_row,:) = A(pvt_row,:)./pvt_key;
for i = 1:size(A,1)
if i ~= pvt_row
A(i,:) = A(i,:) - A(i,pvt_col).*A(pvt_row,:);
end
zjcj = zjcj-zjcj(pvt_col).*A(pvt_row,:);
end
%%% for printing purpose
zcj = [zjcj;A]; simptable = array2table(zcj);
simptable.properties.variable(1:size(zcj,2)) = {'x_1','x_2','x_3','s_1','s_2','s_3','sol'};
%%%% simplex method start
run = true
while run
if any(zjcj < 0);
fprintf(' the current solution is not optimal\n')
fprintf(' ----next iteration start----\n')
disp('the old basic variable (BV)=');
disp(BV);
%% print the optimal solution
BFS = zeros(1,size(A,2)); BFS(BV) = A(:,end); BFS(end) = sum(BFS.*cost);
current_BFS = array2table(BFS);
current_BFS.properties.variablenames(1:size(current_BFS,2)) = {'x_1','x_2','x_3','s_1','s_2','s_3','sol'};
else
run = false
fprintf('-------**********--------\n')
fprintf('the solution is optimal\n')
disp('optimal solution')
end
end

Réponse acceptée

Walter Roberson
Walter Roberson le 30 Oct 2021
if all(column <= 0)
if that is false, then
[MinRatio, pvt_row] = min(ratio);
that is not executed, and then
BV(pvt_row) = pvt_col;
pvt_row is not defined -- because you only assign to it if all(column <= 0)
  3 commentaires
Walter Roberson
Walter Roberson le 11 Nov 2021
Maybe V(pvt_row) = pvt_col; should be inside the if
MINATI PATRA
MINATI PATRA le 13 Nov 2021
@ Thank you Walter, It is done.

Connectez-vous pour commenter.

Plus de réponses (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 30 Oct 2021
Some of the assigned variables don't get their values from the second [if .. end] and [for end] operations, e.g.: pvt_row
Thus, its default value has to be assigned. Moreover, one variable name is misspelled: pivot_key vs. pivt_key
There is no need to rename simptable variable names, since it is already done.
In addition, to rename table variable names (e.g. current_BFS), it is better to use renamevars().
  1 commentaire
MINATI PATRA
MINATI PATRA le 11 Nov 2021
@ Sulaymon
Still not working

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by