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;
190 cout << string(60,
'=') << endl;
192 cout <<
"\nProblem:" << 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;
202 cout <<
" xA, xB >= 0" << endl;
212 double labor[] = {1.0, 1.0, 40.0};
213 double machine[] = {2.0, 1.0, 60.0};
220 auto state = solver.
solve();
222 cout <<
"\n--- Solution ---" << endl;
233 cout << fixed << setprecision(2);
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;
244 cout <<
" All constraints satisfied: YES" << endl;
269 cout <<
"\n" << string(60,
'=') << endl;
270 cout <<
"Example 2: Diet Problem (Minimization)" << endl;
271 cout << string(60,
'=') << endl;
273 cout <<
"\nProblem:" << 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;
281 cout <<
" x1 + 3*x2 >= 6" << 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;
294 double protein[] = {-3.0, -4.0, -12.0};
295 double fat[] = {-1.0, -3.0, -6.0};
301 auto state = solver.
solve();
303 cout <<
"\n--- Solution ---" << endl;
314 cout << fixed << setprecision(2);
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;
338 cout << string(60,
'=') << endl;
340 cout <<
"\nProblem:" << 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;
346 cout <<
"\nFormulation:" << endl;
347 cout <<
" Maximize: Z = 5*x + 4*y + 3*z" << endl;
348 cout <<
" Subject to: x <= 2" << endl;
349 cout <<
" y <= 3" << endl;
350 cout <<
" z <= 4" << endl;
351 cout <<
" x + y + z <= 6" << endl;
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};
372 auto state = solver.
solve();
374 cout <<
"\n--- Solution ---" << endl;
386 cout << fixed << setprecision(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;
391 cout <<
" Maximum yield: " << yield << 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;
406 cout << string(60,
'=') << 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;
418 double limit[] = {1.0, 1.0, 10.0};
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;
443 double limit1[] = {1.0, 1.0, 5.0};
444 double limit2[] = {-1.0, -1.0, -10.0};
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;
464 cout << string(60,
'=') << 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;
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};
498 auto state = solver.
solve();
504 cout <<
"\nFeasible region vertices:" << endl;
505 cout <<
" (0, 0): Z = 0" << endl;
506 cout <<
" (3, 0): Z = 3" << 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;
509 cout <<
" (0, 2): Z = 2" << endl;
511 cout << fixed << setprecision(2);
518int main(
int argc,
char* argv[])
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);
544 cmd.parse(argc, argv);
546 bool runProd = prodArg.getValue();
547 bool runDiet = dietArg.getValue();
548 bool runRes = resArg.getValue();
549 bool runSpec = specArg.getValue();
550 bool runAlgo = algoArg.getValue();
551 bool runAll = allArg.getValue();
553 if (not runProd
and not runDiet
and not runRes
and
554 not runSpec
and not runAlgo)
557 cout <<
"=== Simplex Algorithm: Linear Programming ===" << endl;
558 cout <<
"Find optimal solutions subject to linear constraints" << endl;
560 if (runAll or runProd)
563 if (runAll or runDiet)
566 if (runAll or runRes)
569 if (runAll or runSpec)
572 if (runAll or runAlgo)
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.
T objetive_value() const noexcept
Computes and returns the objective function value.
bool verify_solution() const
Verifies that the solution satisfies all constraints.
T * put_restriction(const T *coefs=nullptr, ConstraintType type=ConstraintType::LE)
Adds a constraint to the linear program.
void prepare_linear_program()
Prepares the linear program for solving.
const T & get_solution(size_t i) const noexcept
Gets the solution value for a specific variable.
void put_objetive_function_coef(size_t i, const T &coef)
Sets a coefficient in the objective function.
void load_solution() noexcept
Loads the solution values into the solution array.
State solve()
Solves the linear program.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
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.