@@ -518,6 +518,58 @@ def test_export(self, remove_names: Optional[bool]) -> None:
518518 ),
519519 )
520520
521+ def test_from_model_proto (self ) -> None :
522+ model_proto = model_pb2 .ModelProto (
523+ name = "test_model" ,
524+ variables = model_pb2 .VariablesProto (
525+ ids = [0 , 1 ],
526+ lower_bounds = [0.0 , 1.0 ],
527+ upper_bounds = [2.0 , 3.0 ],
528+ integers = [True , False ],
529+ names = ["x" , "y" ],
530+ ),
531+ linear_constraints = model_pb2 .LinearConstraintsProto (
532+ ids = [0 ],
533+ lower_bounds = [- 1.0 ],
534+ upper_bounds = [2.0 ],
535+ names = ["c" ],
536+ ),
537+ objective = model_pb2 .ObjectiveProto (
538+ maximize = True ,
539+ offset = 2.0 ,
540+ linear_coefficients = sparse_containers_pb2 .SparseDoubleVectorProto (
541+ ids = [1 ], values = [3.0 ]
542+ ),
543+ ),
544+ linear_constraint_matrix = sparse_containers_pb2 .SparseDoubleMatrixProto (
545+ row_ids = [0 , 0 ], column_ids = [0 , 1 ], coefficients = [1.0 , 2.0 ]
546+ ),
547+ )
548+ mod = model .Model .from_model_proto (model_proto )
549+ self .assertEqual (mod .name , "test_model" )
550+ self .assertEqual (mod .get_num_variables (), 2 )
551+ x = mod .get_variable (0 )
552+ y = mod .get_variable (1 )
553+ self .assertEqual (x .name , "x" )
554+ self .assertEqual (x .lower_bound , 0.0 )
555+ self .assertEqual (x .upper_bound , 2.0 )
556+ self .assertTrue (x .integer )
557+ self .assertEqual (y .name , "y" )
558+ self .assertEqual (y .lower_bound , 1.0 )
559+ self .assertEqual (y .upper_bound , 3.0 )
560+ self .assertFalse (y .integer )
561+ self .assertEqual (mod .get_num_linear_constraints (), 1 )
562+ c = mod .get_linear_constraint (0 )
563+ self .assertEqual (c .name , "c" )
564+ self .assertEqual (c .lower_bound , - 1.0 )
565+ self .assertEqual (c .upper_bound , 2.0 )
566+ self .assertEqual (c .get_coefficient (x ), 1.0 )
567+ self .assertEqual (c .get_coefficient (y ), 2.0 )
568+ self .assertTrue (mod .objective .is_maximize )
569+ self .assertEqual (mod .objective .offset , 2.0 )
570+ self .assertEqual (mod .objective .get_linear_coefficient (x ), 0.0 )
571+ self .assertEqual (mod .objective .get_linear_coefficient (y ), 3.0 )
572+
521573 def test_update_tracker_simple (self ) -> None :
522574 mod = model .Model (name = "test_model" )
523575 x = mod .add_binary_variable (name = "x" )
0 commit comments