changeset 20:7413c98553c3 draft

main: call solver with new optional debug arguments Also add debug arguments to greedy and brute_force.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 17 Mar 2015 09:48:55 -0400
parents 6015c8cfe502
children 523f1eb145be
files optim.py
diffstat 1 files changed, 12 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/optim.py
+++ b/optim.py
@@ -127,7 +127,7 @@
     print
 
 
-def brute_force(case):
+def brute_force(case, debug=False):
     """Form all possible plans, and just try them all."""
     machines = case.machines
 
@@ -144,15 +144,21 @@
     for plan in plans:
         try:
             plancapital = final_capital(case, machines, plan)
+            if debug:
+                showplan(plan, machines)
+                print "final capital: ", plancapital
             if plancapital > maxcapital:
                 maxcapital = plancapital
                 maxplan = plan
-        except InvalidPlan:
+        except InvalidPlan as e:
+            if debug:
+                showplan(plan, machines)
+                print "\t", e
             pass
 
     return maxcapital, maxplan
 
-def greedy(case):
+def greedy(case, debug=False):
     """Greedy algorithm, considering each machine only once, in decreasing
     order of maxprofit.
 
@@ -187,13 +193,12 @@
             solver = greedy
         if args.brute_force:
             solver = brute_force
-        maxcapital, plan = solver(case)
+        maxcapital, plan = solver(case, debug=args.debug)
         print "Case %d: %d" % (number + 1, maxcapital)
         if args.debug:
-            for (action, machine) in zip(plan, case.machines):
-                if action:
-                    print "Buy ", machine
             print
+            print "  Winning plan:"
+            showplan(plan, case.machines)
 
 if __name__ == "__main__":
     main()