Skip to content

Commit 599627e

Browse files
committed
Cleaned up tests
1 parent 5977754 commit 599627e

2 files changed

Lines changed: 24 additions & 90 deletions

File tree

pyomo/contrib/doe/doe.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252

5353
import pyomo.environ as pyo
5454
from pyomo.contrib.doe.utils import (
55+
_SMALL_TOLERANCE_DEFINITENESS,
5556
check_FIM,
5657
compute_FIM_metrics,
5758
regularize_fim_for_cholesky,

pyomo/contrib/doe/tests/test_greybox.py

Lines changed: 23 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def make_greybox_and_doe_objects_rooney_biegler(objective_option):
426426
cyipopt_call_working = not (
427427
bad_message in doe_object.results["Termination Message"]
428428
)
429-
except:
429+
except Exception:
430430
cyipopt_call_working = False
431431

432432

@@ -942,35 +942,17 @@ def test_A_opt_greybox_build(self):
942942
# on the DoE object
943943
doe_obj.create_grey_box_objective_function()
944944

945-
# Check to see if each component exists
946-
all_exist = True
947-
948945
# Check output and value
949946
# FIM Initial will be the prior FIM
950947
# added with the identity matrix.
951948
A_opt_val = np.trace(np.linalg.inv(testing_matrix + np.eye(4)))
952-
953-
try:
954-
A_opt_val_gb = doe_obj.model.obj_cons.egb_fim_block.outputs["A-opt"].value
955-
except:
956-
A_opt_val_gb = -10.0 # Trace should never be negative
957-
all_exist = False
958-
959-
# Intermediate check for output existence
960-
self.assertTrue(all_exist)
949+
A_opt_val_gb = doe_obj.model.obj_cons.egb_fim_block.outputs["A-opt"].value
961950
self.assertAlmostEqual(A_opt_val, A_opt_val_gb)
962951

963952
# Check inputs and values
964-
try:
965-
input_values = []
966-
for i in _.input_names():
967-
input_values.append(doe_obj.model.obj_cons.egb_fim_block.inputs[i]())
968-
except:
969-
input_values = np.zeros_like(testing_matrix)
970-
all_exist = False
971-
972-
# Final check on existence of inputs
973-
self.assertTrue(all_exist)
953+
input_values = []
954+
for i in grey_box_object.input_names():
955+
input_values.append(doe_obj.model.obj_cons.egb_fim_block.inputs[i]())
974956
# Rebuild the current FIM from the input
975957
# values taken from the egb_fim_block
976958
current_FIM = np.zeros_like(testing_matrix)
@@ -996,37 +978,17 @@ def test_D_opt_greybox_build(self):
996978
self.assertIsInstance(doe_obj.model.objective, pyo.Objective)
997979
self.assertEqual(doe_obj.model.objective.sense, pyo.maximize)
998980

999-
# Check to see if each component exists
1000-
all_exist = True
1001-
1002981
# Check output and value
1003982
# FIM Initial will be the prior FIM
1004983
# added with the identity matrix.
1005984
D_opt_val = np.log(np.linalg.det(testing_matrix + np.eye(4)))
1006-
1007-
try:
1008-
D_opt_val_gb = doe_obj.model.obj_cons.egb_fim_block.outputs[
1009-
"log-D-opt"
1010-
].value
1011-
except:
1012-
D_opt_val_gb = -100.0 # Determinant should never be negative beyond -64
1013-
all_exist = False
1014-
1015-
# Intermediate check for output existence
1016-
self.assertTrue(all_exist)
985+
D_opt_val_gb = doe_obj.model.obj_cons.egb_fim_block.outputs["log-D-opt"].value
1017986
self.assertAlmostEqual(D_opt_val, D_opt_val_gb)
1018987

1019988
# Check inputs and values
1020-
try:
1021-
input_values = []
1022-
for i in _.input_names():
1023-
input_values.append(doe_obj.model.obj_cons.egb_fim_block.inputs[i]())
1024-
except:
1025-
input_values = np.zeros_like(testing_matrix)
1026-
all_exist = False
1027-
1028-
# Final check on existence of inputs
1029-
self.assertTrue(all_exist)
989+
input_values = []
990+
for i in grey_box_object.input_names():
991+
input_values.append(doe_obj.model.obj_cons.egb_fim_block.inputs[i]())
1030992
# Rebuild the current FIM from the input
1031993
# values taken from the egb_fim_block
1032994
current_FIM = np.zeros_like(testing_matrix)
@@ -1038,7 +1000,9 @@ def test_D_opt_greybox_build(self):
10381000
def test_E_opt_greybox_build(self):
10391001
"""Validate E-opt grey-box block wiring and initialized values on DoE model."""
10401002
objective_option = "minimum_eigenvalue"
1041-
doe_obj, _ = make_greybox_and_doe_objects(objective_option=objective_option)
1003+
doe_obj, grey_box_object = make_greybox_and_doe_objects(
1004+
objective_option=objective_option
1005+
)
10421006

10431007
# Build the greybox objective block
10441008
# on the DoE object
@@ -1052,28 +1016,13 @@ def test_E_opt_greybox_build(self):
10521016
# added with the identity matrix.
10531017
vals, vecs = np.linalg.eig(testing_matrix + np.eye(4))
10541018
E_opt_val = np.min(vals)
1055-
1056-
try:
1057-
E_opt_val_gb = doe_obj.model.obj_cons.egb_fim_block.outputs["E-opt"].value
1058-
except:
1059-
E_opt_val_gb = -10.0 # Determinant should never be negative
1060-
all_exist = False
1061-
1062-
# Intermediate check for output existence
1063-
self.assertTrue(all_exist)
1019+
E_opt_val_gb = doe_obj.model.obj_cons.egb_fim_block.outputs["E-opt"].value
10641020
self.assertAlmostEqual(E_opt_val, E_opt_val_gb)
10651021

10661022
# Check inputs and values
1067-
try:
1068-
input_values = []
1069-
for i in _.input_names():
1070-
input_values.append(doe_obj.model.obj_cons.egb_fim_block.inputs[i]())
1071-
except:
1072-
input_values = np.zeros_like(testing_matrix)
1073-
all_exist = False
1074-
1075-
# Final check on existence of inputs
1076-
self.assertTrue(all_exist)
1023+
input_values = []
1024+
for i in grey_box_object.input_names():
1025+
input_values.append(doe_obj.model.obj_cons.egb_fim_block.inputs[i]())
10771026
# Rebuild the current FIM from the input
10781027
# values taken from the egb_fim_block
10791028
current_FIM = np.zeros_like(testing_matrix)
@@ -1085,42 +1034,26 @@ def test_E_opt_greybox_build(self):
10851034
def test_ME_opt_greybox_build(self):
10861035
"""Validate ME-opt grey-box block wiring and initialized values on DoE model."""
10871036
objective_option = "condition_number"
1088-
doe_obj, _ = make_greybox_and_doe_objects(objective_option=objective_option)
1037+
doe_obj, grey_box_object = make_greybox_and_doe_objects(
1038+
objective_option=objective_option
1039+
)
10891040

10901041
# Build the greybox objective block
10911042
# on the DoE object
10921043
doe_obj.create_grey_box_objective_function()
10931044

1094-
# Check to see if each component exists
1095-
all_exist = True
1096-
10971045
# Check output and value
10981046
# FIM Initial will be the prior FIM
10991047
# added with the identity matrix.
11001048
vals, vecs = np.linalg.eig(testing_matrix + np.eye(4))
11011049
ME_opt_val = np.log(np.abs(np.max(vals) / np.min(vals)))
1102-
1103-
try:
1104-
ME_opt_val_gb = doe_obj.model.obj_cons.egb_fim_block.outputs["ME-opt"].value
1105-
except:
1106-
ME_opt_val_gb = -10.0 # Condition number should not be negative
1107-
all_exist = False
1108-
1109-
# Intermediate check for output existence
1110-
self.assertTrue(all_exist)
1050+
ME_opt_val_gb = doe_obj.model.obj_cons.egb_fim_block.outputs["ME-opt"].value
11111051
self.assertAlmostEqual(ME_opt_val, ME_opt_val_gb)
11121052

11131053
# Check inputs and values
1114-
try:
1115-
input_values = []
1116-
for i in _.input_names():
1117-
input_values.append(doe_obj.model.obj_cons.egb_fim_block.inputs[i]())
1118-
except:
1119-
input_values = np.zeros_like(testing_matrix)
1120-
all_exist = False
1121-
1122-
# Final check on existence of inputs
1123-
self.assertTrue(all_exist)
1054+
input_values = []
1055+
for i in grey_box_object.input_names():
1056+
input_values.append(doe_obj.model.obj_cons.egb_fim_block.inputs[i]())
11241057
# Rebuild the current FIM from the input
11251058
# values taken from the egb_fim_block
11261059
current_FIM = np.zeros_like(testing_matrix)

0 commit comments

Comments
 (0)