fix pdlp python
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Tests for ortools.pdlp.python.quadratic_program."""
|
||||
|
||||
from absl.testing import absltest
|
||||
@@ -32,21 +33,19 @@ def small_proto_lp():
|
||||
maximize=False,
|
||||
objective_offset=0.0,
|
||||
variable=[
|
||||
linear_solver_pb2.MPVariableProto(lower_bound=0,
|
||||
upper_bound=np.inf,
|
||||
objective_coefficient=0,
|
||||
name='x'),
|
||||
linear_solver_pb2.MPVariableProto(lower_bound=0,
|
||||
upper_bound=np.inf,
|
||||
objective_coefficient=-2,
|
||||
name='y')
|
||||
linear_solver_pb2.MPVariableProto(
|
||||
lower_bound=0, upper_bound=np.inf, objective_coefficient=0, name="x"
|
||||
),
|
||||
linear_solver_pb2.MPVariableProto(
|
||||
lower_bound=0, upper_bound=np.inf, objective_coefficient=-2, name="y"
|
||||
),
|
||||
],
|
||||
constraint=[
|
||||
linear_solver_pb2.MPConstraintProto(var_index=[0, 1],
|
||||
coefficient=[1, 1],
|
||||
lower_bound=-np.inf,
|
||||
upper_bound=1)
|
||||
])
|
||||
linear_solver_pb2.MPConstraintProto(
|
||||
var_index=[0, 1], coefficient=[1, 1], lower_bound=-np.inf, upper_bound=1
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def small_proto_qp():
|
||||
@@ -58,27 +57,25 @@ def small_proto_qp():
|
||||
maximize=False,
|
||||
objective_offset=0.0,
|
||||
variable=[
|
||||
linear_solver_pb2.MPVariableProto(lower_bound=0,
|
||||
upper_bound=np.inf,
|
||||
objective_coefficient=0,
|
||||
name='x'),
|
||||
linear_solver_pb2.MPVariableProto(lower_bound=0,
|
||||
upper_bound=np.inf,
|
||||
objective_coefficient=0,
|
||||
name='y')
|
||||
linear_solver_pb2.MPVariableProto(
|
||||
lower_bound=0, upper_bound=np.inf, objective_coefficient=0, name="x"
|
||||
),
|
||||
linear_solver_pb2.MPVariableProto(
|
||||
lower_bound=0, upper_bound=np.inf, objective_coefficient=0, name="y"
|
||||
),
|
||||
],
|
||||
constraint=[
|
||||
linear_solver_pb2.MPConstraintProto(var_index=[0, 1],
|
||||
coefficient=[1, 1],
|
||||
lower_bound=-np.inf,
|
||||
upper_bound=1)
|
||||
linear_solver_pb2.MPConstraintProto(
|
||||
var_index=[0, 1], coefficient=[1, 1], lower_bound=-np.inf, upper_bound=1
|
||||
)
|
||||
],
|
||||
quadratic_objective=linear_solver_pb2.MPQuadraticObjective(
|
||||
qvar1_index=[0], qvar2_index=[0], coefficient=[2]))
|
||||
qvar1_index=[0], qvar2_index=[0], coefficient=[2]
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class QuadraticProgramTest(absltest.TestCase):
|
||||
|
||||
def test_validate_quadratic_program_dimensions_for_empty_qp(self):
|
||||
qp = pywrap_pdlp.QuadraticProgram()
|
||||
qp.resize_and_initialize(3, 2)
|
||||
@@ -87,16 +84,18 @@ class QuadraticProgramTest(absltest.TestCase):
|
||||
|
||||
def test_converts_from_tiny_mpmodel_lp(self):
|
||||
lp_proto = small_proto_lp()
|
||||
qp = pywrap_pdlp.qp_from_mpmodel_proto(lp_proto.SerializeToString(),
|
||||
relax_integer_variables=False)
|
||||
qp = pywrap_pdlp.qp_from_mpmodel_proto(
|
||||
lp_proto.SerializeToString(), relax_integer_variables=False
|
||||
)
|
||||
pywrap_pdlp.validate_quadratic_program_dimensions(qp)
|
||||
self.assertTrue(pywrap_pdlp.is_linear_program(qp))
|
||||
self.assertSameElements(qp.objective_vector, [0, -2])
|
||||
|
||||
def test_converts_from_tiny_mpmodel_qp(self):
|
||||
qp_proto = small_proto_qp()
|
||||
qp = pywrap_pdlp.qp_from_mpmodel_proto(qp_proto.SerializeToString(),
|
||||
relax_integer_variables=False)
|
||||
qp = pywrap_pdlp.qp_from_mpmodel_proto(
|
||||
qp_proto.SerializeToString(), relax_integer_variables=False
|
||||
)
|
||||
pywrap_pdlp.validate_quadratic_program_dimensions(qp)
|
||||
self.assertFalse(pywrap_pdlp.is_linear_program(qp))
|
||||
self.assertSameElements(qp.objective_vector, [0, 0])
|
||||
@@ -109,10 +108,13 @@ class QuadraticProgramTest(absltest.TestCase):
|
||||
qp.constraint_upper_bounds = [1.0]
|
||||
qp.variable_lower_bounds = [0.0, 0.0]
|
||||
qp.variable_upper_bounds = [np.inf, np.inf]
|
||||
qp.variable_names = ['x', 'y']
|
||||
qp.variable_names = ["x", "y"]
|
||||
self.assertEqual(
|
||||
linear_solver_pb2.MPModelProto.FromString(
|
||||
pywrap_pdlp.qp_to_mpmodel_proto(qp)), small_proto_lp())
|
||||
pywrap_pdlp.qp_to_mpmodel_proto(qp)
|
||||
),
|
||||
small_proto_lp(),
|
||||
)
|
||||
|
||||
def test_build_qp(self):
|
||||
qp = pywrap_pdlp.QuadraticProgram()
|
||||
@@ -123,30 +125,33 @@ class QuadraticProgramTest(absltest.TestCase):
|
||||
qp.constraint_upper_bounds = [1.0]
|
||||
qp.variable_lower_bounds = [0.0, 0.0]
|
||||
qp.variable_upper_bounds = [np.inf, np.inf]
|
||||
qp.variable_names = ['x', 'y']
|
||||
qp.variable_names = ["x", "y"]
|
||||
self.assertEqual(
|
||||
linear_solver_pb2.MPModelProto.FromString(
|
||||
pywrap_pdlp.qp_to_mpmodel_proto(qp)), small_proto_qp())
|
||||
pywrap_pdlp.qp_to_mpmodel_proto(qp)
|
||||
),
|
||||
small_proto_qp(),
|
||||
)
|
||||
|
||||
|
||||
def tiny_lp():
|
||||
"""Returns a small test LP.
|
||||
|
||||
The LP:
|
||||
min 5 x_1 + 2 x_2 + x_3 + x_4 - 14 s.t.
|
||||
2 x_1 + x_2 + x_3 + 2 x_4 = 12
|
||||
x_1 + x_3 >= 7
|
||||
x_3 - x_4 >= 1
|
||||
0 <= x_1 <= 2
|
||||
0 <= x_2 <= 4
|
||||
0 <= x_3 <= 6
|
||||
0 <= x_4 <= 3
|
||||
The LP:
|
||||
min 5 x_1 + 2 x_2 + x_3 + x_4 - 14 s.t.
|
||||
2 x_1 + x_2 + x_3 + 2 x_4 = 12
|
||||
x_1 + x_3 >= 7
|
||||
x_3 - x_4 >= 1
|
||||
0 <= x_1 <= 2
|
||||
0 <= x_2 <= 4
|
||||
0 <= x_3 <= 6
|
||||
0 <= x_4 <= 3
|
||||
|
||||
Optimum solutions:
|
||||
Primal: x_1 = 1, x_2 = 0, x_3 = 6, x_4 = 2. Value: 5 + 0 + 6 + 2 - 14 = -1.
|
||||
Dual: [0.5, 4.0, 0.0] Value: 6 + 28 - 3.5*6 - 14 = -1
|
||||
Reduced costs: [0.0, 1.5, -3.5, 0.0]
|
||||
"""
|
||||
Optimum solutions:
|
||||
Primal: x_1 = 1, x_2 = 0, x_3 = 6, x_4 = 2. Value: 5 + 0 + 6 + 2 - 14 = -1.
|
||||
Dual: [0.5, 4.0, 0.0] Value: 6 + 28 - 3.5*6 - 14 = -1
|
||||
Reduced costs: [0.0, 1.5, -3.5, 0.0]
|
||||
"""
|
||||
qp = pywrap_pdlp.QuadraticProgram()
|
||||
qp.objective_offset = -14
|
||||
qp.objective_vector = [5, 2, 1, 1]
|
||||
@@ -162,21 +167,21 @@ def tiny_lp():
|
||||
def test_lp():
|
||||
"""Returns a small LP with all 4 patterns lower and upper bounds.
|
||||
|
||||
min 5.5 x_0 - 2 x_1 - x_2 + x_3 - 14 s.t.
|
||||
2 x_0 + x_1 + x_2 + 2 x_3 = 12
|
||||
x_0 + x_2 <= 7
|
||||
4 x_0 >= -4
|
||||
-1 <= 1.5 x_2 - x_3 <= 1
|
||||
-infinity <= x_0 <= infinity
|
||||
-2 <= x_1 <= infinity
|
||||
-infinity <= x_2 <= 6
|
||||
2.5 <= x_3 <= 3.5
|
||||
min 5.5 x_0 - 2 x_1 - x_2 + x_3 - 14 s.t.
|
||||
2 x_0 + x_1 + x_2 + 2 x_3 = 12
|
||||
x_0 + x_2 <= 7
|
||||
4 x_0 >= -4
|
||||
-1 <= 1.5 x_2 - x_3 <= 1
|
||||
-infinity <= x_0 <= infinity
|
||||
-2 <= x_1 <= infinity
|
||||
-infinity <= x_2 <= 6
|
||||
2.5 <= x_3 <= 3.5
|
||||
|
||||
Optimal solutions:
|
||||
Primal: [-1, 8, 1, 2.5]
|
||||
Dual: [-2, 0, 2.375, 2.0/3]
|
||||
Value: -5.5 - 16 -1 + 2.5 - 14 = -34
|
||||
"""
|
||||
Optimal solutions:
|
||||
Primal: [-1, 8, 1, 2.5]
|
||||
Dual: [-2, 0, 2.375, 2.0/3]
|
||||
Value: -5.5 - 16 -1 + 2.5 - 14 = -34
|
||||
"""
|
||||
qp = pywrap_pdlp.QuadraticProgram()
|
||||
qp.objective_offset = -14
|
||||
qp.objective_vector = [5.5, -2, -1, 1]
|
||||
@@ -184,24 +189,27 @@ def test_lp():
|
||||
qp.constraint_upper_bounds = [12, 7, np.inf, 1]
|
||||
qp.variable_lower_bounds = [-np.inf, -2, -np.inf, 2.5]
|
||||
qp.variable_upper_bounds = [np.inf, np.inf, 6, 3.5]
|
||||
constraint_matrix = np.array([[2, 1, 1, 2], [1, 0, 1, 0], [4, 0, 0, 0],
|
||||
[0, 0, 1.5, -1]])
|
||||
constraint_matrix = np.array(
|
||||
[[2, 1, 1, 2], [1, 0, 1, 0], [4, 0, 0, 0], [0, 0, 1.5, -1]]
|
||||
)
|
||||
qp.constraint_matrix = scipy.sparse.csr_matrix(constraint_matrix)
|
||||
return qp
|
||||
|
||||
|
||||
class PrimalDualHybridGradientTest(absltest.TestCase):
|
||||
|
||||
def test_iteration_limit(self):
|
||||
params = solvers_pb2.PrimalDualHybridGradientParams()
|
||||
params.termination_criteria.iteration_limit = 1
|
||||
params.termination_check_frequency = 1
|
||||
result = pywrap_pdlp.primal_dual_hybrid_gradient(
|
||||
tiny_lp(), params.SerializeToString())
|
||||
tiny_lp(), params.SerializeToString()
|
||||
)
|
||||
solve_log = solve_log_pb2.SolveLog.FromString(result.solve_log_str)
|
||||
self.assertLessEqual(solve_log.iteration_count, 1)
|
||||
self.assertEqual(solve_log.termination_reason,
|
||||
solve_log_pb2.TERMINATION_REASON_ITERATION_LIMIT)
|
||||
self.assertEqual(
|
||||
solve_log.termination_reason,
|
||||
solve_log_pb2.TERMINATION_REASON_ITERATION_LIMIT,
|
||||
)
|
||||
|
||||
def test_solution(self):
|
||||
params = solvers_pb2.PrimalDualHybridGradientParams()
|
||||
@@ -209,15 +217,15 @@ class PrimalDualHybridGradientTest(absltest.TestCase):
|
||||
opt_criteria.eps_optimal_relative = 0.0
|
||||
opt_criteria.eps_optimal_absolute = 1.0e-10
|
||||
result = pywrap_pdlp.primal_dual_hybrid_gradient(
|
||||
tiny_lp(), params.SerializeToString())
|
||||
tiny_lp(), params.SerializeToString()
|
||||
)
|
||||
solve_log = solve_log_pb2.SolveLog.FromString(result.solve_log_str)
|
||||
self.assertEqual(solve_log.termination_reason,
|
||||
solve_log_pb2.TERMINATION_REASON_OPTIMAL)
|
||||
self.assertSequenceAlmostEqual(result.primal_solution,
|
||||
[1.0, 0.0, 6.0, 2.0])
|
||||
self.assertEqual(
|
||||
solve_log.termination_reason, solve_log_pb2.TERMINATION_REASON_OPTIMAL
|
||||
)
|
||||
self.assertSequenceAlmostEqual(result.primal_solution, [1.0, 0.0, 6.0, 2.0])
|
||||
self.assertSequenceAlmostEqual(result.dual_solution, [0.5, 4.0, 0.0])
|
||||
self.assertSequenceAlmostEqual(result.reduced_costs,
|
||||
[0.0, 1.5, -3.5, 0.0])
|
||||
self.assertSequenceAlmostEqual(result.reduced_costs, [0.0, 1.5, -3.5, 0.0])
|
||||
|
||||
def test_solution_2(self):
|
||||
params = solvers_pb2.PrimalDualHybridGradientParams()
|
||||
@@ -225,13 +233,14 @@ class PrimalDualHybridGradientTest(absltest.TestCase):
|
||||
opt_criteria.eps_optimal_relative = 0.0
|
||||
opt_criteria.eps_optimal_absolute = 1.0e-10
|
||||
result = pywrap_pdlp.primal_dual_hybrid_gradient(
|
||||
test_lp(), params.SerializeToString())
|
||||
test_lp(), params.SerializeToString()
|
||||
)
|
||||
solve_log = solve_log_pb2.SolveLog.FromString(result.solve_log_str)
|
||||
self.assertEqual(solve_log.termination_reason,
|
||||
solve_log_pb2.TERMINATION_REASON_OPTIMAL)
|
||||
self.assertEqual(
|
||||
solve_log.termination_reason, solve_log_pb2.TERMINATION_REASON_OPTIMAL
|
||||
)
|
||||
self.assertSequenceAlmostEqual(result.primal_solution, [-1, 8, 1, 2.5])
|
||||
self.assertSequenceAlmostEqual(result.dual_solution,
|
||||
[-2, 0, 2.375, 2 / 3])
|
||||
self.assertSequenceAlmostEqual(result.dual_solution, [-2, 0, 2.375, 2 / 3])
|
||||
|
||||
def test_starting_point(self):
|
||||
params = solvers_pb2.PrimalDualHybridGradientParams()
|
||||
@@ -245,12 +254,14 @@ class PrimalDualHybridGradientTest(absltest.TestCase):
|
||||
start.primal_solution = [1.0, 0.0, 6.0, 2.0]
|
||||
start.dual_solution = [0.5, 4.0, 0.0]
|
||||
result = pywrap_pdlp.primal_dual_hybrid_gradient(
|
||||
tiny_lp(), params.SerializeToString(), initial_solution=start)
|
||||
tiny_lp(), params.SerializeToString(), initial_solution=start
|
||||
)
|
||||
solve_log = solve_log_pb2.SolveLog.FromString(result.solve_log_str)
|
||||
self.assertEqual(solve_log.termination_reason,
|
||||
solve_log_pb2.TERMINATION_REASON_OPTIMAL)
|
||||
self.assertEqual(
|
||||
solve_log.termination_reason, solve_log_pb2.TERMINATION_REASON_OPTIMAL
|
||||
)
|
||||
self.assertEqual(solve_log.iteration_count, 0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
absltest.main()
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Solves a simple LP using PDLP's direct Python API.
|
||||
|
||||
Note: The direct API is generally for advanced use cases. It is matrix-based,
|
||||
@@ -25,22 +26,22 @@ import scipy.sparse
|
||||
from ortools.pdlp import solve_log_pb2
|
||||
from ortools.pdlp import solvers_pb2
|
||||
from ortools.pdlp.python import pywrap_pdlp
|
||||
from ortools.init import pywrapinit
|
||||
from ortools.init.python import pywrapinit
|
||||
|
||||
|
||||
def simple_lp() -> pywrap_pdlp.QuadraticProgram:
|
||||
"""Returns a small LP.
|
||||
|
||||
min 5.5 x_0 - 2 x_1 - x_2 + x_3 - 14 s.t.
|
||||
2 x_0 + x_1 + x_2 + 2 x_3 = 12
|
||||
x_0 + x_2 <= 7
|
||||
4 x_0 >= -4
|
||||
-1 <= 1.5 x_2 - x_3 <= 1
|
||||
-infinity <= x_0 <= infinity
|
||||
-2 <= x_1 <= infinity
|
||||
-infinity <= x_2 <= 6
|
||||
2.5 <= x_3 <= 3.5
|
||||
"""
|
||||
min 5.5 x_0 - 2 x_1 - x_2 + x_3 - 14 s.t.
|
||||
2 x_0 + x_1 + x_2 + 2 x_3 = 12
|
||||
x_0 + x_2 <= 7
|
||||
4 x_0 >= -4
|
||||
-1 <= 1.5 x_2 - x_3 <= 1
|
||||
-infinity <= x_0 <= infinity
|
||||
-2 <= x_1 <= infinity
|
||||
-infinity <= x_2 <= 6
|
||||
2.5 <= x_3 <= 3.5
|
||||
"""
|
||||
lp = pywrap_pdlp.QuadraticProgram()
|
||||
lp.objective_offset = -14
|
||||
lp.objective_vector = [5.5, -2, -1, 1]
|
||||
@@ -51,8 +52,9 @@ def simple_lp() -> pywrap_pdlp.QuadraticProgram:
|
||||
# Most use cases should initialize the sparse constraint matrix without
|
||||
# constructing a dense matrix first! We use a np.array here for convenience
|
||||
# only.
|
||||
constraint_matrix = np.array([[2, 1, 1, 2], [1, 0, 1, 0], [4, 0, 0, 0],
|
||||
[0, 0, 1.5, -1]])
|
||||
constraint_matrix = np.array(
|
||||
[[2, 1, 1, 2], [1, 0, 1, 0], [4, 0, 0, 0], [0, 0, 1.5, -1]]
|
||||
)
|
||||
lp.constraint_matrix = scipy.sparse.csc_matrix(constraint_matrix)
|
||||
return lp
|
||||
|
||||
@@ -71,40 +73,42 @@ def main() -> None:
|
||||
|
||||
# Call the main solve function. Note that a quirk of the pywrap11 API forces
|
||||
# us to serialize the `params` and deserialize the `solve_log` proto messages.
|
||||
result = pywrap_pdlp.primal_dual_hybrid_gradient(simple_lp(),
|
||||
params.SerializeToString())
|
||||
result = pywrap_pdlp.primal_dual_hybrid_gradient(
|
||||
simple_lp(), params.SerializeToString()
|
||||
)
|
||||
solve_log = solve_log_pb2.SolveLog.FromString(result.solve_log_str)
|
||||
|
||||
if solve_log.termination_reason == solve_log_pb2.TERMINATION_REASON_OPTIMAL:
|
||||
print('Solve successful')
|
||||
print("Solve successful")
|
||||
else:
|
||||
print(
|
||||
'Solve not successful. Status:',
|
||||
solve_log_pb2.TerminationReason.Name(solve_log.termination_reason))
|
||||
"Solve not successful. Status:",
|
||||
solve_log_pb2.TerminationReason.Name(solve_log.termination_reason),
|
||||
)
|
||||
|
||||
# Solutions vectors are always returned. *However*, their interpretation
|
||||
# depends on termination_reason! See primal_dual_hybrid_gradient.h for more
|
||||
# details on what the vectors mean if termination_reason is not
|
||||
# TERMINATION_REASON_OPTIMAL.
|
||||
print('Primal solution:', result.primal_solution)
|
||||
print('Dual solution:', result.dual_solution)
|
||||
print('Reduced costs:', result.reduced_costs)
|
||||
print("Primal solution:", result.primal_solution)
|
||||
print("Dual solution:", result.dual_solution)
|
||||
print("Reduced costs:", result.reduced_costs)
|
||||
|
||||
solution_type = solve_log.solution_type
|
||||
print('Solution type:', solve_log_pb2.PointType.Name(solution_type))
|
||||
print("Solution type:", solve_log_pb2.PointType.Name(solution_type))
|
||||
for ci in solve_log.solution_stats.convergence_information:
|
||||
if ci.candidate_type == solution_type:
|
||||
print('Primal objective:', ci.primal_objective)
|
||||
print('Dual objective:', ci.dual_objective)
|
||||
print("Primal objective:", ci.primal_objective)
|
||||
print("Dual objective:", ci.dual_objective)
|
||||
|
||||
print('Iterations:', solve_log.iteration_count)
|
||||
print('Solve time (sec):', solve_log.solve_time_sec)
|
||||
print("Iterations:", solve_log.iteration_count)
|
||||
print("Solve time (sec):", solve_log.solve_time_sec)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pywrapinit.CppBridge.InitLogging('simple_pdlp_program.py')
|
||||
if __name__ == "__main__":
|
||||
pywrapinit.CppBridge.init_logging("simple_pdlp_program.py")
|
||||
cpp_flags = pywrapinit.CppFlags()
|
||||
cpp_flags.logtostderr = True
|
||||
cpp_flags.stderrthreshold = 0
|
||||
cpp_flags.log_prefix = False
|
||||
pywrapinit.CppBridge.SetFlags(cpp_flags)
|
||||
pywrapinit.CppBridge.set_flags(cpp_flags)
|
||||
main()
|
||||
|
||||
@@ -170,12 +170,12 @@ class Sharder {
|
||||
// comments on the first constructor.
|
||||
Sharder(const Sharder& other_sharder, int64_t num_elements);
|
||||
|
||||
// `Sharder` may be copied or moved.
|
||||
// `Sharder` may be moved, but not copied.
|
||||
// Moved-from objects may be in an invalid state. The only methods that may be
|
||||
// called on a moved-from object are the destructor or `operator=`.
|
||||
Sharder(const Sharder& other) = default;
|
||||
Sharder(const Sharder& other) = delete;
|
||||
Sharder& operator=(const Sharder& other) = delete;
|
||||
Sharder(Sharder&& other) = default;
|
||||
Sharder& operator=(const Sharder& other) = default;
|
||||
Sharder& operator=(Sharder&& other) = default;
|
||||
|
||||
int NumShards() const { return static_cast<int>(shard_starts_.size()) - 1; }
|
||||
|
||||
Reference in New Issue
Block a user