153#include <tclap/CmdLine.h>
156using namespace Aleph;
170 default:
return "Unknown";
188 cout <<
"\n" << string(60,
'=') <<
endl;
189 cout <<
"Example 1: Production Planning Problem" <<
endl;
193 cout <<
" Factory produces two products (A and B)" <<
endl;
194 cout <<
" Product A: $40 profit, 1 hr labor, 2 hrs machine" <<
endl;
195 cout <<
" Product B: $30 profit, 1 hr labor, 1 hr machine" <<
endl;
196 cout <<
" Available: 40 labor hours, 60 machine hours" <<
endl;
198 cout <<
"\nMathematical formulation:" <<
endl;
199 cout <<
" Maximize: Z = 40*xA + 30*xB" <<
endl;
200 cout <<
" Subject to: xA + xB <= 40 (labor)" <<
endl;
201 cout <<
" 2*xA + xB <= 60 (machine)" <<
endl;
208 solver.put_objetive_function_coef(0, 40.0);
209 solver.put_objetive_function_coef(1, 30.0);
212 double labor[] = {1.0, 1.0, 40.0};
213 double machine[] = {2.0, 1.0, 60.0};
219 solver.prepare_linear_program();
220 auto state =
solver.solve();
222 cout <<
"\n--- Solution ---" <<
endl;
231 double profit =
solver.objetive_value();
234 cout <<
" Product A (xA): " <<
xA <<
" units" <<
endl;
235 cout <<
" Product B (xB): " <<
xB <<
" units" <<
endl;
236 cout <<
" Maximum profit: $" << profit <<
endl;
239 cout <<
"\n--- Constraint Verification ---" <<
endl;
240 cout <<
" Labor used: " << (
xA +
xB) <<
" / 40 hours" <<
endl;
241 cout <<
" Machine used: " << (2*
xA +
xB) <<
" / 60 hours" <<
endl;
243 if (
solver.verify_solution())
244 cout <<
" All constraints satisfied: YES" <<
endl;
269 cout <<
"\n" << string(60,
'=') <<
endl;
270 cout <<
"Example 2: Diet Problem (Minimization)" <<
endl;
274 cout <<
" Food 1: $2/unit, 3g protein, 1g fat" <<
endl;
275 cout <<
" Food 2: $4/unit, 4g protein, 3g fat" <<
endl;
276 cout <<
" Requirements: >= 12g protein, >= 6g fat" <<
endl;
278 cout <<
"\nOriginal formulation:" <<
endl;
279 cout <<
" Minimize: C = 2*x1 + 4*x2" <<
endl;
280 cout <<
" Subject to: 3*x1 + 4*x2 >= 12" <<
endl;
283 cout <<
"\nConverted to standard form:" <<
endl;
284 cout <<
" Maximize: -C = -2*x1 - 4*x2" <<
endl;
285 cout <<
" Constraints multiplied by -1 for <= form" <<
endl;
290 solver.put_objetive_function_coef(0, -2.0);
291 solver.put_objetive_function_coef(1, -4.0);
294 double protein[] = {-3.0, -4.0, -12.0};
295 double fat[] = {-1.0, -3.0, -6.0};
300 solver.prepare_linear_program();
301 auto state =
solver.solve();
303 cout <<
"\n--- Solution ---" <<
endl;
310 double x1 =
solver.get_solution(0);
311 double x2 =
solver.get_solution(1);
312 double cost = -
solver.objetive_value();
315 cout <<
" Food 1 (x1): " << x1 <<
" units" <<
endl;
316 cout <<
" Food 2 (x2): " << x2 <<
" units" <<
endl;
317 cout <<
" Minimum cost: $" << cost <<
endl;
319 cout <<
"\n--- Nutritional Check ---" <<
endl;
320 cout <<
" Protein: " << (3*x1 + 4*x2) <<
"g (need >= 12g)" <<
endl;
321 cout <<
" Fat: " << (x1 + 3*x2) <<
"g (need >= 6g)" <<
endl;
336 cout <<
"\n" << string(60,
'=') <<
endl;
337 cout <<
"Example 3: Resource Allocation" <<
endl;
341 cout <<
" Resource X: max 2 units, yields 5/unit" <<
endl;
342 cout <<
" Resource Y: max 3 units, yields 4/unit" <<
endl;
343 cout <<
" Resource Z: max 4 units, yields 3/unit" <<
endl;
344 cout <<
" Total capacity: max 6 units" <<
endl;
347 cout <<
" Maximize: Z = 5*x + 4*y + 3*z" <<
endl;
348 cout <<
" Subject to: x <= 2" <<
endl;
356 solver.put_objetive_function_coef(0, 5.0);
357 solver.put_objetive_function_coef(1, 4.0);
358 solver.put_objetive_function_coef(2, 3.0);
361 double limit_x[] = {1.0, 0.0, 0.0, 2.0};
362 double limit_y[] = {0.0, 1.0, 0.0, 3.0};
363 double limit_z[] = {0.0, 0.0, 1.0, 4.0};
364 double total[] = {1.0, 1.0, 1.0, 6.0};
371 solver.prepare_linear_program();
372 auto state =
solver.solve();
374 cout <<
"\n--- Solution ---" <<
endl;
381 double x =
solver.get_solution(0);
382 double y =
solver.get_solution(1);
383 double z =
solver.get_solution(2);
387 cout <<
" Resource X: " << x <<
" units" <<
endl;
388 cout <<
" Resource Y: " <<
y <<
" units" <<
endl;
389 cout <<
" Resource Z: " <<
z <<
" units" <<
endl;
390 cout <<
" Total allocated: " << (x +
y +
z) <<
" / 6 units" <<
endl;
393 cout <<
"\n--- Analysis ---" <<
endl;
394 cout <<
"Notice how the algorithm prioritizes higher-yield resources" <<
endl;
395 cout <<
"while respecting all constraints." <<
endl;
404 cout <<
"\n" << string(60,
'=') <<
endl;
405 cout <<
"Special Cases: Unbounded and Infeasible" <<
endl;
409 cout <<
"\n--- Case 1: Well-defined problem ---" <<
endl;
410 cout <<
"Maximize: Z = x + y" <<
endl;
411 cout <<
"Subject to: x + y <= 10" <<
endl;
415 solver.put_objetive_function_coef(0, 1.0);
416 solver.put_objetive_function_coef(1, 1.0);
418 double limit[] = {1.0, 1.0, 10.0};
421 solver.prepare_linear_program();
422 auto state =
solver.solve();
433 cout <<
"\n--- Case 2: Conflicting constraints ---" <<
endl;
434 cout <<
"Maximize: Z = x + y" <<
endl;
435 cout <<
"Subject to: x + y <= 5" <<
endl;
436 cout <<
" x + y >= 10 (converted to -x - y <= -10)" <<
endl;
440 solver.put_objetive_function_coef(0, 1.0);
441 solver.put_objetive_function_coef(1, 1.0);
443 double limit1[] = {1.0, 1.0, 5.0};
444 double limit2[] = {-1.0, -1.0, -10.0};
449 solver.prepare_linear_program();
450 auto state =
solver.solve();
453 cout <<
"(x + y can't be both <= 5 and >= 10 simultaneously)" <<
endl;
462 cout <<
"\n" << string(60,
'=') <<
endl;
463 cout <<
"Understanding the Simplex Algorithm" <<
endl;
466 cout <<
"\n--- The Simplex Method Steps ---" <<
endl;
467 cout <<
"1. Convert to standard form (slack variables)" <<
endl;
468 cout <<
"2. Build initial simplex tableau" <<
endl;
469 cout <<
"3. Find pivot column (most negative in objective row)" <<
endl;
470 cout <<
"4. Find pivot row (minimum ratio test)" <<
endl;
471 cout <<
"5. Pivot to improve solution" <<
endl;
472 cout <<
"6. Repeat until optimal (no negative in objective row)" <<
endl;
474 cout <<
"\n--- Geometric Interpretation ---" <<
endl;
475 cout <<
"The constraints define a convex polytope (feasible region)." <<
endl;
476 cout <<
"Simplex moves along edges of this polytope," <<
endl;
477 cout <<
"visiting vertices until it finds the optimum." <<
endl;
478 cout <<
"Each pivot operation moves to an adjacent vertex" <<
endl;
479 cout <<
"with a better (or equal) objective value." <<
endl;
481 cout <<
"\n--- Simple Example Visualization ---" <<
endl;
482 cout <<
"Maximize: Z = x + y" <<
endl;
483 cout <<
"Subject to: x <= 3, y <= 2, x + y <= 4" <<
endl;
486 solver.put_objetive_function_coef(0, 1.0);
487 solver.put_objetive_function_coef(1, 1.0);
489 double c1[] = {1.0, 0.0, 3.0};
490 double c2[] = {0.0, 1.0, 2.0};
491 double c3[] = {1.0, 1.0, 4.0};
497 solver.prepare_linear_program();
498 auto state =
solver.solve();
504 cout <<
"\nFeasible region vertices:" <<
endl;
507 cout <<
" (3, 1): Z = 4 <-- binding x<=3 and x+y<=4" <<
endl;
508 cout <<
" (2, 2): Z = 4 <-- binding y<=2 and x+y<=4" <<
endl;
512 cout <<
"\nSimplex found: (" <<
solver.get_solution(0)
513 <<
", " <<
solver.get_solution(1) <<
")" <<
endl;
522 TCLAP::CmdLine
cmd(
"Simplex Linear Programming Example",
' ',
"1.0");
524 TCLAP::SwitchArg
prodArg(
"p",
"production",
525 "Show production planning example",
false);
526 TCLAP::SwitchArg
dietArg(
"d",
"diet",
527 "Show diet problem example",
false);
528 TCLAP::SwitchArg
resArg(
"r",
"resources",
529 "Show resource allocation example",
false);
530 TCLAP::SwitchArg
specArg(
"s",
"special",
531 "Show special cases (unbounded, infeasible)",
false);
532 TCLAP::SwitchArg
algoArg(
"g",
"algorithm",
533 "Show algorithm explanation",
false);
534 TCLAP::SwitchArg
allArg(
"a",
"all",
535 "Run all demos",
false);
557 cout <<
"=== Simplex Algorithm: Linear Programming ===" <<
endl;
558 cout <<
"Find optimal solutions subject to linear constraints" <<
endl;
575 cout <<
"\n=== Summary ===" <<
endl;
576 cout <<
"Simplex is the workhorse of linear programming." <<
endl;
577 cout <<
"Standard form: Maximize c'x subject to Ax <= b, x >= 0" <<
endl;
578 cout <<
"Convert: Min -> Max (negate), >= -> <= (negate)" <<
endl;
579 cout <<
"Applications: Production, logistics, finance, scheduling" <<
endl;
583 catch (TCLAP::ArgException& e)
585 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() <<
endl;
Simplex algorithm for linear programming.
Linear program solver using the Simplex method.
State
Solution states for the linear program.
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
void demo_special_cases()
Demonstrate unbounded and infeasible cases.
string state_to_string(Simplex< double >::State state)
Helper to print solution state.
void demo_diet_problem()
Diet Problem (classic LP problem)
void demo_resource_allocation()
Transportation Problem (simplified)
void demo_production_planning()
Classic example: Production Planning.
void demo_algorithm_steps()
Demonstrate the algorithm steps.