def solve_tsp(prob, options={}): # Set the options prob.options = options # When branching, use a callback (just for # showing a fractional TSP solution) prob.branch_method = user_branch_tsp # # # When checking feasibility, use a callback # to check for subtours prob.is_solution_feasible = is_solution_feasible_tsp # # # When generating cuts, use a callback # to generate subtour elimination constraints prob.generate_cuts = generate_cuts_tsp # # dippyOpts = { # 'CutCGL': 1, # <----- Cuts turned on 'CutCGL': 0, # <----- Cuts turned off # 'LogDumpModel': 5, # 'LogDebugLevel': 5, } # Can use Cut Generator Library (CGL) cuts too if ("Cuts" in options) and (options["Cuts"] == "CGL"): dippyOpts['CutCGL'] = 1 if "Interval" in options: prob.display_interval = options["Interval"] # plt.figure(figsize=FIGSIZE) status, message, primals, duals = dippy.Solve(prob, dippyOpts) if status == LpStatusOptimal: return dict((var, var.value()) for var in prob.variables()) else: return None
def test_dippy_branch(self): """ tests that the custom branch can solve the problem """ self.prob.branch_method = self.do_branch dippy.Solve(self.prob, {}) self.assertAlmostEqual(self.prob.objective.value(), 191078860.725, 2) self.variable_feasibility_test(self.prob) self.constraint_feasibility_test(self.prob)
def test_dippy_cuts(self): """ tests that the custom branch can solve the problem """ self.prob.generate_cuts = self.cuts dippy.Solve(self.prob, { 'TolZero': '%s' % self.tol, }) self.assertAlmostEqual(self.prob.objective.value(), 212.0) self.variable_feasibility_test(self.prob) self.constraint_feasibility_test(self.prob)
def test_dippy_solve(self): """ tests that dippy can solve the problem """ self.prob.generate_cuts = self.generate_cuts self.prob.is_solution_feasible = self.is_solution_feasible sol, duals = dippy.Solve(self.prob, {}) self.assertAlmostEqual(self.prob.objective.value(), 18.39, 2) self.variable_feasibility_test(self.prob) self.constraint_feasibility_test(self.prob) # would like this in here but not sure how to do it self.assertTrue(self.is_solution_feasible(self.prob, sol, self.tol))
def solve(prob, options={}): # Solve the TSP # Set the options prob.options = options # When checking feasibility, use a callback # to check for subtours prob.is_solution_feasible = is_solution_feasible # When generating cuts, use a callback # to generate subtour elimination constraints prob.generate_cuts = generate_cuts prob.branch_method = my_branch if ("Root" in options) and (options["Root"] is not None): prob.is_root_node = True prob.root_heuristic = True prob.heuristics = my_heuristics else: prob.root_heuristic = False if ("Node" in options) and (options["Node"] is not None): prob.is_root_node = True prob.node_heuristic = True prob.heuristics = my_heuristics else: prob.node_heuristic = False dippyOpts = {} # 'CutCGL': 1, # <----- Cuts turned on # 'CutCGL': 0, # <----- Cuts turned off # 'LogDumpModel': 5, # 'LogDebugLevel': 5, # } # Can use Cut Generator Library (CGL) cuts too if "Cuts" in options: if options["Cuts"] == "CGL": dippyOpts['CutCGL'] = 1 elif options["Cuts"] == "Lazy Tight": dippyOpts['CutLazyTight'] = 1 if "Interval" in options: prob.display_interval = options["Interval"] # plt.figure(figsize=FIGSIZE) status, message, primals, duals = dippy.Solve(prob, dippyOpts) if status == LpStatusOptimal: return dict((var, var.value()) for var in prob.variables()) else: return None
def test_dippy_relaxation(self): """ tests that the custom branch can solve the problem """ self.prob, self.relaxation = create_cutting_stock_problem(doRelaxed=True) self.prob.relaxed_solver = self.relaxation dippy.Solve(self.prob, { 'doPriceCut':1, 'CutCGL': 0, }) self.assertAlmostEqual(self.prob.objective.value(), 2.0) self.variable_feasibility_test(self.prob) self.constraint_feasibility_test(self.prob)
def solve(prob, options={}): prob.options = options prob.is_solution_feasible = is_solution_feasible prob.generate_cuts = generate_cuts prob.branch_method = my_branch if ("Root" in options) and (options["Root"] is not None): prob.is_root_node = True prob.root_heuristic = True prob.heuristics = my_heuristics else: prob.root_heuristic = False if ("Node" in options) and (options["Node"] is not None): prob.is_root_node = True prob.node_heuristic = True prob.heuristics = my_heuristics else: prob.node_heuristic = False dippyOpts = { # 'CutCGL': 1, # <----- Cuts turned on 'CutCGL': 0, # <----- Cuts turned off # 'LogDumpModel': 5, # 'LogDebugLevel': 5, } if ("Cuts" in options) and (options["Cuts"] == "CGL"): dippyOpts['CutCGL'] = 1 if "Interval" in options: prob.display_interval = options["Interval"] # plt.figure(figsize=FIGSIZE) status, message, primals, duals = dippy.Solve(prob, dippyOpts) if status == LpStatusOptimal: return dict((var, var.value()) for var in prob.variables()) else: return None
def solve(prob, options={}): prob.options = options prob.is_solution_feasible = is_solution_feasible prob.generate_cuts = generate_cuts dippyOpts = { # 'CutCGL': 1, # <----- Cuts turned on 'CutCGL': 0, # <----- Cuts turned off # 'LogDumpModel': 5, # 'LogDebugLevel': 5, } if ("Cuts" in options) and (options["Cuts"] == "CGL"): dippyOpts['CutCGL'] = 1 if "Interval" in options: prob.display_interval = options["Interval"] plt.figure(figsize=FIGSIZE) status, message, primals, duals = dippy.Solve(prob, dippyOpts) if status == LpStatusOptimal: return dict((var, var.value()) for var in prob.variables()) else: return None