@@ -118,7 +118,9 @@ def solve_model(model, solver):
118118 solver (str): the solver used to solve the model.
119119
120120 Returns:
121- A numpy array which is the solved result of the model.
121+ A tuple of (np.ndarray, float), where the numpy array is
122+ the solved x of the model and the float value is the solved
123+ objective function value of the model.
122124
123125 Raises:
124126 ValueError if the solving process fails.
@@ -152,7 +154,9 @@ def solve_model(model, solver):
152154 raise ValueError (msg )
153155
154156 np_dtype = np .int64 if model .x [0 ].is_integer () else np .float64
155- return np .array (result_values , dtype = np_dtype )
157+ x = np .array (result_values , dtype = np_dtype )
158+ y = model .objective ()
159+ return x , y
156160
157161
158162def load_db_data_to_data_frame (datasource , select ):
@@ -193,8 +197,9 @@ def save_solved_result_in_db(solved_result, data_frame, variables,
193197 Save the solved result of the Pyomo model into the database.
194198
195199 Args:
196- solved_result (numpy.ndarray): a numpy array which indicates
197- the solved result.
200+ solved_result (tuple(numpy.ndarray, float)): a numpy array
201+ which indicates the solved x, and a float value which
202+ indicates the objective function value.
198203 data_frame (panda.DataFrame): the input table data.
199204 variables (list[str]): the variable names to be optimized.
200205 result_value_name (str): the result value name to be optimized.
@@ -223,7 +228,7 @@ def save_solved_result_in_db(solved_result, data_frame, variables,
223228 variables = variables )
224229
225230 column_names .append (result_value_name )
226- data_frame [result_value_name ] = solved_result
231+ data_frame [result_value_name ] = solved_result [ 0 ]
227232
228233 conn = db .connect_with_data_source (datasource )
229234 with db .buffered_db_writer (conn .driver , conn , result_table ,
@@ -235,6 +240,7 @@ def save_solved_result_in_db(solved_result, data_frame, variables,
235240 print ('Solved result is:' )
236241 print (data_frame )
237242 print ('Saved in {}.' .format (result_table ))
243+ print ('Objective value is {}' .format (solved_result [1 ]))
238244
239245
240246def run_optimize_locally (datasource , select , variables , variable_type ,
@@ -269,8 +275,8 @@ def run_optimize_locally(datasource, select, variables, variable_type,
269275 objective = objective ,
270276 direction = direction ,
271277 constraints = constraints )
272- solved_result = solve_model (model , solver )
273- save_solved_result_in_db (solved_result = solved_result ,
278+ solved_x , solved_y = solve_model (model , solver )
279+ save_solved_result_in_db (solved_result = [ solved_x , solved_y ] ,
274280 data_frame = data_frame ,
275281 variables = variables ,
276282 result_value_name = result_value_name ,
0 commit comments