Simplex Algorithm

11112023, 11:21 PM
(This post was last modified: 01072024 02:41 AM by ftneek.)
Post: #1




Simplex Algorithm
Attached is an implementation of the simplex algorithm as developed in
An Introduction to Linear Programming and Game Theory 3rd Edition, Paul R. Thie and Gerard E. Keough (ISBN: 9780470232866). Thanks to Albert Chan for his contributions. I have also included their simplex_le() method. I originally posted it in this thread, but there's been a few changes since then. 2023/11/11: initial upload 2023/11/17: some speed improvements. simplex2 was renamed to simplex_core(), shown examples will still work if you just change the name. 2023/11/18: added simplex_int() for integer programming problems. simplex_core() returns a list of basic variables as 4th item when there is a solution. Updated and added some integer tests. See post 26 for an example. 2023/11/19: speed improvements, new simplex() wrapper. See post 27 for example inputs. 2023/11/19: fixed exact> floating point bug for integer problems, more tests 2023/11/21: fixed bug in infinite solution code, new test 2023/12/21:
2023/12/23:
 neek 

11112023, 11:51 PM
(This post was last modified: 11122023 11:17 PM by Albert Chan.)
Post: #2




RE: Simplex Algorithm
Hi, ftneek
simplex_le code had a bug, using some HOME functions instead of CAS. This sometimes turn exact numbers to approximate. Also, I added "=" constraints, by adding "≥" on top of "≤" constraints. (≤ constraint) + (≥ constraint) = (= constraint) It is not efficient, but it is a 1 liner, worthy of adding in, for convenience. Here is my latest update Code: simplex_le(a, dir) := Example: minimize Z = 2x  3y  4z Constraints (3x+2y+z=10) AND (2x+5y+3z=15), x,y,z ≥ 0, > m := [[3,2,1,10], [2,5,3,15], [2,3,4,0]] > simplex_le(m, 2) /* '' = minimize cost, top 2 are "=" constraints */ [130/7, [15/7, 0, 25/7, 0, 0, 0, 0], ...] We can't do ±0. For "≤" constraints only, dir = ±∞, (+ = max,  = min) HP Prime Help screen simplex_reduce example. (it always maximize) > simplex_reduce([[3,2,2],[1,1,1]], [3,4], [1,2,3]) [9/2, [0,0,3/2,0,5/2], [[3/2,1,1,1/2,0,3/2],[1/2,0,0,1/2,1,5/2],[7/2,1,0,3/2,0,9/2]]] > simplex_le([[3,2,2,3], [1,1,1,4], [1,2,3,0]], +∞) [9/2, [0,0,3/2,0,5/2], [[3/2,1,1,1/2,0,3/2],[1/2,0,0,1/2,1,5/2],[7/2,1,0,3/2,0,9/2]]] 

11122023, 12:00 AM
Post: #3




RE: Simplex Algorithm
Thanks, I modified the original post with an updated hpprgm file.
 neek 

11122023, 12:38 AM
Post: #4




RE: Simplex Algorithm
(11112023 11:51 PM)Albert Chan Wrote: Also, I added "=" constraints, by adding "≥" on top of "≤" constraints. Some benchmarks. On my old laptop, running HP Prime emulator (2018 10 16) > time(simplex_le(m, 2)) → time = 0.0195 s (11112023 04:54 AM)ftneek Wrote: > simplex2([[3,2,1,1,0,10],[2,5,3,0,1,15],[2,3,4,0,0,0],[0,0,0,1,1,0]],{4,5},5,2,0) I have no idea how this is setup, but it run faster , time = 0.0105 s Use simplex wrapper is easier, but cost a bit more, time = 0.0125 s > simplex([[3,2,1,1,0,10],[2,5,3,0,1,15],[2,3,4,0,0,0],[0,0,0,1,1,0]]) (11112023 01:43 AM)Albert Chan Wrote: > m := [[3,2,1,10], [2,5,3,15], [2,3,4,0]] > simplex_le(m, ∞) /* "=" constraints are gone! */ [130/7, [15/7, 0, 25/7, 15/7, 0], ...] Fastest is remove excess variables, turning it into slack variables. Note that we scale pivot as 1 before clearing other cells. This does not affect other constraints, not even cost functions. In other words, row operations are safe. Time (including pivot operations) = 0.0075 s Production code need pivots selection, but should not cost much. 

11122023, 01:44 AM
(This post was last modified: 11122023 07:14 AM by ftneek.)
Post: #5




RE: Simplex Algorithm
Thank you for the benchmarks. Now that I thought about it a little, since the simplex_le() method assumes the n constraints are <=, we know we need to add n slack variables. Thus if we make a list of the last n column indices (not counting/using b column) to use as initial basic variables, we can call simplex2() directly as we know all the arguments. This should speed up simplex_le() a bit. This is the idea, but I have a little trouble actually making the list at the moment.
Code: a := simplex2(a,makelist(I,I,b(2),b(2)+b(1)),b(2)+b(1)1,0,0); A similar idea could be used for artificial variables. Since simplex_le() assumes the top dir constraints are "=", this makes things easy. After the added identity matrix, we should add dir more columns (identity matrix like). But we should also add a row of 0's with 1's in positions of the new dir columns (the last dir columns, not counting/using b column). Again we can make a list of indices of the added columns and call simplex2 directly, bypassing the simplex() wrapper. Except this time we call simplex2(a,list,v,dir,0) since we have dir artificial variables. This is an "initial tableau" so we should not ignore any columns yet. (11122023 12:38 AM)Albert Chan Wrote:(11112023 04:54 AM)ftneek Wrote: > simplex2([[3,2,1,1,0,10],[2,5,3,0,1,15],[2,3,4,0,0,0],[0,0,0,1,1,0]],{4,5},5,2,0) It is just manually entering the arguments simplex() tries to find. 2 constraints > 2 basic variables. since all constraints are "=", they are the positions of 1's in the last row, hence positions {4,5}. We have 3 variables and 2 artificial variables > v=5. 2 "=" constraints > 2 artificial variables. Because we have artificial variables and it's an initial tableau, the last argument is 0. (11122023 12:38 AM)Albert Chan Wrote:(11112023 11:51 PM)Albert Chan Wrote: > m := [[3,2,1,10], [2,5,3,15], [2,3,4,0]]> time(simplex_le(m, 2)) → time = 0.0195 s Compare the time when supplying the equivalent inputs for simplex2 using <= + >= method, to see the time saved by bypassing the simplex wrapper. > simplex2([[3,2,1,1,0,0,0,10],[2,5,3,0,1,0,0,15],[3,2,1,0,0,1,0,10],[2,5,3,0,0,0,1,15],[2,3,4,0,0,0,0,0]],{4,5,6,7},7,0,0) [130/7,[15/7,0,25/7,0,0,0,0],...]  neek 

11122023, 05:11 PM
(This post was last modified: 11122023 11:23 PM by Albert Chan.)
Post: #6




RE: Simplex Algorithm
(11122023 12:38 AM)Albert Chan Wrote: Fastest is remove excess variables, turning it into slack variables. I don't know a good name for this algorithm, so I just use remove_eq_c(a, n) It remove top n "=" constraints. (n variables, if constraints are independent) In other words, only "≤" constraints remain. simplex_le(a, ±n) ≡ simplex_le(remove_eq_c(a,n), ±∞) simplex_le(a, dir) only had 1 change. Instead of more constraints, it now do less variables. < IF b<len(a) THEN a := extend(a[1 .. b],a) END > IF b<len(a) THEN a := remove_eq_c(a, b) END Code: pivot_index(a) := Benchmark example: > m := [[3,2,1,10], [2,5,3,15], [2,3,4,0]] > time(r := simplex_le(m, 2)) → 0.0077 > r \([\displaystyle \frac{130}{7},[\frac{15}{7},0,\frac{25}{7},\frac{15}{7},0],\left(\begin{array}{cccccc} 1 & \frac{1}{7} & 0 & 1 & \frac{1}{7} & \frac{15}{7} \\ 0 & \frac{11}{7} & 1 & 0 & \frac{11}{7} & \frac{25}{7} \\ 0 & \frac{25}{7} & 0 & 0 & \frac{25}{7} & \frac{130}{7} \end{array}\right) ]\) Update Nov 12, 2023: simplex_le directly call simplex2, instead of wrapper simplex. see https://www.hpmuseum.org/forum/thread20...#pid180152 

11122023, 05:40 PM
(This post was last modified: 11122023 06:47 PM by ftneek.)
Post: #7




RE: Simplex Algorithm
(11122023 05:11 PM)Albert Chan Wrote: > m := [[3,2,1,10], [2,5,3,15], [2,3,4,0]] (11112023 04:54 AM)ftneek Wrote: > simplex2([[3,2,1,1,0,10],[2,5,3,0,1,15],[2,3,4,0,0,0],[0,0,0,1,1,0]],{4,5},5,2,0) Reminder: to avoid incorrect answers for the Dual solution, the 15/7 should really be a 0. This likely just requires either a small modification or new sol() command. The sol command reads a solution for variable I if there there's a 1 and rest 0's in a column. If the column is not in canonical form, the Ith variable is 0. Specifically, your output has 3 columns in "canonical" form, columns 1,3,4. But column 4 has the 1 in the same position as column 1, thus the answer read for x4 should probably be 0. This issue does not arise when the system is in standard form and the algorithm is run traditionally.  neek 

11122023, 08:46 PM
Post: #8




RE: Simplex Algorithm
(11122023 05:40 PM)ftneek Wrote: Reminder: to avoid incorrect answers for the Dual solution, the 15/7 should really be a 0. This likely just requires either a small modification or new sol() command. Code: sol(a,v):= 

11122023, 10:09 PM
Post: #9




RE: Simplex Algorithm
Ok, your change to sol() don't seem to change the output of simplex2() (on the examples from the thread that I tried), so I think I will accept it.
Also, I misspoke earlier. Before, the 15/7 effected the primal solution (though it does not really matter as it is an artificial variable, they usually end up being 0). What matters for the Dual solution is the values of the final c row. In short, for this example: Min > Max 2 "=" constraints > y1,y2 unrestricted x1,x2,x3>=0 > 3 "<=" constraints Constant terms of constraints > Objective function coefficients Objective function coefficients > Constraint terms of constraints Constraint coefficients matrix A^t > A Quote:Solving a minimization problem containing equality constraints, a sign change adjustment is necessary when determining an optimal solution point to the dual. The value of each unrestricted variable in the optimal solution point to the dual is the negative of the entry in the bottom row of the associated artificial variable column. (Because for a maximization problem, the corresponding minimization problem is considered, which necessitates an initial sign change in the objective function coefficients when entered in to the initial tableau.)Thus y1=2/7,y2=10/7 is the solution to the associated dual maximization problem. Negative solutions are valid, since y2 is unrestricted. This is generally relevant and important information for price analysis and business related questions. When I get around to it (busy today), I think it would be helpful to compile some test cases of the examples shared on the forum so far. Any proposed changes to the code base should not affect the output of simplex2() with correct arguments.  neek 

11122023, 11:04 PM
(This post was last modified: 11122023 11:34 PM by Albert Chan.)
Post: #10




RE: Simplex Algorithm
(11122023 01:44 AM)ftneek Wrote: Thank you for the benchmarks. Now that I thought about it a little, since the simplex_le() method assumes the n constraints are <=, we know we need to add n slack variables. Thus if we make a list of the last n column indices (not counting/using b column) to use as initial basic variables, we can call simplex2() directly as we know all the arguments. This should speed up simplex_le() a bit. Nice idea. Updated to simplex_le latest version. < a := simplex(a); > a := simplex2(a,range(b(2),sum(b)1),sum(b)2,0,0); range(...) are column location of 1's of identity matrix. variables = rows + cols  2 = sum(b)  2 No artificial variables, thus the doubled 0's (11112023 04:54 AM)ftneek Wrote: Note I think there is a bug with my wrapper simplex() method, sometimes the artificial variable counter is not updated. So far the bad effect is for some systems with artificial variables. But also note that simplex2() works as intended with the correct arguments. I need to determine a correct (and hopefully efficient) way to identify artificial variables without false positives, as that would have a bad effect for systems without artificial variables. Call simplex2 directly, simplex_le is more robust. Run faster too (was 0.0077 s) > m := [[3,2,1,10], [2,5,3,15], [2,3,4,0]] > time(simplex_le(m, 2)) → 0.0058 Just curious, if simplex2 can assume no artificial variables, can it speed up more? 

11132023, 01:51 AM
(This post was last modified: 11132023 09:46 AM by ftneek.)
Post: #11




RE: Simplex Algorithm
(11122023 11:04 PM)Albert Chan Wrote: Just curious, if simplex2 can assume no artificial variables, can it speed up more? Artificial variables only really affect the indices to use in checks of b column and c row, and there is an IF art>0 inside the while loop. It is only used once, but the if statement is reached when the algorithm decides to exit the while loop or use Dual Simplex. Once artificial variables have served their purpose, ign:=art (so we will not consider the artificial entries in the c row, they are allowed to be negative) and art:=0, the IF art>0 statement has been used and won't be needed again. Also n:=1, now we only ignore the c row/z entry in the b column, as the artificial row was deleted. I'm not sure the time saved by removing those parts of the code and maintaining a separate version would be worth it. But maybe it could be possible to simplify the arguments of simplex2. There are potentially optimizations that could be made as well. For example sometimes the matrix is already given in canonical form, or some columns already are. Then it makes sense to omit row operations from the basic variable columns already in canonical form, but I'm not sure if detecting that would have a smaller computation cost than just keeping it as is. The IF aij!=0 in canonicalForm is to prevent division by zero, and may already handle it. Again, I would like to set up some tests to ensure that any changes to the code do not effect the output of simplex2.  neek 

11132023, 02:48 PM
(This post was last modified: 11132023 10:52 PM by Albert Chan.)
Post: #12




RE: Simplex Algorithm
Another optimization, remove_eq_c(a,n) now also return pivots information.
simplex_le(a, ±n) use pivots to make less slack variables (smaller identity matrix) Code: pivot_index(a) := Benchmark example, time reduced to 0.0050 s. For this example, no slack variables! > m := [[3,2,1,10], [2,5,3,15], [2,3,4,0]] > simplex_le(m, 2) \(\displaystyle [ \frac{130}{7},[\frac{15}{7},0,\frac{25}{7}],\left(\begin{array}{cccc} 1 & \frac{1}{7} & 0 & \frac{15}{7} \\ 0 & \frac{11}{7} & 1 & \frac{25}{7} \\ 0 & \frac{25}{7} & 0 & \frac{130}{7} \end{array}\right) ]\) An Introduction to Linear Programming and Game Theory 3rd Edition, Paul R. Thie and Gerard E. Keough Chapter 3.7 Redundant system, example 2. Again, no slack variables! > m := [[1,2,0,1,20], [2,1,1,0,10], [1,4,2,3,40], [1,4,3,2,0]] > simplex_le(m, 3) \([35,[5,0,0,15],\left(\begin{array}{ccccc} 0 & \frac{3}{2} & \frac{1}{2} & 1 & 15 \\ 1 & \frac{1}{2} & \frac{1}{2} & 0 & 5 \\ 0 & \frac{1}{2} & \frac{7}{2} & 0 & 35 \end{array}\right) ]\) We have redundant "=" constraint, net of only 2 "=" constraints. > remove_eq_c(m, 3) \([\left(\begin{array}{ccccc} 0 & 1 & \frac{1}{3} & \frac{2}{3} & 10 \\ 1 & 0 & \frac{2}{3} & \frac{1}{3} & 0 \\ 0 & 0 & \frac{11}{3} & \frac{1}{3} & 40 \end{array}\right) ,[2,1] ]\) This simulate my previous version, not using pivot information. > simplex_le(Ans[1], inf) \([35,[5,0,0,15,0,0],\left(\begin{array}{ccccccc} 0 & \frac{3}{2} & \frac{1}{2} & 1 & \frac{3}{2} & 0 & 15 \\ 1 & \frac{1}{2} & \frac{1}{2} & 0 & \frac{1}{2} & 1 & 5 \\ 0 & \frac{1}{2} & \frac{7}{2} & 0 & \frac{1}{2} & 0 & 35 \end{array}\right) ]\) Update: HP Prime had a weird behavior list /= k interpeted as list := list * (1/k) > m := [3.0] > m /= 3.0 > m[1]  1 → −3.5527136788e−15 Thus, in remove_eq_c, to turn pivot as 1. (exactly), we do a[j] := a[j]/a[j][k] I have made changes to ftneek code as well. Now pivot of 1. is exactly 1 sol(a,v) (and others) are reverted back to original, comparing pivot 1 instead of fuzzy 1.0 

11132023, 05:34 PM
(This post was last modified: 11132023 09:41 PM by ftneek.)
Post: #13




RE: Simplex Algorithm
Albert, I do think it is interesting that it seems your "shortcut" method is able to return the same primal solution in the examples you've shown. If the shortcut method correctly provides solutions to larger systems with mixed constraints, and you are not interested in the dual solution, I suppose it could be worth keeping as an option if speed is truly a concern on larger systems.
But the algorithm was designed to simultaneously solve the dual problem as well. If you do not use artificial variables, "half" the information the algorithm was designed to provide is missing. I guess it makes sense then that your shortcut takes roughly half as much time... But I think for this shortcut to be absolutely useful, there should be some way to recover the final tableau obtained by calling simplex2 on the system in proper standard form. Additionally, slack variables generally represent “unused” resources. For example if the constraint is x1 + x2<=30, and x1=5 and x2=10, then the slack variable should equal 15, because that is the remaining quantity of whatever the constraint represents (x1+x2+s1=30 > s1=15). It is nice to provide this information as well with the solutions, eg provide x3=15. Quote:Since an artificial variable cannot be removed from the basis, the original system of constraints contains one redundant equation.Also, I think this may be a nice piece of information to include in the result. If an artificial variable remains a basic variable at the final tableau, then the system contains a redundant equation. This should be easy enough to check. But do you have any suggestions about how to present this information to the user? I think it is probably best to keep the return type as a list of 3 items if possible. Proper standard form arguments for Example 3.7.2: > simplex2([[1,2,0,1,1,0,0,20],[2,1,1,0,0,1,0,10],[1,4,2,3,0,0,1,40],[1,4,3,2,0,0,0,0],[0,0,0,0,1,1,1,0]],{5,6,7},7,3,0) [35,[5,0,0,15,0,0,0],[[1,1/2,1/2,0,3/4,0,1/4,5],[0,0,0,0,3/2,1,1/2,0],[0,3/2,1/2,1,1/4,0,1/4,15],[0,1/2,7/2,0,5/4,0,1/4,35]]]  neek 

11132023, 10:46 PM
Post: #14




RE: Simplex Algorithm
Hi, ftneek
It is not just a shortcut (as in hack), there is no lost of information. Row operations represent *exactly* the original problem, but with less variables to handle. We are adding equal amount to both side. All constraints, even cost function is unaffected. > m := [[1,2,0,1,20], [2,1,1,0,10], [1,4,2,3,40], [1,4,3,2,0]] > m2 := remove_eq_c(m, 3)[1] \(\left(\begin{array}{ccccc} 0 & 1 & \frac{1}{3} & \frac{2}{3} & 10 \\ 1 & 0 & \frac{2}{3} & \frac{1}{3} & 0 \\ 0 & 0 & \frac{11}{3} & \frac{1}{3} & 40 \end{array}\right) \) Once variable are unrelated to cost function, it automatically becomes "slack". You can adjust them whatever you want, to fit your need (max or min) Yes, identity matrix on the "wrong" side, but it does not matter. (if you prefer, swap columns to make identity matrix look right) We don't need any more slack variables. > simplex2(m2, [2,1], 4, 0, 0) \([35,[5,0,0,15],\left(\begin{array}{ccccc} 0 & \frac{3}{2} & \frac{1}{2} & 1 & 15 \\ 1 & \frac{1}{2} & \frac{1}{2} & 0 & 5 \\ 0 & \frac{1}{2} & \frac{7}{2} & 0 & 35 \end{array}\right) ]\) ftneek Wrote:If an artificial variable remains a basic variable at the final tableau, then the system contains a redundant equation. This should be easy enough to check. But do you have any suggestions about how to present this information to the user? Problem matrix of 4 rows, Solution matrix of 3 rows > 1 redundant "=" constraint. 

11142023, 02:25 AM
Post: #15




RE: Simplex Algorithm
(11132023 05:34 PM)ftneek Wrote: Proper standard form arguments for Example 3.7.2: You can do this with simplex_le too, but not much typing is saved. Constraints already are "=", artificial variables need to start with 1, thus result negated. > simplex_le([[1,2,0,1,1,0,0,20],[2,1,1,0,0,1,0,10],[1,4,2,3,0,0,1,40],[1,4,3,2,0,0,0,0]],3) [35,[5,0,0,15,0,0,0],[[0,3/2,1/2,1,1/4,0,1/4,15],[0,0,0,0,3/2,1,1/2,0],[1,1/2,1/2,0,3/4,0,1/4,5],[0,1/2,7/2,0,5/4,0,1/4,35]]] Inside numbers not quite match, because simplex_le use pivots order {5,6,1}. But that shouldn't matter. 

11142023, 06:24 AM
Post: #16




RE: Simplex Algorithm
(11142023 02:25 AM)Albert Chan Wrote: You can do this with simplex_le too, but not much typing is saved. Does that input still save computing time? That is closer, potentially promising. Order of pivots does not matter. But the coefficients of the objective function/z row should match simplex2, including in sign, else the dual solution will not be correct. To me it seems those columns need to be multiplied by 1, except for the entries where the b coefficient is 0... But to me this just seems to be creating new, strange rules and complicating things. For example adding negative slack variables to "=" constraints, why? That does serve a purpose, but I don't think it's explained in this textbook. I know another book that refers to that has "soft and hard constraints" for when you sometimes want to allow the "=" constraints to be violated for a cost, my upcoming project actually uses that idea. If it does not save computing time, I suggest just using the intended standard form. As I mentioned in the other thread, artificial variables are not always required to solve a system, but can always be used as starting place for the simplex algorithm, and their use cuts down on computation time (when compared to size of <= + >= matrix). Learning how to put systems in standard form is not very difficult, there are only a few rules to follow for when the following arise: ">=","=","<=", unrestricted variables. I think that's everything.. Once you learn the few rules you should be able to handle most systems. But I agree it is not convenient to have to enter this information manually. A program to convert to standard form for the user would be immensely convenient and useful. That was what I was going to start with originally, but handling user input would have taken too much time for me then. So I just wrote the main simplex2 code (the original simplex()) with the assumption that it will be provided the correct inputs to use, ideally by a helper method that sets everything up for you, which was my goal for simplex() (current version). The algorithm does make some assumptions, and in return makes some guarantees. Namely if it is provided a non degenerate matrix in standard form, it is guaranteed to return the optimal solution to the primal and dual system (or determine no solution) in a finite number of iterations. Even if the matrix is degenerate, there are a few things we can try that should guarantee finiteness. If the assumption is not met, I don't know what is guaranteed. If you want an algorithm that does not use artificial variables, there are some out there. I encourage you to read the article/link in the source comment. If I were to implement another simplex algorithm, it would probably be that one unless I find something more promising. It uses no artificial variables, requires less multiplications and additions, and has the dual solution embedded in the matrix. But I follow this text this semester, as my course uses it heavily. I don't want to make it seem like I'm squashing this idea. If you just want a faster simplex algorithm for large problems, that's fine. But (unless you can figure out how to consistently match the signs of simplex2) it should be made clear that the intention is to only provide solutions to the primal, and that finding the dual solution would require converting to dual problem AND rewriting in standard form (maybe not if using your method) AND another round of simplex iteration, probably expensive for a large system. Needing to run a second tableau, the computation time is probably at least back to running simplex2 as originally intended... My original intention was for extensions of the program to make it easier to use, extract the desired information, or add functionality without limiting its capabilities. In my opinion without the dual solution available, the capabilities are being limited. But it should useful if you want only the primal, fast.  neek 

11142023, 09:23 AM
Post: #17




RE: Simplex Algorithm
(11142023 06:24 AM)ftneek Wrote:(11142023 02:25 AM)Albert Chan Wrote: You can do this with simplex_le too, but not much typing is saved. Constraint (a*x ≤ b) is setup as matrix [ab], with a 1 column b To turn "≤" into "=", we do a*x + (slack var) = b + (artificial var) But we can't setup this way, thus a*x + (slack var) + (−artficial var) = b (−artificial var) = [5/4,0,1/4] (artificial var) = −[5/4,0,1/4] // answer do match simplex2 Purpose of wrapper function is not to save computing time, but to save typing time. But yes, to match result of standard form, you might as well just use simplex2. Again, simplex_le() is just a wrapper for simplex2(). I have nothing against the standard form, except for typing it all in. simplex_le can do dual problem, with identity matrix squeeze inside. Video 1st dual probelm (time = 6 .. 17 minutes) > simplex_le([[2,4,40],[1,1,12],[5,1,40],[20,30,0]],+∞) \([320,[4,8,0,0,12],\left(\begin{array}{cccccc} 0 & 1 & \frac{1}{2} & 1 & 0 & 8 \\ 1 & 0 & \frac{1}{2} & 2 & 0 & 4 \\ 0 & 0 & 2 & 9 & 1 & 12 \\ 0 & 0 & 5 & 10 & 0 & 320 \end{array}\right) ]\) Note that P column is always [0,0 .. 0,1] thus not needed to carry along. 

11142023, 10:39 AM
Post: #18




RE: Simplex Algorithm
(11142023 09:23 AM)Albert Chan Wrote: simplex_le can do dual problem, with identity matrix squeeze inside. That is impressive, much less typing, and matches the standard form output of simplex2. Can simplex_le() already solve dual problem with "=" constraints? I think the only thing it really needs is a way to set up artificial variables. Odd, for whatever reason the video explained it the opposite way from my understanding. According to our source, primal is given by b column while dual is found in c row. Anyhow, the final tableau matches simplex2 exactly! If you would like to make another optimization, there is potentially one regarding duality. Sometimes the dual or the primal is easier to solve (it might have positive variables or only needs slack variables, or a certain quality that makes it "easier"). Choosing the "easier" problem to solve may require less computation, and the solution to the harder problem is provided by duality. Finally, I had a little time to start making some "tests". But for some reason test 3 returns 0, even after directly copying the result from the command history... Would you happen to know why? I'm not sure if this is the best way to maintain tests, and it would be nice if they don't all show up in the catalog, only a single wrapper that runs them. Since we only are checking the equivalency of 2 items, does it really need to be a cas program? I would appreciate tips on how to organize it, I think it's important to ensure refactoring simplex2 does not cause correct solutions to change. Code: #cas  neek 

11152023, 04:36 PM
Post: #19




RE: Simplex Algorithm
Hi, ftneek
Why simplex2() able to handle negative b column, but HP Prime default simplex_reduce() can't ? Is the b≥0 requirement arbitrarily added, or does it serve a purpose? 

11152023, 05:58 PM
(This post was last modified: 11162023 07:29 AM by ftneek.)
Post: #20




RE: Simplex Algorithm
simplex2 incorporates Dual Simplex algorithm. Using (non dual) simplex, in the end if c row is all positive we assume we have reached the optimum. But if there is a negative in b column, the answer is considered infeasible as the assumption is all variables are non negative. Using dual simplex, if the "final tableau" would have a negative b iteration continues. If a c becomes negative again, regular simplex is used until all c are positive and there is a negative b.
So I think b>=0 requirement means only regular simplex will be used. For whatever reason test 3 kept returning 0 on my virtual and physical calculator despite both sides of "==" being visually the same. I believe there to be some kind of issue, at least on my end. Why would "==" return 0 if the objects are the same? It seemed to work fine on the other 2 tests. Even if I were to reset my virtual and physical calcs, I hope to figure out the cause so that this issue doesn't randomly pop up again sometime in the future. Using size(difference())==0 seems to work.. But I guess using this, it would be possible for a false pass if the values are all the same but ordered differently. Code: #cas edit: I added the rest of the examples shared in the threads as tests and added the problem statements to the comments. For any change to simplex2, test_simplex() should still return all 1's (at minimum). Feel free to add more examples as you encounter them. If you want to check an answer before adding it, lpsolve on xcas can be used as a reference. It might be nice to have a method that can read problems stored in a file, once we have a method to convert a system to standard form. That would probably make it easy to transfer problems found online to the calculator if needed. If anyone has a hint for not exposing all the test cases in the catalog, please let me know.  neek 

« Next Oldest  Next Newest »

User(s) browsing this thread: 1 Guest(s)