-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path(4)Algebraic_Method_BFS.m
264 lines (246 loc) · 8.32 KB
/
(4)Algebraic_Method_BFS.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
%% SHREEYA CHATTERJI
%% QUESTION 1
% TO OBTAIN BFS USING ALGEBRAIC METHOD
% Question
% Max Z= 2x1+3x2+4x3+7x4
% st: 2x1+3x2-x3+4x4=8
% x1-2x2+6x3-7x4=-3
% %xi>=0; i=1,2,3,4
clc
clear all
format short
% PHASE-1: Input the parameter
c=[2,3,4,7]; %Objective function
A=[2 3 -1 4; -1 2 -6 7]; %Coefficient Matrix
B=[8;3];%RHS of const
objective=1; %1 for max and -1 for minimization problem
%Number of possible solutions: nCm:nchoosek
% PHASE-2: Number of constraint and variable
m=size(A,1); %number of constraints
n=size(A,2); % number of variables
% PHASE-3: Compute the ncm Basic Solutions: The max number of basic
% solutions will always be nCm
nab=nchoosek(n,m); %total number of atmost basic solution
t=nchoosek(1:n,m); %from this we can extract our set of variables that we need to equate to zero
% PHASE-4:Construct the basic solution
% for this n>m must be satisfied
sol=[]; %default solution is zero (Empty Matrix)
if n>=m %if this is not statisfied then we can not have solutions
for i=1:nab
y=zeros(n,1);
%selecting all rows for a specific column where for t we are taking all columns for a
% specific row (which is basically the variables that are equated to zero)
X=(A(:,t(i,:)))\B;
%fetching values from A matrix for the rows correspond
%checking feasibility condition
if all(X>=0 & X~=inf & X~=-inf)
y(t(i,:))=X;
sol=[sol y];
end
end
disp("Solution: ");
disp(sol);
else
error('No. of variables is less than number of constraints')
end
if any(X == 0)
fprintf("DEGENERATE SOLUTION");
else
fprintf('NON-DEGENERATE SOLUTION\n');
end
%PHASE 5: To find optimal solution
Z=c*sol; %finding the values corresponding to each point
if(objective==1)
[Zmax,Zindex]=max(Z);%storing the max value of Z and the col in which this max value resides
else
[Zmax,Zindex]=min(Z);%storing the min value of Z and the col in which this min value resides
end
BFS=sol(:,Zindex);%basic feasible solution
[Optimal_Value]=[BFS' Zmax];
Optimal_bfs=array2table(Optimal_Value);
Optimal_bfs.Properties.VariableNames(1:size(Optimal_bfs,2))={'x1','x2','x3','x4','Optimal Value of Z'};
disp(Optimal_bfs);
%% QUESTION 2
% TO OBTAIN BFS USING ALGEBRAIC METHOD
% Question 2
% Max Z= -x1+2x2-x3
% st: x1+s1=4
% x2+s2=4
% -x1+x2+s3=6
% -x1+2x3+s4=4
% x1,x2,x3>=0
clc
clear all
format short
% PHASE-1: Input the parameter
c=[-1,2,-1,0,0,0,0]; %Objective function
A=[1,0,0,1,0,0,0;0,1,0,0,1,0,0;-1,1,0,0,0,1,0;-1,0,2,0,0,0,1]; %Coefficient Matrix
B=[4;4;6;4];%RHS of const
objective=1; %1 for max and -1 for minimization problem
%Number of possible solutions: nCm:nchoosek
% PHASE-2: Number of constraint and variable
m=size(A,1); %number of constraints
n=size(A,2); % number of variables
% PHASE-3: Compute the ncm Basic Solutions: The max number of basic
% solutions will always be nCm
nab=nchoosek(n,m); %total number of atmost basic solution
t=nchoosek(1:n,m); %from this we can extract our set of variables that we need to equate to zero
% PHASE-4:Construct the basic solution
% for this n>m must be satisfied
sol=[]; %default solution is zero (Empty Matrix)
if n>=m %if this is not statisfied then we can not have solutions
for i = 1:nab
y = zeros(n, 1);
% Check if the selected variables form a singular matrix
if rank(A(:, t(i, :))) == m
X = A(:, t(i, :)) \ B; % Solve for basic variables
if all(X >= 0)
y(t(i, :)) = X;
sol = [sol y];
end
end
end
disp("Solution: ");
disp(sol);
else
error('No. of variables is less than number of constraints')
end
if any(X == 0)
fprintf("DEGENERATE SOLUTION");
else
fprintf('NON-DEGENERATE SOLUTION\n');
end
%PHASE 5: To find optimal solution
Z=c*sol; %finding the values corresponding to each point
if(objective==1)
[Zmax,Zindex]=max(Z);%storing the max value of Z and the col in which this max value resides
else
[Zmax,Zindex]=min(Z);%storing the min value of Z and the col in which this min value resides
end
BFS=sol(:,Zindex);%basic feasible solution
[Optimal_Value]=[BFS' Zmax];
Optimal_bfs=array2table(Optimal_Value);
Optimal_bfs.Properties.VariableNames(1:size(Optimal_bfs,2))={'x1','x2','x3','s1','s2','s3','s4','Optimal Value of Z'};
disp(Optimal_bfs);
%% QUESTION 3
% TO OBTAIN BFS USING ALGEBRAIC METHOD
% Question
% Min Z= 5x2-2x1
% st: 2x1+5x2+s1=8
% x1+x2+s2=2
% %xi>=0
clc
clear all
format short
% PHASE-1: Input the parameter
c=[-2,5,0,0]; %Objective function
A=[2,5,1,0;1,1,0,1]; %Coefficient Matrix
B=[8;2];%RHS of const
objective=-1; %1 for max and -1 for minimization problem
%Number of possible solutions: nCm:nchoosek
% PHASE-2: Number of constraint and variable
m=size(A,1); %number of constraints
n=size(A,2); % number of variables
% PHASE-3: Compute the ncm Basic Solutions: The max number of basic
% solutions will always be nCm
nab=nchoosek(n,m); %total number of atmost basic solution
t=nchoosek(1:n,m); %from this we can extract our set of variables that we need to equate to zero
% PHASE-4:Construct the basic solution
% for this n>m must be satisfied
sol=[]; %default solution is zero (Empty Matrix)
if n>=m %if this is not statisfied then we can not have solutions
for i=1:nab
y=zeros(n,1);
%selecting all rows for a specific column where for t we are taking all columns for a
% specific row (which is basically the variables that are equated to zero)
X=(A(:,t(i,:)))\B;
%fetching values from A matrix for the rows correspond
%checking feasibility condition
if all(X>=0 & X~=inf & X~=-inf)
y(t(i,:))=X;
sol=[sol y];
end
end
disp("Solution: ");
disp(sol);
else
error('No. of variables is less than number of constraints')
end
if any(X == 0)
fprintf("DEGENERATE SOLUTION\n");
else
fprintf('NON-DEGENERATE SOLUTION\n');
end
%PHASE 5: To find optimal solution
Z=c*sol; %finding the values corresponding to each point
if(objective==1)
[Zmax,Zindex]=max(Z);%storing the max value of Z and the col in which this max value resides
else
[Zmax,Zindex]=min(Z);%storing the min value of Z and the col in which this min value resides
end
%Optimal BFS
BFS=sol(:,Zindex);%basic feasible solution
[Optimal_Value]=[BFS' Zmax];
Optimal_bfs=array2table(Optimal_Value);
Optimal_bfs.Properties.VariableNames(1:size(Optimal_bfs,2))={'x1','x2','s1','s2','Optimal Value of Z'};
disp(Optimal_bfs);
%% QUESTION 4
% TO OBTAIN BFS USING ALGEBRAIC METHOD
% Question
% Max Z= x1+x2+x3+0s1+0s2
% st: x1+x2+s1=1
% -x2+x3+s2=0
% %xi>=0
clc
clear all
format short
% PHASE-1: Input the parameter
c=[1,1,1,0,0]; %Objective function
A=[1,1,0,1,0;0,-1,1,0,1]; %Coefficient Matrix
B=[1;0];%RHS of const
objective=1; %1 for max and -1 for minimization problem
%Number of possible solutions: nCm:nchoosek
% PHASE-2: Number of constraint and variable
m=size(A,1); %number of constraints
n=size(A,2); % number of variables
% PHASE-3: Compute the ncm Basic Solutions: The max number of basic
% solutions will always be nCm
nab=nchoosek(n,m); %total number of atmost basic solution
t=nchoosek(1:n,m); %from this we can extract our set of variables that we need to equate to zero
% PHASE-4:Construct the basic solution
% for this n>m must be satisfied
sol=[]; %default solution is zero (Empty Matrix)
if n>=m %if this is not statisfied then we can not have solutions
for i = 1:nab
y = zeros(n, 1);
% Check if the selected variables form a singular matrix
if rank(A(:, t(i, :))) == m
X = A(:, t(i, :)) \ B; % Solve for basic variables
if all(X >= 0)
y(t(i, :)) = X;
sol = [sol y];
end
end
end
disp("Solution: ");
disp(sol);
else
error('No. of variables is less than number of constraints')
end
if any(X == 0)
fprintf("DEGENERATE SOLUTION\n");
else
fprintf('NON-DEGENERATE SOLUTION\n');
end
%PHASE 5: To find optimal solution
Z=c*sol; %finding the values corresponding to each point
if(objective==1)
[Zmax,Zindex]=max(Z);%storing the max value of Z and the col in which this max value resides
else
[Zmax,Zindex]=min(Z);%storing the min value of Z and the col in which this min value resides
end
BFS=sol(:,Zindex);%basic feasible solution
[Optimal_Value]=[BFS' Zmax];
Optimal_bfs=array2table(Optimal_Value);
Optimal_bfs.Properties.VariableNames(1:size(Optimal_bfs,2))={'x1','x2','x3','s1','s2','Optimal Value of Z'};
disp(Optimal_bfs);