· 9 years ago · Nov 02, 2016, 05:22 AM
1// *** ERROR HANDLING
2
3window.onerror = myErrorTrap;
4
5var epsilon = .00000000000001 // 10^-14
6
7var maxSigDig = 13; // max number of sig digits
8
9// var exit = false; // get out of here
10
11var okToRoll = true; // preliminary testing results
12
13var stepName = ""; // for error trap
14
15var tab = unescape( "%09" ); // these are now the appropriate strings;
16
17var cr = unescape( "%0D" );
18
19var lf = unescape( "%0A" );
20
21var symb = unescape( "%C5" );
22
23var backSlash = unescape( "%5C" );
24
25var gteSymbol = unescape( "%B3" ); // symbols in old netscape
26
27var lteSymbol = unescape( "%B2" );
28
29var lte = unescape ("%u2264"); // actual symbol in IE
30
31var gte = unescape ("%u2265");
32
33var comma = ",";
34
35var singular = false;
36
37var msFormat = false;
38
39var maxRows = 15;
40
41var maxCols = 30;
42
43var numRows = 0;
44
45var numCols = 0;
46
47var numConstraints = 0;
48
49var maximization = true; // this is a max problem
50
51var phase1 = false; // are we in phase 1?
52
53var objectiveName = "p";
54
55var numVariables = 1;
56
57var variables = [];
58
59var theTableau = new makeArray2 (1,1);
60
61var theStringTableau = new makeArray2 (1,1); // to display steps in the computation
62
63var starred = new makeArray(1); // starred rows
64
65var TableauNumber = 1; // the number of tableaus
66
67var maxSteps = 50; // maximum number of tableaux
68
69var numSigDigs = 6; // default accuracy
70
71var activeVars = new Array(); // active variables
72
73// old globals below...
74
75var maxDenom = 1000; // for fraction approximation
76
77var tol = .000000001; // for 10 digit accuracy guaranteed cutoff for fraction approx not yet implemented
78
79var tooBigString = "Too many matrices in your expression," + cr + "or your expression is too complicated." + cr +"Please keep it simple!"
80
81
82
83// the Instructions
84
85
86
87var theSampleLPString = "Maximize p = (1/2)x + 3y + z + 4w subject to" + cr + "x + y + z + w <= 40" + cr + "2x + y - z - w >= 10" + cr + "w - y >= 10";
88
89
90
91var theInstructionString = "Notes on formatting: " + cr + " (1) Variable names must begin with letters." + cr + tab + tab + " (eg. p, x1, loss, z, s, t, u...) " + cr + " (2) For fraction inputs, keep the variable on the right." + cr + tab + tab + " (eg. (1/3)x and not x/3) " + cr + " (3) Every variable you use must appear in the objective function, (but not"+cr+" necessarily in the constraints). " + cr + tab + tab + " (eg. p = 0x + 2y + 0z ) " + cr + " (4) The words 'maximize' (or 'minimize') and 'subject to' must appear. " + cr + " (5) Each inequality should be on its own line, as shown. " + cr + " (6) No need to enter the default constraints: x >= 0, y >= 0, z >= 0 etc."
92
93
94
95// end instructions
96
97
98
99
100
101
102
103
104
105var fractionMode = false;
106
107var integerMode = false;
108
109var okToRoll = true;
110
111var browserName = navigator.appName;
112
113var browserVersion = navigator.appVersion;
114
115if ( (browserName == "Netscape") && (parseInt(browserVersion) >= 3)) browserName = "N";
116
117else if ( (browserName == "Microsoft Internet Explorer") && (parseInt(browserVersion) >= 3) ) browserName = "M";
118
119
120
121// ****************** ERROR HANDLER *************
122
123function myErrorTrap(message,url,linenumber) {
124
125alert("Sorry, I can't process this." + cr +" Press 'Example' for general information.");
126
127return (true);
128
129} // end of on error
130
131
132
133// ******************** MATH UTILITIES ******************
134
135function hcf (a,b) {
136
137var bigger = Math.abs(a);
138
139var smaller = Math.abs(b);
140
141var x = 0;
142
143var theResult = 1;
144
145if ( (a == 0) || (b == 0) ) return(1);
146
147if (smaller > bigger) {x = bigger; bigger = smaller; smaller = x}
148
149
150
151var testRatio = roundSigDig(bigger/smaller, 11);
152
153var testRatio2 = 0;
154
155if (testRatio == Math.floor(testRatio) ) return (smaller)
156
157else
158
159 {
160
161 // look for a factor of the smaller, deplete it by that factor and multiply bigger by it
162
163 var found = false;
164
165 var upperlimit = smaller;
166
167 for (var i = upperlimit; i >= 2; i--)
168
169 {
170
171 testRatio = roundSigDig(smaller/i, 10);
172
173 testRatio2 = roundSigDig(bigger/i, 10);
174
175 if ( (testRatio == Math.floor(testRatio) ) && (testRatio2 == Math.floor(testRatio2) ) )
176
177 {
178
179 smaller = Math.round(smaller/i);
180
181 smaller = Math.round(bigger/i);
182
183 return(theResult *hcf(bigger, smaller) );
184
185 }
186
187 }
188
189 return(theResult);
190
191 }
192
193alert("error!");
194
195return(-1); // should never get here
196
197} // hcf
198
199
200
201
202
203function lcm(a,b) {
204
205// lowest common multiple
206
207var bigger = Math.abs(a);
208
209var smaller = Math.abs(b);
210
211var x = 0;
212
213if ( (a == 0) || (b == 0) ) return(1);
214
215if (smaller > bigger) {x = bigger; bigger = smaller; smaller = x}
216
217
218
219var testRatio = roundSigDig(bigger/smaller, 11)
220
221if (testRatio == Math.floor(testRatio) ) return (bigger)
222
223else
224
225 {
226
227 // look for a factor of the smaller, deplete it by that factor and multiply bigger by it
228
229 var found = false;
230
231 for (var i = 2; i <= smaller; i++)
232
233 {
234
235 if (i*i >= smaller) break;
236
237 testRatio = roundSigDig(smaller/i, 11);
238
239 if (testRatio == Math.floor(testRatio) )
240
241 {
242
243 smaller = testRatio;
244
245 bigger = bigger*i;
246
247 return( lcm(bigger, smaller) );
248
249 }
250
251 }
252
253 return(bigger*smaller);
254
255 }
256
257alert("error!");
258
259return(-1); // should never get here
260
261} // lcm
262
263
264
265// *** reducing a fraction ***
266
267function reduce(fraction){
268
269with (Math)
270
271 {
272
273 var HCF = hcf(fraction[1], fraction[2]);
274
275 fraction[1] = Math.round(fraction[1]/HCF);
276
277 fraction[2] = Math.round(fraction[2]/HCF);
278
279 } // with math
280
281return(fraction);
282
283} // reduce fraction
284
285
286
287
288
289function toFracArr(x, maxDenom, tol) {
290
291// identical to toFrac, except this returns an array [1] = numerator; [2] = denom
292
293// rather than a string
294
295// tolerance is the largest errror you will tolerate before resorting to
296
297// expressing the result as the input decimal in fraction form
298
299// suggest no less than 10^-10, since we round all to 15 decimal places.
300
301 var theFrac = new Array();
302
303 theFrac[1] = 0;
304
305 theFrac[2] = 0;
306
307 var p1 = 1;
308
309 var p2 = 0;
310
311 var q1 = 0;
312
313 var q2 = 1;
314
315 var u =0;
316
317 var t = 0;
318
319 var flag = true;
320
321 var negflag = false;
322
323 var a = 0;
324
325 var xIn = x; // variable for later
326
327
328
329 if (x >10000000000) return(theFrac);
330
331while (flag)
332
333 {
334
335 if (x<0) {x = -x; negflag = true; p1 = -p1}
336
337 var intPart = Math.floor(x);
338
339 var decimalPart = roundSigDig((x - intPart),15);
340
341
342
343 x = decimalPart;
344
345 a = intPart;
346
347
348
349 t = a*p1 + p2;
350
351 u = a*q1 + q2;
352
353 if ( (Math.abs(t) > 10000000000 ) || (u > maxDenom ) )
354
355 {
356
357 n = p1;
358
359 d = q1;
360
361 break;
362
363 }
364
365
366
367 p = t;
368
369 q = u;
370
371
372
373// cout << "cf coeff: " << a << endl; // for debugging
374
375// cout << p << "/" << q << endl; // for debugging
376
377
378
379 if ( x == 0 )
380
381 {
382
383 n = p;
384
385 d = q;
386
387 break;
388
389 }
390
391
392
393 p2 = p1;
394
395 p1 = p;
396
397 q2 = q1;
398
399 q1 = q;
400
401 x = 1/x;
402
403
404
405 } // while ( true );
406
407
408
409 theFrac[1] = n;
410
411 theFrac[2] = d;
412
413 return(theFrac);
414
415
416
417} // toFracArr
418
419
420
421function toFrac(x, maxDenom, tol) {
422
423// tolerance is the largest errror you will tolerate before resorting to
424
425// expressing the result as the input decimal in fraction form
426
427// suggest no less than 10^-10, since we round all to 15 decimal places.
428
429 var theFrac = new Array();
430
431 theFrac[1] = 0;
432
433 theFrac[2] = 0;
434
435 var p1 = 1;
436
437 var p2 = 0;
438
439 var q1 = 0;
440
441 var q2 = 1;
442
443 var u =0;
444
445 var t = 0;
446
447 var flag = true;
448
449 var negflag = false;
450
451 var a = 0;
452
453 var xIn = x; // variable for later
454
455
456
457 if (x >10000000000) return(theFrac);
458
459while (flag)
460
461 {
462
463 if (x<0) {x = -x; negflag = true; p1 = -p1}
464
465 var intPart = Math.floor(x);
466
467 var decimalPart = roundSigDig((x - intPart),15);
468
469
470
471 x = decimalPart;
472
473 a = intPart;
474
475
476
477 t = a*p1 + p2;
478
479 u = a*q1 + q2;
480
481 if ( (Math.abs(t) > 10000000000 ) || (u > maxDenom ) )
482
483 {
484
485 n = p1;
486
487 d = q1;
488
489 break;
490
491 }
492
493
494
495 p = t;
496
497 q = u;
498
499
500
501// cout << "cf coeff: " << a << endl; // for debugging
502
503// cout << p << "/" << q << endl; // for debugging
504
505
506
507 if ( x == 0 )
508
509 {
510
511 n = p;
512
513 d = q;
514
515 break;
516
517 }
518
519
520
521 p2 = p1;
522
523 p1 = p;
524
525 q2 = q1;
526
527 q1 = q;
528
529 x = 1/x;
530
531
532
533 } // while ( true );
534
535
536
537 theFrac[1] = n;
538
539 theFrac[2] = d;
540
541
542
543 if (theFrac[2] == 1) return (theFrac[1].toString());
544
545 else return (theFrac[1] + "/" + theFrac[2]);
546
547
548
549} // toFrac
550
551
552
553
554
555function lastChar(theString) {
556
557if (theString == "") return(theString);
558
559var len = theString.length;
560
561return theString.charAt(len-1);
562
563}
564
565
566
567function isCharHere (InString, RefString) {
568
569 if(InString.length!=1)
570
571 return (false);
572
573 if (RefString.indexOf (InString, 0)==-1)
574
575 return (false);
576
577 return (true);
578
579}
580
581
582
583function looksLikeANumber(theString) {
584
585// returns true if theString looks like it can be evaluated
586
587var result = true;
588
589var length = theString.length;
590
591if (length == 0) return (false);
592
593var x = ""
594
595var y = "1234567890-+*. /"
596
597var yLength = y.length;
598
599for (var i = 0; i <= length; i++)
600
601 {
602
603 x = theString.charAt(i);
604
605 result = false;
606
607 for (var j = 0; j <= yLength; j++)
608
609 {
610
611 if (x == y.charAt(j)) {result = true; break}
612
613 } // j
614
615 if (result == false) return(false);
616
617 } // i
618
619return(result);
620
621} // looks like a number
622
623
624
625function roundSix(theNumber) {
626
627var x = (Math.round(1000000*theNumber))/1000000;
628
629return(x);
630
631}
632
633
634
635function shiftRight(theNumber, k) {
636
637 if (k == 0) return (theNumber)
638
639 else
640
641 {
642
643 var k2 = 1;
644
645 var num = k;
646
647 if (num < 0) num = -num;
648
649 for (var i = 1; i <= num; i++)
650
651 {
652
653 k2 = k2*10
654
655 }
656
657 }
658
659 if (k>0)
660
661 {return(k2*theNumber)}
662
663 else
664
665 {return(theNumber/k2)}
666
667 }
668
669
670
671function roundSigDig(theNumber, numDigits) {
672
673 numDigits = numDigits -1 // too accurate as written
674
675 with (Math)
676
677 {
678
679 if (theNumber == 0) return(0);
680
681 else if(abs(theNumber) < 0.000000000001) return(0);
682
683// WARNING: ignores numbers less than 10^(-12)
684
685 else
686
687 {
688
689 var k = floor(log(abs(theNumber))/log(10))-numDigits
690
691 var k2 = shiftRight(round(shiftRight(abs(theNumber),-k)),k)
692
693 if (theNumber > 0) return(k2);
694
695 else return(-k2)
696
697 } // end else
698
699 }
700
701 }
702
703
704
705
706
707function looksLikeANumber(theString) {
708
709// returns true if theString looks like it can be evaluated
710
711var result = true;
712
713var length = theString.length;
714
715var x = ""
716
717var y = "1234567890-+^*./ "
718
719var yLength = y.length;
720
721for (var i = 0; i <= length; i++)
722
723 {
724
725 x = theString.charAt(i);
726
727 result = false;
728
729 for (var j = 0; j <= yLength; j++)
730
731 {
732
733 if (x == y.charAt(j)) {result = true; break}
734
735 } // j
736
737 if (result == false) return(false);
738
739 } // i
740
741return(result);
742
743} // looks like a number
744
745
746
747// ************ MAKE INTEGER
748
749// Makes a matrix integer by least common multiples of rows
750
751// returms a matrix of STRINGS if Strings = true else gives integers
752
753// input = a matrix of real floats
754
755// also records the row lcm of row i in outArray[i][0]
756
757
758
759function makeInteger(theMatrix, RowNum, ColNum,Strings) {
760
761var rowArray = new makeArray2(ColNum,2);
762
763var outArray = new makeArray2(RowNum,ColNum);
764
765for (var i = 1; i <= RowNum; i++)
766
767 {
768
769 // set up fraction row array
770
771 for (var j = 1; j <= ColNum; j++)
772
773 {
774
775 for (var k = 1; k <= 2; k++) rowArray[j][k] = toFracArr(theMatrix[i][j],maxDenom, tol)[k];
776
777 } // j
778
779
780
781 // get the lcm of all the row denominators
782
783 var rowLcm = 1;
784
785 for (j = 1; j <= ColNum; j++) rowLcm = lcm(rowLcm,rowArray[j][2]);
786
787 // now multiply the row by the lcm
788
789 var x = 0;
790
791 for (j = 1; j <= ColNum; j++)
792
793 {
794
795 x = rowLcm*rowArray[j][1]/rowArray[j][2];
796
797 if (!Strings) outArray[i][j] = Math.round(x);
798
799 else outArray[i][j] = Math.round(x).toString();
800
801 } // j
802
803 outArray[0][j] = rowLcm;
804
805 } // i
806
807return(outArray);
808
809
810
811} // makeInteger
812
813
814
815
816
817// *******************PIVOT **********************
818
819function pivot(InMatrix,rows,cols,theRow,theCol) {
820
821// alert("theRow = " + theRow + "theCol" + theCol);
822
823var thePivot = InMatrix[theRow][theCol];
824
825
826
827activeVars[theRow] = theCol; // reset the active variable
828starred[theRow] = 0; // unstar the row
829
830for (var i = 1; i <= cols; i++)
831
832 {
833
834 InMatrix[theRow][i] = InMatrix[theRow][i]/thePivot;
835
836 } // i
837
838
839
840// now pivot
841
842for (var i = 1; i <= rows; i++)
843
844 {
845
846 if ( (i != theRow) && (InMatrix[i][theCol] != 0) )
847
848 {
849
850 var factr = InMatrix[i][theCol];
851
852 for (var j = 1; j <= cols; j++)
853
854 {
855
856 InMatrix[i][j] = roundSigDig(InMatrix[i][j],maxSigDig+2) - roundSigDig(factr*InMatrix[theRow][j],maxSigDig+2); // Fix 01 avoiding subtractive error
857
858 } // j
859
860 }
861
862 } // i
863
864// now round all answers
865
866// for (var i = 1; i <= rows; i++)
867
868// {
869
870// for (var j = 1; j <= cols; j++)
871
872// {
873
874// InMatrix[i][j] = roundNine(InMatrix[i][j]);
875
876// } // j
877
878// } // i
879
880
881
882return(InMatrix);
883
884}
885
886// ***************** END PIVOT *********************
887
888
889
890// ****************SIMPLEX METHOD****************
891
892function simplexMethod(InMatrix, rows, cols) {
893
894var negIndicator = false;
895
896var testRatio = new Array();
897
898var theRow = 0; singular = false;
899
900document.theSpreadsheet.expr.value = "working..";
901
902
903
904// alert("HERE")
905
906// PHASE I
907
908while ( (phase1) && (TableauNumber <= maxSteps) )
909
910 {
911
912// big chunk of code removed here including an "if"
913
914 // first unstar all rows with zeros on the right-hand side
915 // by reversing the inequalities
916 // this is absolutely necessary in case of things like
917 // -x - y >= 0
918 // reversing the signs and removing the star
919 // will not effect the active variable name
920 // as it value is still zero in this case
921 // and its pivot will now be positive
922
923
924 var checkingForZeros = true;
925 var foundAZero = false;
926 while(checkingForZeros) {
927 checkingForZeros = false;
928 for (i = 1; i <= numRows-1; i++)
929 {
930 if (starred[i] == 1) break;
931 } // i
932 theRowx = i;
933 // check the first column to see if it has a zero on the
934 // right-hand side and is hence equivalent to <= constraint
935 // Fix 01 if it is really small make it zero first:
936 if (roundSigDig(InMatrix[theRowx][cols],maxSigDig)==0) InMatrix[theRowx][cols]=0;
937
938 if ((InMatrix[theRowx][cols] == 0)&&(starred[theRowx] == 1)){
939 checkingForZeros = true;
940 foundAZero = true;
941 for (var j = 1; j <= cols-1; j++) {
942 InMatrix[theRowx][j] *= -1;
943 } // j
944 starred[theRowx] = 0;
945 // add additional tableaus
946 TableauNumber +=1;
947 document.theSpreadsheet.expr.value += "..";
948 displayMatrix(1);
949 } // found a zero on the right-hand side
950 } // while checking for zeros
951
952 // at this point, check if there are any starred rows left
953 phase1 = false;
954 for (var i = 1; i <= numConstraints; i++) {
955 if (starred[i] == 1) {phase1 = true; break}
956 } // i
957
958 if (phase1) {
959 // there are starred rows left
960 // scan the first starred row starred row for the largest pos. element & pivot on that column
961
962 // this is actually step 2
963 if(!foundAZero) {
964 // find the largest positive entry in the first starred row
965 // and pivot
966
967 var rowmax = 0;
968
969 for (i = 1; i <= numRows-1; i++)
970
971 {
972
973 if (starred[i] == 1) break;
974
975 } // i
976
977 theRowx = i;
978
979 for (j = 1; j <= numCols-2; j++)
980
981 {
982
983 numx = roundSigDig(InMatrix[i][j],10);
984
985 if (numx > rowmax) {rowmax = numx; theColx = j;}
986
987 } // j
988
989 if (rowmax == 0) {singular = true; displayFinalStatus(); return(InMatrix)}
990
991
992 else
993
994 {
995
996 // get the lowest ratio and pivot on theRowx, theColx;
997
998 for (var i = 1; i <=rows-1; i++)
999
1000 {
1001
1002 testRatio[i] = -1;
1003
1004 if (roundSigDig(InMatrix[i][theColx],maxSigDig) >0) // dont want to pivot on a number too close to zero
1005
1006 {
1007 if (Math.abs(InMatrix[i][cols]) < epsilon) InMatrix[i][cols] = 0;
1008 // fixing numbers really close to zero
1009 testRatio[i] = InMatrix[i][cols]/ InMatrix[i][theColx];
1010
1011 }
1012
1013 } // i
1014
1015 var minRatio = 10000000000000;
1016
1017 theRow = 0; // this will have smallest ratio
1018
1019 for (var i = 1; i <=rows-1; i++)
1020
1021 {
1022
1023 if ((testRatio[i] >= 0) && (testRatio[i] < minRatio))
1024
1025 {
1026
1027 minRatio = testRatio[i];
1028
1029 theRow = i;
1030
1031 } // end if
1032
1033
1034
1035 else if ((testRatio[i] >= 0) && (testRatio[i] == minRatio))
1036
1037 {
1038
1039 if (starred[i] == 1) theRow = i;
1040
1041 // select starred ones in preference to others
1042 else if (Math.random()>.5) theRow = i;
1043 // random tie-breaking
1044 }
1045
1046 } // i
1047
1048 // escape clause follows
1049
1050 if (theRow == 0) {singular = true; displayFinalStatus(); return(InMatrix)}
1051
1052
1053
1054 InMatrix = pivot(InMatrix,rows,cols,theRow,theColx);
1055
1056 // end of this step
1057 } // if did not find a zero
1058
1059
1060 TableauNumber +=1;
1061
1062 document.theSpreadsheet.expr.value += "..";
1063
1064 displayMatrix(1);
1065
1066
1067
1068 }
1069
1070 } // end of phase 1 treatment
1071
1072
1073
1074
1075
1076// phase1 = false // TEMPORARY MEASURE TO PREVENT INF LOOPS
1077
1078 }
1079
1080
1081
1082
1083
1084// END OF PHASE I
1085
1086// NOW PHASE II
1087
1088// alert ("HERE AT PHASE 2")
1089
1090
1091
1092var testnum = 0;
1093
1094
1095
1096for (var i = 1; i <= cols-1; i++)
1097
1098 {
1099
1100 testnum = roundSigDig(InMatrix[rows][i],10)
1101
1102 if (testnum<0)
1103
1104 {
1105
1106 negIndicator = true;
1107
1108 }
1109
1110 } // i
1111
1112
1113
1114var theCol = 0;
1115
1116if (negIndicator)
1117
1118 {
1119
1120 // look for most negative of them;
1121
1122 var minval = 0;
1123
1124 for (i = 1; i <= cols-1; i++)
1125
1126 {
1127
1128 testnum = roundSigDig(InMatrix[rows][i],10);
1129
1130 if (testnum<minval)
1131
1132 {
1133
1134 minval = testnum;
1135
1136 theCol = i;
1137
1138 }
1139
1140 } // i
1141
1142//alert(theCol)
1143
1144 }
1145
1146
1147
1148while ( (negIndicator) && (TableauNumber <= maxSteps) ) // phase 2
1149
1150 {
1151
1152 for (var i = 1; i <=rows-1; i++)
1153
1154 {
1155
1156 testRatio[i] = -1;
1157
1158 if (roundSigDig(InMatrix[i][theCol],maxSigDig) >0) // dont want to pivot on a number too close to zero
1159
1160 {
1161 if (Math.abs(InMatrix[i][cols]) < epsilon) InMatrix[i][cols] = 0;
1162 // fixing numbers really close to zero
1163 testRatio[i] = InMatrix[i][cols]/ InMatrix[i][theCol];
1164// alert(testRatio[i]);
1165
1166 }
1167
1168 } // i
1169
1170 var minRatio = 10000000000000;
1171
1172 theRow = 0; // this will have smallest ratio
1173
1174 for (var i = 1; i <=rows-1; i++)
1175
1176 {
1177
1178
1179
1180 if ((testRatio[i] >= 0) && (testRatio[i] < minRatio))
1181
1182 {
1183
1184 minRatio = testRatio[i];
1185
1186 theRow = i;
1187
1188 }
1189
1190 else if ((testRatio[i] >= 0) && (testRatio[i] == minRatio))
1191
1192 {
1193
1194 if (Math.random()>.5) theRow = i;
1195 // random tie-breaking
1196 }
1197
1198
1199
1200
1201 } // i
1202
1203
1204
1205 // escape clause:
1206
1207 if (theRow == 0) {singular = true; displayFinalStatus(); return(InMatrix)}
1208
1209
1210
1211 InMatrix = pivot(InMatrix,rows,cols,theRow,theCol);
1212
1213 // end of this step
1214
1215
1216
1217 TableauNumber +=1;
1218
1219 document.theSpreadsheet.expr.value += "..";
1220
1221 displayMatrix(1);
1222
1223
1224
1225
1226
1227 negIndicator = false;
1228
1229 for (var i = 1; i <= cols-1; i++)
1230
1231 {
1232
1233 if (roundSigDig(InMatrix[rows][i], 10) <0)
1234
1235 {
1236
1237 // theCol = i;
1238
1239 negIndicator = true;
1240
1241//alert("Column = "+i+" Value = " + InMatrix[rows][i]);
1242
1243 }
1244
1245 } // i
1246
1247
1248
1249// ERROR CORRECTION BELOW:
1250
1251 if (negIndicator) // need to select the most negative EVERY time
1252
1253 {
1254
1255 // look for most negative of them;
1256
1257 var minval = 0;
1258
1259 for (i = 1; i <= cols-1; i++)
1260
1261 {
1262
1263 testnum = roundSigDig(InMatrix[rows][i],10);
1264
1265 if (testnum<minval)
1266
1267 {
1268
1269 minval = testnum;
1270
1271 theCol = i;
1272
1273 }
1274
1275 } // i
1276
1277 } // end if negIndicator is still true
1278//alert(theCol)
1279 } // while negIndicator
1280
1281
1282
1283
1284
1285displayFinalStatus();
1286
1287return(InMatrix);
1288
1289} // simplexMethod
1290
1291// ********************** END OF SIMPLEX METHOD
1292
1293
1294
1295function checkString(InString,subString,backtrack)
1296
1297// check for subString
1298
1299// if backtrack = false, returns -1 if not found, and left-most location in string if found
1300
1301// if backtrack = true, returns -1 if not found, and right-most location in string if found
1302
1303// note that location is to the left of the substring in both cases
1304
1305{
1306
1307var found = -1;
1308
1309var theString = InString;
1310
1311var Length = theString.length;
1312
1313var symbLength = subString.length;
1314
1315for (var i = Length- symbLength; i >-1; i--)
1316
1317 {
1318
1319 TempChar=theString.substring (i, i+ symbLength);
1320
1321 if (TempChar == subString)
1322
1323 {
1324
1325 found = i;
1326
1327 if (backtrack) i = -1
1328
1329 }
1330
1331 } // i
1332
1333return(found);
1334
1335} // check
1336
1337
1338
1339
1340
1341// alert("HERE")
1342
1343function parser (InString, Sep) {
1344
1345// ************************
1346
1347// returns an array 0th entry = number n of blocks (-1) if the character Sep does not occur
1348
1349// subsequent n entries are the blocks themselves
1350
1351// Here are the blocks
1352
1353// ***block 1 *** SEP *** block 2 *** SEP *** block 3 ***
1354
1355// (one more block than number of occurrences of SEP)
1356
1357// ************************
1358
1359 var NumSeps=0; var Count = 0;
1360
1361 var location = new Array;
1362
1363 location[0] = -1;
1364
1365 var len = InString.length;
1366
1367 for (Count=0; Count < len; Count++) {
1368
1369 if (InString.charAt(Count)==Sep)
1370
1371 {
1372
1373 NumSeps++;
1374
1375 location[NumSeps] = Count;
1376
1377 }
1378
1379 }
1380
1381
1382
1383 var parse = new makeArray (NumSeps+2);
1384
1385 if (NumSeps == 0) {parse[0] = 1; parse[1] = InString; return(parse);}
1386
1387 parse[0] = NumSeps + 1;
1388
1389
1390
1391 for (var i = 1; i <=NumSeps; i++)
1392
1393 {
1394
1395 parse[i] = InString.substring(location[i-1]+1, location[i]);
1396
1397// alert("i = " + i + " " + parse[i]);
1398
1399 }
1400
1401 parse[NumSeps+1] = InString.substring(location[NumSeps]+1, len);
1402
1403// alert("i = " + i + " " + parse[i]);
1404
1405
1406
1407
1408
1409 return (parse);
1410
1411}
1412
1413
1414
1415function parseLinearExpr(InString) {
1416
1417// **********
1418
1419// Returns an array: with 0th entry = an array of variable names
1420
1421// (eg. ["x", "x'", "y", "z"])
1422
1423// and subsequent entries the coefficients.
1424
1425// to get the number of coefficients, just take the length of the array in position 0.
1426
1427// first remove a leading cr if there
1428
1429
1430InString = stripChar(InString,"("); // get rid of parens (not needed once x is gone...
1431
1432InString = stripChar(InString,")");
1433
1434var stringlen = InString.length
1435
1436// alert(escape(InString.charAt(0)));
1437
1438// if (InString.charAt(0) == unescape( "%0A" )) InString = InString.substring(1, stringlen);
1439
1440// THE ABOVE LINE REMOVES A STRANGE BUG IN NETSCAPE, WHICH SEEMS
1441
1442// TO INSERT A SPURIOUS LINE BREAK (0A) THERE RATHER THAN A CR (0D)
1443
1444
1445
1446// first insert a leading 1
1447
1448// ***HERE THE FOLLOWING LINE WAS ADDED AS A FIX FOR WINTEL
1449
1450if (!looksLikeANumber(InString.charAt(0))) InString = "1" + InString;
1451
1452
1453
1454// first insert a leading + if necessary
1455
1456if (InString.charAt(0) != "-") InString = "+"+ InString;
1457
1458// alert(InString);
1459
1460 var variableList = "";
1461
1462 InString = replaceSubstring (InString,"+","_+");
1463
1464 InString = replaceSubstring (InString,"-","_-");
1465
1466
1467
1468 var ch = "_";
1469
1470 var Ar = parser(InString, ch);
1471
1472 var parsd = new makeArray (Ar[0]+1, "");
1473
1474// alert(Ar[0] + "***" + Ar[1] + "***" + Ar[2] + "***" + Ar[3] + "***" + Ar[4] );
1475
1476
1477
1478 for (var i = 1; i < Ar[0]; i++)
1479
1480 {
1481
1482 parsd[i] = stripChar(Ar[i+1],"_");
1483
1484 // parser gives 1st entry as what is before first sign -- ignore it
1485
1486 }
1487
1488
1489
1490// now for the variable names
1491
1492var vars = [];
1493
1494for (var i = 1; i < Ar[0]; i++)
1495
1496 {
1497
1498 vars[i-1] = /([a-zA-Z].*)/.exec(parsd[i])[1];
1499
1500 parsd[i] = parsd[i].replace(/[a-zA-Z].*/, '');
1501
1502 if (parsd[i] == "+") parsd[i] = "1"; // fix up the coefficients
1503
1504 else if (parsd[i] == "-") parsd[i] = "-1";
1505
1506 parsd[i] = stripChar(parsd[i],"+");
1507
1508 }
1509
1510parsd[0] = vars;
1511
1512
1513
1514// alert(parsd[0] + "," + parsd[1] + "," + parsd[2] + "," + parsd[3])
1515
1516 return (parsd);
1517
1518
1519
1520} // parser
1521
1522
1523
1524
1525
1526
1527
1528function rightString (InString, num) {
1529
1530 OutString=InString.substring (InString.length-num, InString.length);
1531
1532 return (OutString);
1533
1534}
1535
1536
1537
1538function rightTrim (InString) {
1539
1540 var length = InString.length;
1541
1542 OutString=InString.substring(0,length-1);
1543
1544 return (OutString);
1545
1546}
1547
1548
1549
1550function replaceChar (InString,oldSymbol,newSymbol) {
1551
1552 var OutString="";
1553
1554 var TempChar = "";
1555
1556 for (Count=0; Count < InString.length; Count++) {
1557
1558 TempChar=InString.substring (Count, Count+1);
1559
1560 if (TempChar!=oldSymbol)
1561
1562 OutString=OutString+TempChar
1563
1564 else OutString=OutString+newSymbol;
1565
1566 }
1567
1568 return (OutString);
1569
1570}
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580function replaceSubstring (InString,oldSubstring,newSubstring) {
1581
1582 OutString="";
1583
1584 var sublength = oldSubstring.length;
1585
1586 for (Count=0; Count < InString.length; Count++) {
1587
1588 TempStr=InString.substring (Count, Count+sublength);
1589
1590 TempChar=InString.substring (Count, Count+1);
1591
1592 if (TempStr!= oldSubstring)
1593
1594 OutString=OutString+TempChar
1595
1596 else
1597
1598 {
1599
1600 OutString=OutString+ newSubstring;
1601
1602 Count +=sublength-1
1603
1604 }
1605
1606
1607
1608 }
1609
1610 return (OutString);
1611
1612}
1613
1614
1615
1616
1617
1618// ******************** FORM UTILITIES ******************
1619
1620
1621
1622function sesame(url,hsize,vsize){
1623
1624// Default size is 550 x 400
1625
1626 var tb="toolbar=0,directories=0,status=0,menubar=0"
1627
1628 tb+=",scrollbars=1,resizable=1,"
1629
1630 var tbend="width="+hsize+",height="+vsize;
1631
1632 if(tbend.indexOf("<undefined>")!=-1){tbend="width=550,height=400"}
1633
1634 tb+=tbend
1635
1636 Win_1 = window.open("","win1",tb);
1637
1638 Win_1 = window.open(url,"win1",tb);
1639
1640 }
1641
1642
1643
1644
1645
1646// *** testing *******
1647
1648// document.theSpreadsheet.output.value = theSampleMatrixString;
1649
1650// document.theSpreadsheet.output.value += "\r" + checkString(theString, cr+cr,false);
1651
1652// var matrixName = "A";
1653
1654// *** testing *******
1655
1656// alert ("here");
1657
1658
1659
1660
1661
1662
1663
1664// ******************* LP PARSER FOLLOWS **************************
1665
1666
1667
1668function SetupTableau() {
1669
1670// reads problem and sets up the first tableau
1671
1672
1673
1674// get out of here if not ok
1675
1676if (!okToRoll) return (666);
1677
1678
1679
1680// first, adjust some globals...
1681
1682maximization = true;
1683
1684singular = false; // start with a clean slate
1685
1686
1687
1688var theString = document.theSpreadsheet.input.value;
1689
1690theString += cr; // want an extra cr at the end
1691
1692theString = stripSpaces(theString);
1693
1694theString = stripChar(theString,tab); // get rid of tabs
1695
1696theString = stripChar(theString,":"); // get rid of colons
1697
1698theString = replaceSubstring(theString,lf, cr); // replace line feeds by carriage returns
1699
1700 // some browsers add these to cr
1701
1702
1703
1704// convert everything to lower case
1705
1706theString = theString.toLowerCase();
1707
1708// now parse commas into line breaks and introduce a line break after "subject to"
1709
1710theString = replaceSubstring(theString, "to", "to"+cr);
1711
1712theString = replaceSubstring(theString, ",", cr);
1713
1714theString = replaceSubstring(theString, cr+"subject", "subject"); // in case they have introduced a line break or comma before 'subject to'
1715
1716
1717
1718// now get rid of double carriage returns
1719
1720
1721
1722var doublecr = true;
1723
1724while (doublecr)
1725
1726 {
1727
1728 if (checkString(theString,cr+cr,false) == -1) doublecr = false;
1729
1730 else theString = replaceSubstring(theString,cr+cr,cr);
1731
1732 }
1733
1734// get rid of terminating cr
1735
1736if (lastChar(theString) == cr) theString = rightTrim(theString,1);
1737
1738
1739
1740// else alert("*"+lastChar(theString)+"*");
1741
1742
1743
1744theString = replaceSubstring(theString, "<=", lteSymbol);
1745
1746theString = replaceSubstring(theString, ">=", gteSymbol);
1747
1748theString = replaceSubstring(theString, lte, lteSymbol);
1749
1750theString = replaceSubstring(theString, gte, gteSymbol);
1751
1752
1753
1754
1755
1756
1757
1758// look for "maximize" and chop the string there
1759
1760var check = checkString(theString,"maxi",false)
1761
1762if (check == -1)
1763
1764 {check = checkString(theString,"mini",false); maximization = false; phase1 = true}
1765
1766if (check == -1) { document.theSpreadsheet.expr.value = "Huh?"; document.theSpreadsheet.output.value = "That does not look like a linear programming problem to me!" + cr + cr + "Press Example to see how to type one in." ; okToRoll = false; return(666);}
1767
1768len = theString.length;
1769
1770theString = theString.substring(check,len);
1771
1772// now the string starts with "max or "min"
1773
1774
1775
1776
1777
1778// now extract the objective and constraints
1779
1780
1781
1782var tempAr = parser(theString,cr);
1783
1784var numConstTemp = tempAr[0]-1;
1785//alert(numConstTemp);
1786for (var i = 2; i <= numConstTemp+1; i++) {
1787
1788 if (tempAr[i] && tempAr[i].match(/=/)) {
1789
1790 tempAr[i] = tempAr[i].replace(/=/, lteSymbol);
1791
1792 tempAr[numConstTemp+2] = tempAr[i].replace(lteSymbol, gteSymbol);
1793 numConstTemp += 1;
1794 tempAr[0] += 1;
1795
1796 }
1797
1798}
1799
1800// alert("HERElines of the problem are: "+tempAr[0] + " blocks " + tempAr[1] + " \n" + tempAr[2] + "\n" + tempAr[3] + "\n" + tempAr[4] + " \n " + tempAr[5] + "***")
1801
1802// the first line should contain the objective
1803
1804
1805
1806var line1 = tempAr[1];
1807
1808
1809
1810// get rid of "subject to, if there"
1811
1812check = checkString(line1,"subj",true);
1813
1814if (check > 0) line1 = line1.substring(0,check);
1815
1816
1817
1818// now look for objective
1819
1820check = checkString(line1,"=",false);
1821
1822if (check <=0) return(666);
1823
1824objectiveName = line1.charAt(check-1);
1825
1826len = line1.length;
1827
1828var expression = line1.substring(check+1,len);
1829
1830// alert(expression);
1831
1832var OBJ = parseLinearExpr(expression);
1833
1834variables = OBJ[0];
1835
1836// alert (variables);
1837
1838
1839
1840numConstraints = tempAr[0]-1;
1841
1842// alert(numConstraints+1);
1843
1844
1845
1846
1847
1848// make the tableau .. note that all the variables are assumed to appear in the objective!!!
1849
1850numVariables = variables.length;
1851
1852
1853// alert("number of variables =" + numVariables)
1854
1855
1856
1857numRows = numConstraints+1;
1858
1859numCols = numRows + numVariables + 1;
1860
1861
1862
1863
1864
1865// create the tableau
1866
1867
1868
1869theTableau = new makeArray2 (numRows,numCols);
1870
1871theStringTableau = new makeArray2 (numRows,numCols); // for display purposes
1872
1873if (phase1) starred = new makeArray(numRows); // for starred rows
1874
1875
1876
1877// do the last row
1878
1879for (var j = 1; j <= numCols; j++) theTableau[numRows][j] = 0; // init
1880
1881for (var i = 1; i <= numVariables; i++)
1882
1883 {
1884
1885 if (maximization) theTableau[numRows][i] = -eval(OBJ[i]);
1886
1887 else theTableau[numRows][i] = eval(OBJ[i]);
1888
1889 }
1890
1891theTableau[numRows][numCols-1] = 1;
1892
1893theTableau[numRows][numCols] = 0;
1894
1895
1896// now extract the constraints
1897
1898
1899
1900// first remove the "subject to..."
1901
1902theString = tempAr[2];
1903
1904var x = checkString(theString,"to",false);
1905
1906len = theString.length;
1907
1908if (x != -1) theString = theString.substring(x+2,len);
1909
1910// alert(theString);
1911
1912
1913
1914tempAr[2] = theString;
1915
1916var GTE = false; // greater-than-eq flag
1917
1918// alert("num constraints is " + numConstraints )
1919
1920for (var i = 1; i <= numConstraints; i++)
1921
1922 {
1923
1924 activeVars[i] = i + numVariables;
1925 starred[i] = 0;
1926
1927 GTE = false; // clean slate
1928// alert(tempAr[1+i]);
1929 // first get the inequalities out of the way
1930 twoPart = parser(tempAr[1+i], lteSymbol);
1931
1932 if (twoPart[0] < 2) {
1933// alert(tempAr[1+i]);
1934 twoPart = parser(tempAr[1+i], gteSymbol); phase1 = true; GTE = true;
1935
1936 }
1937
1938
1939
1940 if (twoPart[0] <2)
1941
1942 {
1943// alert(tempAr[1+i]);
1944 i += 1;
1945
1946 okToRoll = false;
1947
1948 document.theSpreadsheet.expr.value = "Huh? The expression in line " + i + " does not look like an inequality to me!";
1949
1950
1951
1952// alert("left-side of inequality = " + twoPart[1]);
1953
1954
1955
1956 return (666)
1957
1958 }
1959
1960
1961
1962// alert("left-side of ineuqulaity = " + twoPart[1]);
1963
1964 // alert(twoPart[2]);
1965
1966
1967
1968
1969
1970 var leftHandSide = parseLinearExpr(twoPart[1]);
1971
1972
1973
1974 for (var j = 1; j <= numCols; j++) theTableau[i][j] = 0; // init
1975
1976 theTableau[i][numCols] = eval(twoPart [2]); // the right-hand side
1977
1978 if (GTE) {
1979 theTableau[i][numVariables + i] = -1;
1980 starred[i] = 1;
1981 phase1 = true;
1982 }
1983
1984 else theTableau[i][numVariables + i] = 1;
1985
1986
1987
1988 var theIndex = 0;
1989
1990 for (var j = 1; j <= numVariables; j++)
1991
1992 {
1993
1994 theVar = variables[j-1];
1995
1996
1997
1998 theIndex = -1;
1999
2000 for (var k = 0; k < leftHandSide[0].length; k++) {
2001
2002 if (leftHandSide[0][k] == theVar) {
2003
2004 theIndex = k;
2005
2006 break;
2007
2008 }
2009
2010 }
2011
2012// if (i == 3) alert(theIndex);
2013
2014
2015
2016 if (theIndex == -1) theTableau[i][j] = 0;
2017
2018 else theTableau[i][j] = eval(leftHandSide[theIndex+1]);
2019
2020// alert("HERE");
2021
2022
2023
2024
2025
2026 }
2027
2028
2029
2030
2031
2032 } // enf of the loop i
2033
2034// alert("HERE")
2035
2036
2037
2038// *** testing starts *******HERE
2039
2040// var display = "\r";
2041
2042// for (var i = 1; i <= numRows; i++)
2043
2044// {
2045
2046// for (j = 1; j <= numCols; j++)
2047
2048// {
2049
2050// display += theTableau[i][j] + tab;
2051
2052// } // j
2053
2054// display += cr;
2055
2056// } // i
2057
2058// document.theSpreadsheet.output.value += display;
2059
2060// alert("Pausing");
2061
2062// *** testing *******
2063
2064
2065
2066displayMatrix(1);
2067
2068
2069
2070// *** testing *******
2071
2072// document.theSpreadsheet.output.value = "objective name = " + objectiveName + cr + "the expression = " + expression;
2073
2074// alert ("pausing");
2075
2076// *** testing *******
2077
2078
2079
2080return(1);
2081
2082} // SetupTableau
2083
2084
2085
2086
2087
2088function displayFinalStatus() {
2089
2090// gives the solution or error messages
2091
2092
2093
2094if (TableauNumber > maxSteps) document.theSpreadsheet.expr.value = "No optimal solution found after 50 steps. Aborted."
2095
2096else if (singular) document.theSpreadsheet.expr.value = "No optimal solution exists for this problem."
2097
2098else
2099
2100 {
2101
2102 document.theSpreadsheet.expr.value = "Optimal Solution: " + objectiveName + " = ";
2103
2104 var numx = 0; var theRowx = 0; var theColx = 0; var count = 0; var theChar = "";
2105
2106 var theStr = "";
2107
2108 var objectiveVal = theTableau[numRows][numCols];
2109
2110
2111
2112 if (!maximization) objectiveVal = - objectiveVal;
2113
2114 if ((fractionMode) || (integerMode)) document.theSpreadsheet.expr.value += toFrac (roundSigDig(objectiveVal,15), maxDenom, tol) + "; "; else
2115
2116 document.theSpreadsheet.expr.value += roundSigDig(objectiveVal, numSigDigs).toString() + "; ";
2117
2118
2119 var thePivotPosn = new Array();
2120 var useThis = true;
2121 for (var j = 1; j <= numVariables; j++)
2122 {
2123 useThis = true;
2124 count = 0;
2125
2126 theRowx = 0;
2127
2128 theChar = variables[j-1]; // name of this variable
2129 thePivotPosn[j] = 0;
2130 useThis = true;
2131 document.theSpreadsheet.expr.value += theChar + " = ";
2132
2133 for (var i = 1; i <= numRows; i++)
2134 {
2135 numx = roundSigDig(theTableau[i][j],10);
2136 if (numx != 0)
2137 {
2138 count++;
2139 if (numx != 0) theRowx = i
2140 }
2141 } // i
2142// alert(count)
2143 if ((count == 1) && (roundSigDig(theTableau[theRowx][j],10)> 0))
2144 {
2145// correction May 20 2010 he second condition above did not check that the pivot was positive!!!
2146 thePivotPosn[j] = theRowx; // row of that pivot
2147 // check if we have not already used a pivot in that row
2148 // in the case of more than one pivot per row
2149 for (var u = 1; u <= j-1; u++) if (thePivotPosn[j] == thePivotPosn[u]) useThis = false;
2150
2151 // present solution
2152// alert(useThis)
2153 if (useThis) {
2154// Bug fix April 3 2009
2155// if there were more than two pivots ion a row
2156// and it used an earlier one for which the pivot was not 1
2157// then it reported the incorrect solution
2158// fix: divide by the pivot in all cases just in case...
2159// implemented in the next two lines
2160 if ((fractionMode) || (integerMode)) theStr = toFrac (roundSigDig((theTableau[theRowx][numCols]/theTableau[theRowx][j]),15), maxDenom, tol);
2161 else theStr = roundSigDig((theTableau[theRowx][numCols]/theTableau[theRowx][j]),numSigDigs).toString();
2162 }
2163 else theStr = "0";
2164
2165 if (j < numVariables) theStr += ", ";
2166
2167 document.theSpreadsheet.expr.value += theStr;
2168
2169// alert("starred row is row #" + theRowx + "column is "+j)
2170
2171
2172 } // if a pivot there
2173
2174
2175 else
2176
2177 {
2178
2179 theStr = "0";
2180
2181 if (j < numVariables) theStr += ", "; document.theSpreadsheet.expr.value += theStr;
2182
2183 }
2184
2185
2186
2187
2188
2189 } // j
2190
2191
2192
2193
2194
2195 } // end of presentation
2196
2197
2198
2199}
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209function displayMatrix(number) {
2210
2211var theString = "Tableau #" + TableauNumber + cr;
2212
2213
2214
2215if (singular) theString += "undefined";
2216
2217
2218
2219else
2220
2221{
2222
2223var RowNum = numRows;
2224
2225
2226
2227var ColNum = numCols;
2228
2229// alert("about to display a "+ RowNum+ " x " + ColNum + "matrix");
2230
2231
2232
2233// first round all the results and get the longest resulting string
2234
2235var maxLength = 1;
2236
2237var x = "", i=0, j=0, k=0;
2238
2239var xLen = 0;
2240
2241// ok to here
2242
2243// prepare the stringmatrix if integer mode:
2244
2245
2246
2247if (integerMode) theStringTableau = makeInteger(theTableau, RowNum, ColNum,true);
2248
2249
2250
2251 // else, handle fractions & decimals
2252
2253else {
2254
2255 for (i = 1; i <= RowNum; i++)
2256
2257 {
2258
2259 for (j = 1; j <= ColNum; j++)
2260
2261 {
2262
2263// alert("i = "+i + " j = " + j + "table entry = " + theTableau[i][j]);
2264
2265if (fractionMode) x = toFrac (roundSigDig(theTableau[i][j],15) , maxDenom, tol);
2266
2267 else x = roundSigDig(theTableau[i][j], numSigDigs).toString();
2268
2269// alert("x = "+x);
2270
2271 xLen = x.length;
2272
2273// alert("xLen =" + xLen);
2274
2275 if (xLen > maxLength) maxLength = xLen;
2276
2277 theStringTableau[i][j] = x;
2278
2279
2280
2281 } // j
2282
2283 } // i
2284
2285 } // end else (if not integer mode)
2286
2287
2288
2289if (maxLength < 6) maxLength = 6; // more space
2290
2291
2292
2293 var spaceString = "";
2294
2295 for (i = 0; i <= RowNum; i++) // was 1
2296
2297 {
2298
2299
2300
2301 for (j = 1; j <= ColNum; j++)
2302
2303 {
2304
2305 if (i == 0)
2306
2307 {
2308
2309 if (j <= numVariables) x = variables[j-1];
2310
2311 else if (j == numVariables + numConstraints + 1) {x = objectiveName; if (!maximization) x = "-"+x;}
2312
2313 else if (j < ColNum) { var mmm = j - numVariables ; x = "s" + mmm.toString();}
2314
2315 else if (j == ColNum) x = " ";
2316
2317 } // end if
2318
2319
2320
2321 else x = theStringTableau[i][j];
2322
2323
2324
2325
2326
2327 sp = maxLength - x.length
2328
2329 spaceString = "";
2330
2331 for (k = 0; k <= sp; k++) spaceString += " ";
2332
2333 theString += x + spaceString;
2334
2335
2336
2337 } // j
2338
2339 theString += cr;
2340
2341 } // i
2342
2343} // if not singular
2344
2345document.theSpreadsheet.output.value += theString + cr;
2346
2347// now convert the strings back to numbers
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363return(0);
2364
2365}
2366
2367
2368
2369// ******** END OF DISPLAY ROUTINE ***************
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381function makeArray3 (X,Y,Z)
2382
2383 {
2384
2385 var count;
2386
2387 this.length = X+1;
2388
2389 for (var count = 1; count <= X+1; count++)
2390
2391 // to allow starting at 1
2392
2393 this[count] = new makeArray2(Y,Z);
2394
2395 } // makeArray3
2396
2397
2398
2399
2400
2401function makeArray2 (X,Y)
2402
2403 {
2404
2405 var count;
2406
2407 this.length = X+1;
2408
2409 for (var count = 0; count <= X+1; count++)
2410
2411 // to allow starting at 1
2412
2413 this[count] = new makeArray(Y);
2414
2415 } // makeArray2
2416
2417
2418
2419function makeArray (Y)
2420
2421 {
2422
2423 var count;
2424
2425 this.length = Y+1;
2426
2427 for (var count = 1; count <= Y+1; count++)
2428
2429 this[count] = 0;
2430
2431 } // makeArray
2432
2433
2434
2435
2436
2437function stripSpaces (InString) {
2438
2439 OutString="";
2440
2441 for (Count=0; Count < InString.length; Count++) {
2442
2443 TempChar=InString.substring (Count, Count+1);
2444
2445 if (TempChar!=" ")
2446
2447 OutString=OutString+TempChar;
2448
2449 }
2450
2451 return (OutString);
2452
2453 }
2454
2455
2456
2457function stripChar (InString,symbol) {
2458
2459 OutString="";
2460
2461 for (Count=0; Count < InString.length; Count++) {
2462
2463 TempChar=InString.substring (Count, Count+1);
2464
2465 if (TempChar!=symbol)
2466
2467 OutString=OutString+TempChar;
2468
2469 }
2470
2471 return (OutString);
2472
2473}
2474
2475
2476
2477
2478
2479function doIt(){
2480
2481 fractionMode = false;
2482
2483 integerMode = false;
2484
2485 var theMode = document.theSpreadsheet.Mode.selectedIndex;
2486
2487 if (document.theSpreadsheet.Mode.options[theMode].text == "Fraction") fractionMode = true;
2488
2489 else if (document.theSpreadsheet.Mode.options[theMode].text == "Integer") integerMode = true;
2490
2491
2492
2493 var num = doIt.arguments[0];
2494
2495
2496
2497 //**********
2498
2499
2500
2501 // Option 1
2502
2503 if (num == 1)
2504
2505 {
2506
2507 if (okToRoll)
2508
2509 {
2510
2511 TableauNumber = 1;
2512
2513 document.theSpreadsheet.output.value = ""; // clear answer space
2514
2515 SetupTableau();
2516
2517// alert("tableau is set up");
2518
2519 } // of okToRoll
2520
2521
2522
2523 if (okToRoll)
2524
2525 {
2526
2527 theTableau = simplexMethod(theTableau,numRows,numCols);
2528
2529 }
2530
2531 } // end of this option
2532
2533
2534
2535 // Option 2 // preliminary checks
2536
2537 else if (num == 2)
2538
2539 {
2540
2541 okToRoll = true;
2542
2543 stepName = "Rounding information"
2544
2545 var accuracydig = document.theSpreadsheet.acc.value;
2546
2547
2548
2549 if ( (accuracydig == "") || (!looksLikeANumber(accuracydig)) ) { document.theSpreadsheet.expr.value = "Enter a value for the accuracy (Rounding) in the range 1-13."; okToRoll = false}
2550
2551
2552
2553 if (okToRoll)
2554
2555 {
2556
2557 var thenum = eval(accuracydig);
2558
2559 if ((thenum < 1) || (thenum > 14)) {document.theSpreadsheet.expr.value = "Accuracy (Rounding) must be in the range 1-13."; okToRoll = false}
2560
2561
2562
2563 else numSigDigs =thenum;
2564
2565
2566
2567 if (document.theSpreadsheet.input.value == "") {document.theSpreadsheet.expr.value = "Enter a linear programming problem above (or press Example)."; okToRoll = false; }
2568
2569
2570
2571 } // if okToRoll
2572
2573 } // end of this option
2574
2575
2576
2577 // Option 3 (Erase)
2578
2579 else if (num == 3)
2580
2581 {
2582
2583 document.theSpreadsheet.input.value = "";
2584
2585 document.theSpreadsheet.output.value = "";
2586
2587 document.theSpreadsheet.expr.value = "";
2588
2589 }
2590
2591
2592
2593 // compute the expression
2594
2595 else if (num == 4)
2596
2597 {
2598
2599
2600
2601 } // of this option
2602
2603
2604
2605 //
2606
2607 else if (num == 5)
2608
2609 {
2610
2611 document.theSpreadsheet.input.value = theSampleLPString;
2612
2613 if (document.theSpreadsheet.acc.value == "") document.theSpreadsheet.acc.value = numSigDigs;
2614
2615 document.theSpreadsheet.expr.value = "Press 'Solve' to solve the given problem.";
2616
2617 document.theSpreadsheet.output.value = theInstructionString;
2618
2619 }
2620
2621 // Option 6
2622
2623 else if (num == 6)
2624
2625 {
2626
2627
2628
2629 } // of this option
2630
2631
2632
2633}