# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1426600135 14400 # Node ID 7413c98553c39e90c5b3215f9479049773e18229 # Parent 6015c8cfe502750d690696d6e8df6e0d6261366f main: call solver with new optional debug arguments Also add debug arguments to greedy and brute_force. diff --git a/optim.py b/optim.py --- 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()