Skip to content

Commit 28cace1

Browse files
authored
add solved y to optimize (#2810)
1 parent feade33 commit 28cace1

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

python/runtime/optimize/local.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

158162
def 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

240246
def 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,

python/runtime/optimize/model_generation_test.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,10 @@ def test_model_generation(self):
256256
direction="maximize",
257257
constraints=constraints)
258258
self.assertTrue(isinstance(model1, pyomo_env.ConcreteModel))
259-
result = solve_model(model1, 'glpk')
259+
result_x, result_y = solve_model(model1, 'glpk')
260260
self.assertTrue(
261-
np.array_equal(result, np.array([20, 60], dtype='int64')))
261+
np.array_equal(result_x, np.array([20, 60], dtype='int64')))
262+
self.assertEqual(result_y, 180)
262263

263264
model2 = generate_model_with_data_frame(
264265
data_frame=self.data_frame,
@@ -355,9 +356,10 @@ def test_main(self):
355356
constraints=constraints)
356357
self.assertTrue(isinstance(model, pyomo_env.ConcreteModel))
357358

358-
result = solve_model(model, 'glpk')
359+
result_x, result_y = solve_model(model, 'glpk')
359360
self.assertTrue(
360-
np.array_equal(result, np.array([99, 1, 31, 59], dtype='int64')))
361+
np.array_equal(result_x, np.array([99, 1, 31, 59], dtype='int64')))
362+
self.assertAlmostEqual(result_y, 2581.2)
361363

362364

363365
if __name__ == '__main__':

0 commit comments

Comments
 (0)