1919import runtime .db as db
2020import runtime .verifier as verifier
2121import six
22- from pyomo .environ import (Binary , Integers , NegativeIntegers , NegativeReals ,
23- NonNegativeIntegers , NonNegativeReals ,
24- NonPositiveIntegers , NonPositiveReals ,
25- PositiveIntegers , PositiveReals , Reals , maximize ,
26- minimize )
2722from runtime .optimize .model_generation import (
28- generate_objective_and_constraint_expression ,
29- generate_unique_result_value_name )
23+ generate_objective_and_constraint_expr , generate_unique_result_value_name )
3024
3125# FIXME(sneaxiy): do not know why Pyomo requires that the data frame must be
3226# a global variable
@@ -47,25 +41,35 @@ def generate_model_with_data_frame(data_frame, variables, variable_type,
4741 result_value_name (str): the result value name to be optimized.
4842 objective (list[str]): the objective string token list.
4943 direction (str): "maximize" or "minimize".
50- constraints (dict): the constraint expression containing the token list and GROUP BY column name.
44+ constraints (dict): the constraint expression containing the token list
45+ and GROUP BY column name.
5146
5247 Returns:
5348 A Pyomo ConcreteModel.
5449 """
5550 direction = direction .lower ()
56- if direction not in ['maximize' , 'minimize' ]:
51+ if direction == 'maximize' :
52+ direction = pyomo_env .maximize
53+ elif direction == 'minimize' :
54+ direction = pyomo_env .minimize
55+ else :
5756 raise ValueError ("direction must be one of 'maximize' or 'minimize'" )
5857
58+ if not hasattr (pyomo_env , variable_type ):
59+ raise ValueError ("cannot find variable type %s" % variable_type )
60+
61+ variable_type = getattr (pyomo_env , variable_type )
62+
5963 model = pyomo_env .ConcreteModel ()
6064 var_num = len (data_frame )
61- model .x = pyomo_env .Var (list (range (var_num )), within = eval ( variable_type ) )
65+ model .x = pyomo_env .Var (list (range (var_num )), within = variable_type )
6266
6367 columns = data_frame .columns
6468
6569 variable_str = "model.x"
6670 data_str = "DATA_FRAME"
6771
68- obj_expr , c_exprs = generate_objective_and_constraint_expression (
72+ obj_expr , c_exprs = generate_objective_and_constraint_expr (
6973 columns = columns ,
7074 objective = objective ,
7175 constraints = constraints ,
@@ -79,21 +83,23 @@ def generate_model_with_data_frame(data_frame, variables, variable_type,
7983 global DATA_FRAME
8084 DATA_FRAME = data_frame
8185 obj_func = eval ("lambda model: %s" % obj_expr )
82- model .objective = pyomo_env .Objective (rule = obj_func ,
83- sense = eval (direction ))
86+ model .objective = pyomo_env .Objective (rule = obj_func , sense = direction )
8487
8588 for i , (expr , for_range , iter_vars ) in enumerate (c_exprs ):
8689 attr_name = "constraint_%d" % i
8790
8891 if for_range :
89- assert iter_vars , "for_range and iter_vars must be both non-empty"
92+ assert iter_vars , "for_range and iter_vars must be " \
93+ "both non-empty"
9094 setattr (model , attr_name , pyomo_env .ConstraintList ())
9195 constraint_list = getattr (model , attr_name )
92- add_constraint_str = "lambda model, constraint_list: [constraint_list.add(%s) for %s in %s]" % (
93- expr , "," .join (iter_vars ), for_range )
96+ template = "lambda model, constraint_list: [constraint_list.add(%s) for %s in %s]" # noqa: E501
97+ add_constraint_str = template % (expr , "," .join (iter_vars ),
98+ for_range )
9499 eval (add_constraint_str )(model , constraint_list )
95100 else :
96- assert not iter_vars , "for_range and iter_vars must be both empty"
101+ assert not iter_vars , \
102+ "for_range and iter_vars must be both empty"
97103 func = eval ('lambda model: %s' % expr )
98104 constraint = pyomo_env .Constraint (rule = func )
99105 setattr (model , attr_name , constraint )
@@ -137,8 +143,8 @@ def solve_model(model, solver):
137143 if pyomo_dtype is None :
138144 pyomo_dtype = type (model .x [idx ])
139145
140- assert pyomo_dtype == type (
141- model . x [ idx ]), "all variables must be of the same data type"
146+ assert isinstance ( model . x [ idx ], pyomo_dtype ), \
147+ "all variables must be of the same data type"
142148
143149 if has_error :
144150 msg = 'Solve model error. Termination condition: {}.' \
@@ -187,7 +193,8 @@ def save_solved_result_in_db(solved_result, data_frame, variables,
187193 Save the solved result of the Pyomo model into the database.
188194
189195 Args:
190- solved_result (numpy.ndarray): a numpy array which indicates the solved result.
196+ solved_result (numpy.ndarray): a numpy array which indicates
197+ the solved result.
191198 data_frame (panda.DataFrame): the input table data.
192199 variables (list[str]): the variable names to be optimized.
193200 result_value_name (str): the result value name to be optimized.
@@ -244,7 +251,8 @@ def run_optimize_locally(datasource, select, variables, variable_type,
244251 result_value_name (str): the result value name to be optimized.
245252 objective (list[str]): the objective string token list.
246253 direction (str): "maximize" or "minimize".
247- constraints (dict): the constraint expression containing the token list and GROUP BY column name.
254+ constraints (dict): the constraint expression containing the token list
255+ and GROUP BY column name.
248256 solver (str): the solver used to solve the model.
249257 result_table (str): the table name to save the solved results.
250258
0 commit comments