-
Notifications
You must be signed in to change notification settings - Fork 3
/
Simplex-Algorithm.htm
494 lines (448 loc) · 13.8 KB
/
Simplex-Algorithm.htm
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>The Simplex Algorithm Developed by Donghua Chen</title>
<style>
input
{
font-size:medium;
}
.style1
{
width: 105px;
}
</style>
</head>
<script type="text/javascript">
var VN;
var CN;
var th = new Array();
var bv = new Array();
var zx;
var zy;
var step=0;
function initvalue() {
xx0.value = -2; xx1.value = -1;
x00.value = 0; x01.value = 5;
x10.value = 6; x11.value = 2;
x20.value = 1; x21.value = 1;
b0.value = 15;
b1.value = 24;
b2.value = 5;
}
function initM() {
xx0.value = 2; xx1.value = 3;
x00.value = 0.5; x01.value = .25;
x10.value = 1; x11.value = 3;
x20.value = 1; x21.value = 1;
b0.value = 4;
b1.value = 20;
b2.value = 10;
}
function CreateOJ() {
// create objective function
var s;
var ROW;
var COL;
s = objecttype.options[objecttype.selectedIndex].text + " Z = ";
VN = nvar.value;
CN = ncon.value;
var i;
for (i = 0; i < VN; i++) {
value = "x" + (i + 1);
s += "<input type='text' id='xx" + i + "' size='4'/>" + "<input type='text' value='" + value + "' id='th" + i + "' size='3' />";
if (i != VN - 1) {
s += " + ";
}
}
of.innerHTML = s;
var constring;
var temp = "";
for (j = 0; j < CN; j++) {
for (i = 0; i < VN; i++) {
temp += "<input type='text' id='x" + j + i + "' size='3'/><font> </font>" + "<font id='fo_" + i + "' > " + document.getElementById("th" + i).value + " </font>";
if (i != VN - 1) {
temp += " + ";
}
}
temp += "<select id='equ" + j + "' >";
temp += "<option value='less' selected='selected'><=</option>";
temp += "<option value='larger' >>=</option>";
temp += "<option value='equal'>=</option>";
temp += "</select><font> </font>";
temp += "<input type='text' id='b" + j + "' size='3' /><br/>";
}
constr.innerHTML = temp;
}
</script>
<script type="text/javascript">
var a=new Array();
var b=new Array();
var M = new Array();
var row;
var col;
function begin() {
a = new Array(4);
for (i = 0; i < a.length; i++) {
a[i] = new Array(6);
}
M = new Array(6);
b = new Array(4);
row=4;
col=6;
a[0][0]=-2;a[0][1]=-3;a[0][2]=0;a[0][3]=0;a[0][4]=0;a[0][5]=0;
a[1][0]=0.5;a[1][1]=0.25;a[1][2]=0;a[1][3]=0;a[1][4]=0;a[1][5]=0;
a[2][0]=1;a[2][1]=3;a[2][2]=0;a[2][3]=-1;a[2][4]=1;a[2][5]=0;
a[3][0]=1.0;a[3][1]=1;a[3][2]=0;a[3][3]=0;a[3][4]=0;a[3][5]=1;
b[0]=0;b[1]=4;b[2]=20;b[3]=10;
M[0] = 2; M[1] = 4; M[2] = 0; M[3] = -1; M[4] = 0; M[5] = 0; M[6] = 30;
showtable();
checkmax();
}
function solve() {
check();
}
function showtable() {
display.innerHTML += NewTable();
}
function check()
{
var max=-1000.0;
var m=-100;
var maxm=-1000.0;
var mm=-1;
var tz = -1;
var i;
for(i=0;i<M.length-1;i++){
if(M[i]>maxm){
maxm=M[i];
mm=i;
}
if(M[i]!=0){
tz=0;
}
}
if (maxm <= 0 && maxm != -1000.0 && tz == 0) {
alert("Reach Optimal!");
display.innerHTML+= "Reach Optimal, We get the optimal solution";
return "";
}
if(tz!=0)
{
for(i=0;i<M.length-1;i++){
if(a[0][i]>max){
m=i;
max=a[0][i];
}
}
if (max <= 0) {
alert("Reach Optimal!");
display.innerHTML+= "Reach Optimal, We get the optimal solution";
return "";
}
}else{
m=mm;
}
var vj=m;
var vi=0;
var INF=9e9;
var test=INF;
var minn=INF;
var aa;
for(i=1;i<row;i++){
if(a[i][vj]==0)
test=INF;
else
test=b[i]/a[i][vj];
if(test<minn){
vi=i;
minn=test;
}
}
bv[vi] = vj;
if (minn == INF || vi == 0) {
message.innerHTML = "no solution";
return;
}
var mpt=a[vi][vj];
for(j=0;j<col;j++){
var cc;
cc=a[vi][j]/mpt;
a[vi][j]=cc ;
}
b[vi]=b[vi]/mpt;
var mmp=0;
mmp=M[vj];
var mmj=0;
mmj=a[0][vj];
for(j=0;j<col;j++){
a[0][j]-=a[vi][j]*mmj;
M[j]-=a[vi][j]*mmp;
// System.out.println("M["+j+"] = "+M[j]+"-"+a[vi][j]+"*"+M[vj]+" = "+M[j]);
if(j==col-1){
b[0]-=b[vi]*mmj;
M[M.length-1]-=b[vi]*mmp;
}
}
for(i=1;i<row;i++){
mpt=a[i][vj];
if(i!=vi){
for(j=0;j<col;j++){
a[i][j]-=a[vi][j]*mpt;
}
b[i]-=b[vi]*mpt;
}
}
showtable();
check();
}
function NewTable() {
var s = "<font><b>Step " + step + "</b></font><br/><table border='1' ><tr>";
step++;
var k;
for (i = 0; i < col; i++) {
s += "<td width='50'><b>" + th[i] + "</b></td>";
k = i;
}
s += "<td width='50'><b>" + th[k+1] + "</b></td>";
s += "<td width='50'><b>r.h.s</b></td>";
s += "<td width='50'><b>bv</b></td></tr>";
s += "<tr><td>M</td>";
for (i = 0; i < col+1; i++) {
s += "<td><input type='text' size='7' value='" + M[i] + "'/></td>";
}
s += "</tr>";
for (i = 0; i < row; i++) {
if (i == 0) {
s += "<td width='30'>1</td>";
} else {
s += "<td width='30' >0</td>";
}
for (j = 0; j < col; j++) {
s += "<td ><input type='text' size='7' value='" + a[i][j] + "'/></td>"
}
s += "<td ><input type='text' size='7' value='" + b[i] + "'/></td>";
for (as = 0; as < col; as++) {
var cd=-1;var cc = 0;
if (a[i][as] == 1) {
for (bs = 1; bs < row; bs++) {
if (a[bs][as] == 0) {
cc++;
}
}
if (cc == row - 2) {
//found the base
s += "<td ><input type='text' size='7' value='" + th[as+1] + "=" + b[i] + "'/></td>";
break;
}
}
}
s += "</tr>";
}
s += "</table>";
return s;
}
function InitTable() {
step = 0;
display.innerHTML = "";
var s;
var t = new Array();
var Count = parseInt(VN);
//obtain total counts of the entire caculation table
for (i = 0; i < CN; i++) {
s = document.getElementById("equ" + i).options[document.getElementById("equ" + i).selectedIndex].text;
if (s == "<=") {
Count++;
}
if (s == ">=") {
Count = Count + 2;
}
if (s == "=") {
Count++;
}
}
Count = Count + 1; //add z var
//simplify the objective function
t[0] = new Array(Count);
var tt;
t[0][0] = 1;
var j;
for (j = 1; j <= VN; j++) {
// t[0][j] = -document.getElementById("X" + parseInt(jjj)).value;
tt = -parseFloat(document.getElementById('xx' + (j - 1)).value);
t[0][j] = tt;
}
for (k = j; k < Count; k++) {
t[0][k] = 0;
}
//handle with table header
th[0] = "z";
for (k = 1; k <= VN; k++) {
var xs = 0;
xs = k - 1;
th[k] = document.getElementById("th" + xs).value;
}
// handle the contrainsts
var index;
index = 1;
for (i = 1; i <= CN; i++) {
t[i] = new Array(Count);
t[i][0] = 0;
for (j = 1; j < Count; j++) {
if (j <= VN) {
var temp;
temp = document.getElementById("x" + (i - 1) + (j - 1)).value;
t[i][j] = parseFloat(temp);
}
if (j > VN) {
t[i][j] = 0;
}
}
var bb = "";
var d = 0;
bv = new Array(parseFloat(CN));
bb = document.getElementById("equ" + (i - 1)).options[document.getElementById("equ" + (i - 1)).selectedIndex].text;
aa = objecttype.options[objecttype.selectedIndex].text
if (bb == "<=") {
d = parseInt(VN) + parseInt(index);
t[i][d] = 1;
th[d] = "s" + i;
bv[index] = d;
index++;
}
if (bb == ">=") {
d = parseInt(VN) + parseInt(index);
th[d] = "e" + i;
t[i][d] = -1;
index++;
d = parseInt(VN) + parseInt(index);
th[d] = "a" + i;
t[i][d] = 1;
if (aa == "MAX") {
t[0][d] = "M";
} else {
t[0][d] = "-M";
}
bv[index] = d;
index++;
}
if (bb == "=") {
d = parseInt(VN) + parseInt(index);
th[d] = "a" + i;
t[i][d] = 1;
if (aa == "MAX") {
t[0][d] = "M";
} else {
t[0][d] = "-M";
}
bv[index] = d;
index++;
}
}
//basic var rhs
var rhs = new Array(parseInt(CN) + 1);
rhs[0] = 0;
for (i = 1; i < rhs.length; i++) {
rhs[i] = parseFloat(document.getElementById("b" + (i - 1)).value);
}
////////////////////////////////////////////////////////
a = new Array(parseFloat(CN) + 1);
for (i = 0; i < a.length; i++) {
a[i] = new Array(Count - 1);
}
b = new Array(parseFloat(CN) + 1);
row = parseFloat(CN) + 1;
col = Count - 1;
M = new Array(col+1);
//////////////////////////////////////////////
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
a[i][j] = t[i][j + 1];
}
b[i] = rhs[i];
}
//////////////////////////////////////////
var te;
for(i=0;i<col+1;i++)
M[i]=0;
for (j = 0; j < col; j++) {
if (a[0][j] == "M"||a[0][j]=="-M") {
for (i = 0; i < row; i++) {
if (parseInt(a[i][j]) == 1) {
for (k = 0; k < col; k++) {
if (aa == "MAX") {
M[k] -= a[i][k];
} else {
M[k] += a[i][k];
}
}
M[j] = 0;
a[0][j] = 0;
if (aa == "MAX") {
M[M.length - 1] -= b[i];
} else {
M[M.length - 1] += b[i];
}
break;
}
}
}
}
showtable();
}
</script>
<body background="c.jpg" >
<br />
<table style="width: 107%;">
<tr>
<td class="style1">
<img width="100" src="bjlogo.jpg" /></td>
<td>
<h1>The Simplex Algorithm & Big M Method Program</h1>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
<div>Developer: Donghua Chen <br />
<br />
</div>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
<hr />
<font> Objective Function Type:
</font><select id="objecttype" name="D1" >
<option value="MAX" >MAX</option>
<option value="MIN" selected="selected">MIN</option>
</select><br />
<br />
<font>The Number of Variances : </font><input type='text' id='nvar'
style="width: 163px" /><br />
<br />
<font>The Number of Constrains: </font>
<input type='text' id='ncon' /><br />
<br />
<input type="button" value="CreateTable" onclick="CreateOJ()" /> <input type="button" value="Simple Example" onclick="initvalue()" />
<input type="button" value="BigM Example" onclick="initM()" />
</td>
</tr>
</table>
<hr />
<div><b>Objective Function:</b></div>
<div id="of" align="center" >
</div>
<div><b>Subject TO:</b></div>
<div id="constr" align="center"> </div>
<hr />
<input type="button" value="InitTable" onclick="InitTable()" id="beginbutton"/>
<input type="button" value="Do" onclick="solve()" id="bd" />
<div id="display" />
<div id="message" />
</body>
</html>