def solve_parametric(): m = create_warehouse_model(num_locations=50, num_customers=50) opt = pyo.SolverFactory('gurobi') p_values = list(range(1, 31)) obj_values = list() for p in p_values: m.P.value = p res = opt.solve(m) assert_optimal_termination(res) obj_values.append(res.problem.lower_bound)
def solve_parametric_persistent(): m = create_warehouse_model(num_locations=50, num_customers=50) opt = pyo.SolverFactory('gurobi_persistent') opt.set_instance(m) p_values = list(range(1, 31)) obj_values = list() for p in p_values: m.P.value = p opt.remove_constraint(m.num_warehouses) opt.add_constraint(m.num_warehouses) res = opt.solve(save_results=False) assert_optimal_termination(res) obj_values.append(res.problem.lower_bound)
def _ip_helper(self, with_bounds, with_ineq, linear_solver): t0 = 0 delta_t = 1 num_finite_elements = 90 constant_control_duration = 10 time_scale = 0.1 num_time_blocks = 3 interface = Problem(t0=t0, delta_t=delta_t, num_finite_elements=num_finite_elements, constant_control_duration=constant_control_duration, time_scale=time_scale, num_time_blocks=num_time_blocks, with_bounds=with_bounds, with_ineq=with_ineq) options = parapint.algorithms.IPOptions() options.linalg.solver = linear_solver status = parapint.algorithms.ip_solve(interface=interface, options=options) self.assertEqual(status, parapint.algorithms.InteriorPointStatus.optimal) interface.load_primals_into_pyomo_model() x = dict() p = dict() local_block_indices = _distribute_blocks(num_time_blocks, rank, size) for ndx in local_block_indices: m = interface.pyomo_model(ndx) for t in m.x_time_points: if t in x: self.assertAlmostEqual(x[t], m.x[t].value) else: x[t] = m.x[t].value for t in m.p_time_points: p[t] = m.p[t].value m = build_time_block(t0=t0, delta_t=delta_t, num_finite_elements=num_finite_elements, constant_control_duration=constant_control_duration, time_scale=time_scale, with_bounds=with_bounds, with_ineq=with_ineq) opt = pe.SolverFactory('ipopt') res = opt.solve(m, tee=True) assert_optimal_termination(res) for _t, _x in x.items(): self.assertAlmostEqual(_x, m.x[_t].value) for _t, _p in p.items(): self.assertAlmostEqual(_p, m.p[_t].value)
def test_kkt_system(self): t0 = 0 delta_t = 1 num_finite_elements = 90 constant_control_duration = 10 time_scale = 0.1 num_time_blocks = 3 interface = Problem(t0=t0, delta_t=delta_t, num_finite_elements=num_finite_elements, constant_control_duration=constant_control_duration, time_scale=time_scale, num_time_blocks=num_time_blocks) interface.set_primals(interface.init_primals()) interface.set_slacks(interface.init_slacks()) interface.set_duals_eq(interface.init_duals_eq()) interface.set_duals_ineq(interface.init_duals_ineq()) interface.set_duals_primals_lb(interface.init_duals_primals_lb()) interface.set_duals_primals_ub(interface.init_duals_primals_ub()) interface.set_duals_slacks_lb(interface.init_duals_slacks_lb()) interface.set_duals_slacks_ub(interface.init_duals_slacks_ub()) interface.set_barrier_parameter(0) kkt = interface.evaluate_primal_dual_kkt_matrix() rhs = interface.evaluate_primal_dual_kkt_rhs() linear_solver = parapint.linalg.ScipyInterface() linear_solver.do_symbolic_factorization(kkt) linear_solver.do_numeric_factorization(kkt) sol = linear_solver.do_back_solve(rhs) interface.set_primal_dual_kkt_solution(sol) interface.set_primals(interface.get_primals() + interface.get_delta_primals()) interface.set_slacks(interface.get_slacks() + interface.get_delta_slacks()) interface.set_duals_eq(interface.get_duals_eq() + interface.get_delta_duals_eq()) interface.set_duals_ineq(interface.get_duals_ineq() + interface.get_delta_duals_ineq()) interface.set_duals_primals_lb(interface.get_duals_primals_lb() + interface.get_delta_duals_primals_lb()) interface.set_duals_primals_ub(interface.get_duals_primals_ub() + interface.get_delta_duals_primals_ub()) interface.set_duals_slacks_lb(interface.get_duals_slacks_lb() + interface.get_delta_duals_slacks_lb()) interface.set_duals_slacks_ub(interface.get_duals_slacks_ub() + interface.get_delta_duals_slacks_ub()) interface.load_primals_into_pyomo_model() x = dict() p = dict() for ndx in range(num_time_blocks): m = interface.pyomo_model(ndx) for t in m.x_time_points: if t in x: self.assertAlmostEqual(x[t], m.x[t].value) else: x[t] = m.x[t].value for t in m.p_time_points: p[t] = m.p[t].value m = build_time_block(t0=t0, delta_t=delta_t, num_finite_elements=num_finite_elements, constant_control_duration=constant_control_duration, time_scale=time_scale) opt = pe.SolverFactory('ipopt') res = opt.solve(m) assert_optimal_termination(res) for _t, _x in x.items(): self.assertAlmostEqual(_x, m.x[_t].value) for _t, _p in p.items(): self.assertAlmostEqual(_p, m.p[_t].value)
def solve_warehouse_location(m): opt = pyo.SolverFactory('gurobi') res = opt.solve(m) assert_optimal_termination(res)