356 Appendix B
remove_column = 3;
nonbasic_set(remove_column) = [];
N(:,remove_column) = [];
cn(remove_column) = [];
cn_cap = cn-y*N;
fprintf('\n ----Table after removing artificial
variable------\n')
basic_set
nonbasic_set
Initial_Table = [eye(length(B)) inv(B)*N b_cap]
Cost = [cb_ini cn_cap -zz]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code dual.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The matrix A and b corresponds to equation Ax=b
% c -> vector of cost coefficients
% basic_set -> set of basic variables
% nonbasic_set -> set of nonbasic variables
% B -> matrix containing basic variable columns of A
% N -> matrix containing nonbasic variable columns of A
% xb -> basic variables
% y -> simplex multipliers
% cb -> cost coefficients of basic variables
% cn -> cost coefficients of nonbasic variables
%
clear all
clc
format rational
format compact
A = [-1 0 1 0 0 0;
0 -1 0 1 0 0;
-2 -1 0 0 1 0;
-1 -3 0 0 0 1];
b = [-3;-4;-25;-26];
c = [9; 8; 0; 0; 0;0];
basic_set = [3 4 5 6];
nonbasic_set = [1 2];
for i = 1:length(basic_set)
B(:,i) = A(:,basic_set(i));
cb(i) = c(basic_set(i));
end
for i = 1:length(nonbasic_set)
N(:,i) = A(:,nonbasic_set(i));
cn(i) = c(nonbasic_set(i));
end
cn_cap = cn;
cb_ini = cb;
357Appendix B
b_cap = b;
zz = 0;
fprintf('\n ________________________________________\n')
basic_set
nonbasic_set
Initial_Table = [B N b_cap]
Cost = [cb cn_cap zz]
for i = 1:4
[minvalue leaving_basic_variable] = min(b_cap);
mat1 = inv(B)*N;
entering_row = mat1(leaving_basic_variable,:);
ratios = -1*(cn_cap'./entering_row');
[min_ratio entering_basic_variable] = min(ratios);
while min_ratio<0
ratios(entering_basic_variable) = inf;
[min_ratio entering_basic_variable] = min(ratios);
end
temp_basic_set = basic_set;
temp_nonbasic_set = nonbasic_set;
temp_cb = cb;
temp_cn = cn;
basi c_set(leaving_basic_variable) = temp_nonbasic_
set(entering_basic_variable);
nonb asic_set(entering_basic_variable) = temp_basic_
set(leaving_basic_variable);
cb( leaving_basic_variable) = temp_cn(entering_basic_
variable);
cn( entering_basic_variable) = temp_cb(leaving_basic_
variable);
aa(nonbasic_set) = cn;
cn = aa(sort(nonbasic_set));
nonbasic_set = sort(nonbasic_set);
for ii = 1:length(basic_set)
B(:,ii) = A(:,basic_set(ii));
end
for ii = 1:length(nonbasic_set)
N(:,ii) = A(:,nonbasic_set(ii));
end
xb = inv(B)*b;
y = cb*inv(B);
cn_cap = cn-y*N;
b_cap = xb;
zz = cb*xb;
fprintf('\n ________________________________________\n')
basic_set
nonbasic_set
Table = [eye(length(B)) inv(B)*N b_cap]
Cost = [cb_ini cn_cap -zz]
if b_cap >= 0
break;
358 Appendix B
end
end
fprintf('\n ------FINAL SOLUTION------\n')
basic_set
xb
zz
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code interior.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Affine scaling method
%
clear all
clc
A = [3 1;
1 2;
1 0];
b = [10;8;3];
c = [6;7];
x = [0;0];
obj_prev = c’*x;
gamma = 0.9;
tolerance = 1e-5;
for i = 1:10
vk = b-A*x;
dv = diag(vk);
hx = inv(A'*dv^-2*A)*c;
hv = -A*hx;
for j = 1:length(hv)
if hv(j)<0
var(j) = -vk(j)/hv(j);
else
var(j) = inf;
end
end
alpha = gamma*min(var);
x = x + alpha*hx;
objective = c’*x;
if abs(objective-obj_prev)<tolerance
break;
end
obj_prev = objective;
end
objective
x
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359Appendix B
Chapter 5
Code Name Details
prob.m Genetic algorithm (GA; main program)
in.m Inputs to GA
roulett.m Roulette wheel selection
tournament.m Tournament selection
func.m Test function to be included here (for GA)
simann.m Simulated annealing
func1.m Objective function to be included here (for PSO and simulated
annealing)
pso.m Particle swarm optimization (PSO)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File name prob.m
% Genetic algorithm - main program
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
clear all
clc
format long g;
% Read the input file
in;
% INITIALIZATION OF STRINGS
string = 0;
for i = 1:n_of_v
string = string+n_of_bits(i);
end
for j = 1:n_of_p
for i = 1:string
r(j,i) = rand;
if r(j,i)< 0.5
r(j,i) = 0;
else
r(j,i) = 1;
end
end
end
% MAIN LOOP
for g = 1:n_of_g
% Decoded value of r (with left bit as MSB)
deci = cell(n_of_v,1);
decoded = cell(n_of_v,1);
dum1 = 1;
dummy = n_of_bits(1);
for i = 1:n_of_v
deci{i} = bi2de(r(:,dum1:dummy),'left-msb');
dum1 = dum1+n_of_bits(i);
while dummy<string
dummy = dummy+n_of_bits(i+1);
end
360 Appendix B
% NORMALIZE TO THE VARIABLE RANGE
x1(:,i) = deci{i};
decoded{i} = range(i,1)+((range(i,2)-range(i,1))/(2^n_of_bits(i)-
1))*x1(:,i);
xxx(:,i) = decoded{i};
end
% FUNCTION EVALUATION
for i = 1:n_of_p
[fitness1(i),constraint(i,:)] = func(xxx(i,:));
end
fitness = fitness1';
for hh = 1:length(fitness)
if fitness(hh) < 0
flag1 = 1;
end
end
if flag1 == 1
[factor,indices] = min(fitness);
fitness1 = -factor+fitness;
end
% CALLING ROULETTE WHEEL
if tourni_flag ~= 1
if problem == 'min'
fitness2 = 1./(1+fitness1);
end
[best_fit(g),indi(g)] = max(fitness2);
best_var(g,:) = xxx(indi(g),:);
if problem == 'min'
best_fit(g) = fitness(indi(g));
end
% CUMULATIVE PROBABILITY
s = sum(fitness2);
cum_prob = fitness2/s;
roulett;
else
[best_fit(g),indi(g)] = min(fitness);
average_fitness = mean(fitness);
best_var(g,:) = xxx(indi(g),:);
% CALLING TOURNAMENT SELECTION
tournament;
% IF THIS IS A CONSTRAINT PROBLEM THEN WE HAVE TO USE THIS
if n_of_c>=0
best_fit(g) = min_fit;
best_var(g,:) = xxx(indi(g),:);
end
end
% CROSSOVER
for k = 1:2:n_of_p
parent1 = r_new(round(random('unif',0.5,n_of_p+0.5)),:);
parent2 = r_new(round(random('unif',0.5,n_of_p+0.5)),:);
if multi_crossover == 0
cross_o_pos = round(random('unif',1.5,string+0.5-1));
child1(1:cross_o_pos) = parent2(1:cross_o_pos);
child1(cross_o_pos+1:string) = parent1(cross_o_pos+1:string);

Get Optimization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.