Skip to content

Commit

Permalink
modernize sol.value() to -> sol()
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdsharpe committed Jan 1, 2024
1 parent e090c59 commit 773e253
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_gpkit_style_solve():

sol = opti.solve()

assert sol.value(D) == pytest.approx(303.1, abs=0.1)
assert sol(D) == pytest.approx(303.1, abs=0.1)


def test_geometric_program_solve():
Expand Down Expand Up @@ -132,7 +132,7 @@ def test_geometric_program_solve():

sol = opti.solve()

assert sol.value(D) == pytest.approx(303.1, abs=0.1)
assert sol(D) == pytest.approx(303.1, abs=0.1)


def test_non_log_transformed_solve():
Expand Down Expand Up @@ -186,7 +186,7 @@ def test_non_log_transformed_solve():

sol = opti.solve()

assert sol.value(D) == pytest.approx(303.1, abs=0.1)
assert sol(D) == pytest.approx(303.1, abs=0.1)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def test_normal_problem():

sol = opti.solve()

print(f"Chord = {sol.value(chord)}")
print(f"Span = {sol.value(span)}")
print(f"Chord = {sol(chord)}")
print(f"Span = {sol(span)}")

assert sol.value(chord) == pytest.approx(0.2288630528244024)
assert sol.value(span) == pytest.approx(4.369425242121806)
assert sol(chord) == pytest.approx(0.2288630528244024)
assert sol(span) == pytest.approx(4.369425242121806)


def test_log_transformed_problem():
Expand All @@ -51,11 +51,11 @@ def test_log_transformed_problem():

sol = opti.solve()

print(f"Chord = {sol.value(chord)}")
print(f"Span = {sol.value(span)}")
print(f"Chord = {sol(chord)}")
print(f"Span = {sol(span)}")

assert sol.value(chord) == pytest.approx(0.2288630528244024)
assert sol.value(span) == pytest.approx(4.369425242121806)
assert sol(chord) == pytest.approx(0.2288630528244024)
assert sol(span) == pytest.approx(4.369425242121806)


def test_log_transformed_negativity_error():
Expand All @@ -81,11 +81,11 @@ def test_fixed_variable():

sol = opti.solve()

print(f"Chord = {sol.value(chord)}")
print(f"Span = {sol.value(span)}")
print(f"Chord = {sol(chord)}")
print(f"Span = {sol(span)}")

assert sol.value(chord) == pytest.approx(1)
assert sol.value(span) == pytest.approx(1)
assert sol(chord) == pytest.approx(1)
assert sol(span) == pytest.approx(1)


def test_fully_fixed_problem():
Expand All @@ -104,11 +104,11 @@ def test_fully_fixed_problem():

sol = opti.solve()

print(f"Chord = {sol.value(chord)}")
print(f"Span = {sol.value(span)}")
print(f"Chord = {sol(chord)}")
print(f"Span = {sol(span)}")

assert sol.value(chord) == pytest.approx(1)
assert sol.value(span) == pytest.approx(1)
assert sol(chord) == pytest.approx(1)
assert sol(span) == pytest.approx(1)


def test_overconstrained_fully_fixed_problem():
Expand All @@ -128,11 +128,11 @@ def test_overconstrained_fully_fixed_problem():

sol = opti.solve()

print(f"Chord = {sol.value(chord)}")
print(f"Span = {sol.value(span)}")
print(f"Chord = {sol(chord)}")
print(f"Span = {sol(span)}")

# assert sol.value(chord) == pytest.approx(1)
# assert sol.value(span) == pytest.approx(1)
# assert sol(chord) == pytest.approx(1)
# assert sol(span) == pytest.approx(1)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_bounds():

sol = opti.solve()

assert sol.value(x) == pytest.approx(3)
assert sol(x) == pytest.approx(3)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def my_callback(iter: int):
callback=my_callback
)

assert sol.value(potential_energy) == pytest.approx(626.462, abs=1e-3)
assert sol(potential_energy) == pytest.approx(626.462, abs=1e-3)

if plot:
plt.show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_bounds():
opti.minimize(x)
sol = opti.solve()

assert sol.value(x) == pytest.approx(7)
assert sol(x) == pytest.approx(7)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_rocket_control_problem(plot=False):

sol = opti.solve() # solve

assert sol.value(a_max) == pytest.approx(0.02181991952, rel=1e-3) # solved externally with Julia JuMP
assert sol(a_max) == pytest.approx(0.02181991952, rel=1e-3) # solved externally with Julia JuMP

if plot:
import matplotlib.pyplot as plt
Expand All @@ -84,7 +84,7 @@ def test_rocket_control_problem(plot=False):
fig, ax = plt.subplots(1, 1, figsize=(8, 6), dpi=200)
for i, val, lab in zip(np.arange(3), [x, v, a], ["$x$", "$v$", "$a$"]):
plt.subplot(3, 1, i + 1)
plt.plot(sol.value(val), label=lab)
plt.plot(sol(val), label=lab)
plt.xlabel(r"Time [s]")
plt.ylabel(lab)
plt.legend()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def test_rosenbrock_constrained(plot=False):
for r_value in r_values
]
fs = [
sol.value(f)
sol(f)
for sol in sols
]
duals = [
sol.value(dual) # Ensure the dual can be evaluated
sol(dual) # Ensure the dual can be evaluated
for sol in sols
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def test_opti_poorly_scaled_constraints(constraint_jacobian_condition_number=1e1
sol = opti.solve()

# Check
assert sol.value(x) == pytest.approx(0.9, abs=1e-4)
assert sol.value(y) == pytest.approx(0.81, abs=1e-4)
assert sol(x) == pytest.approx(0.9, abs=1e-4)
assert sol(y) == pytest.approx(0.81, abs=1e-4)


def test_opti_poorly_scaled_objective(objective_hessian_condition_number=1e10):
Expand All @@ -51,9 +51,9 @@ def test_opti_poorly_scaled_objective(objective_hessian_condition_number=1e10):
sol = opti.solve()

# Check
assert sol.value(x) == pytest.approx(0, abs=1e-2)
assert sol.value(y) == pytest.approx(0, abs=1e-2)
assert sol.value(f) == pytest.approx(0, abs=1e-4)
assert sol(x) == pytest.approx(0, abs=1e-2)
assert sol(y) == pytest.approx(0, abs=1e-2)
assert sol(f) == pytest.approx(0, abs=1e-4)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_2D_rosenbrock(): # 2-dimensional rosenbrock
sol = opti.solve()

for i in [x, y]:
assert sol.value(i) == pytest.approx(1, abs=1e-4)
assert sol(i) == pytest.approx(1, abs=1e-4)


def test_2D_rosenbrock_circle_constrained(): # 2-dimensional rosenbrock, constrained to be within the unit circle.
Expand All @@ -58,8 +58,8 @@ def test_2D_rosenbrock_circle_constrained(): # 2-dimensional rosenbrock, constr
sol = opti.solve()

# Check
assert sol.value(x) == pytest.approx(0.7864, abs=1e-4)
assert sol.value(y) == pytest.approx(0.6177, abs=1e-4)
assert sol(x) == pytest.approx(0.7864, abs=1e-4)
assert sol(y) == pytest.approx(0.6177, abs=1e-4)
# (Solution also given here: https://www.mathworks.com/help/optim/ug/example-nonlinear-constrained-minimization.html#brg0p3g-2 )


Expand All @@ -81,7 +81,7 @@ def test_ND_rosenbrock_constrained(N=10): # N-dimensional rosenbrock
sol = opti.solve() # solve

for i in range(N):
assert sol.value(x[i]) == pytest.approx(1, abs=1e-4)
assert sol(x[i]) == pytest.approx(1, abs=1e-4)


def test_2D_rosenbrock_frozen():
Expand All @@ -98,9 +98,9 @@ def test_2D_rosenbrock_frozen():
# Optimize
sol = opti.solve()

assert sol.value(x) == pytest.approx(0.161, abs=1e-3)
assert sol.value(y) == pytest.approx(0, abs=1e-3)
assert sol.value(f) == pytest.approx(0.771, abs=1e-3)
assert sol(x) == pytest.approx(0.161, abs=1e-3)
assert sol(y) == pytest.approx(0, abs=1e-3)
assert sol(f) == pytest.approx(0.771, abs=1e-3)


if __name__ == '__main__':
Expand Down
66 changes: 33 additions & 33 deletions aerosandbox/optimization/test_optimization/test_opti_save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_opti():
sol = opti.solve()

for i in [x, y]:
assert sol.value(i) == pytest.approx(1, abs=1e-4)
assert sol(i) == pytest.approx(1, abs=1e-4)


def test_save_opti(tmp_path):
Expand Down Expand Up @@ -68,12 +68,12 @@ def test_save_and_load_opti(tmp_path):
sol = opti.solve()
opti.save_solution()
for i in ["x", "y", "f"]:
print(f"{i}: {sol.value(eval(i))}")
print(f"{i}: {sol(eval(i))}")

# Test
assert sol.value(x) == pytest.approx(1)
assert sol.value(y) == pytest.approx(1)
assert sol.value(f) == pytest.approx(0)
assert sol(x) == pytest.approx(1)
assert sol(y) == pytest.approx(1)
assert sol(f) == pytest.approx(0)

### Round 2 optimization: Cat 1 is fixed from before; slightly different objective now
opti = asb.Opti(
Expand All @@ -90,12 +90,12 @@ def test_save_and_load_opti(tmp_path):
# Optimize, save to cache, print
sol = opti.solve()
for i in ["x", "y", "f"]:
print(f"{i}: {sol.value(eval(i))}")
print(f"{i}: {sol(eval(i))}")

# Test
assert sol.value(x) == pytest.approx(1)
assert sol.value(y) == pytest.approx(2)
assert sol.value(f) == pytest.approx(1)
assert sol(x) == pytest.approx(1)
assert sol(y) == pytest.approx(2)
assert sol(f) == pytest.approx(1)


def test_save_and_load_opti_uncategorized(tmp_path):
Expand All @@ -116,12 +116,12 @@ def test_save_and_load_opti_uncategorized(tmp_path):
sol = opti.solve()
opti.save_solution()
for i in ["x", "y", "f"]:
print(f"{i}: {sol.value(eval(i))}")
print(f"{i}: {sol(eval(i))}")

# Test
assert sol.value(x) == pytest.approx(1)
assert sol.value(y) == pytest.approx(1)
assert sol.value(f) == pytest.approx(0)
assert sol(x) == pytest.approx(1)
assert sol(y) == pytest.approx(1)
assert sol(f) == pytest.approx(0)

### Round 2 optimization: Cat 1 is fixed from before; slightly different objective now
opti = asb.Opti(
Expand All @@ -138,12 +138,12 @@ def test_save_and_load_opti_uncategorized(tmp_path):
# Optimize, save to cache, print
sol = opti.solve()
for i in ["x", "y", "f"]:
print(f"{i}: {sol.value(eval(i))}")
print(f"{i}: {sol(eval(i))}")

# Test
assert sol.value(x) == pytest.approx(1)
assert sol.value(y) == pytest.approx(1)
assert sol.value(f) == pytest.approx(2)
assert sol(x) == pytest.approx(1)
assert sol(y) == pytest.approx(1)
assert sol(f) == pytest.approx(2)


def test_save_and_load_opti_vectorized(tmp_path):
Expand All @@ -165,12 +165,12 @@ def test_save_and_load_opti_vectorized(tmp_path):
sol = opti.solve()
opti.save_solution()
for i in ["x", "y", "f"]:
print(f"{i}: {sol.value(eval(i))}")
print(f"{i}: {sol(eval(i))}")

# Test
assert sol.value(x) == pytest.approx(1)
assert sol.value(y) == pytest.approx(2)
assert sol.value(f) == pytest.approx(0)
assert sol(x) == pytest.approx(1)
assert sol(y) == pytest.approx(2)
assert sol(f) == pytest.approx(0)

### Round 2 optimization: Cat 1 is fixed from before; slightly different objective now
opti = asb.Opti(
Expand All @@ -187,12 +187,12 @@ def test_save_and_load_opti_vectorized(tmp_path):
# Optimize, save to cache, print
sol = opti.solve()
for i in ["x", "y", "f"]:
print(f"{i}: {sol.value(eval(i))}")
print(f"{i}: {sol(eval(i))}")

# Test
assert sol.value(x) == pytest.approx(1)
assert sol.value(y) == pytest.approx(4)
assert sol.value(f) == pytest.approx(12)
assert sol(x) == pytest.approx(1)
assert sol(y) == pytest.approx(4)
assert sol(f) == pytest.approx(12)


def test_save_and_load_opti_freeze_override(tmp_path):
Expand All @@ -214,12 +214,12 @@ def test_save_and_load_opti_freeze_override(tmp_path):
sol = opti.solve()
opti.save_solution()
for i in ["x", "y", "f"]:
print(f"{i}: {sol.value(eval(i))}")
print(f"{i}: {sol(eval(i))}")

# Test
assert sol.value(x) == pytest.approx(1)
assert sol.value(y) == pytest.approx(1)
assert sol.value(f) == pytest.approx(0)
assert sol(x) == pytest.approx(1)
assert sol(y) == pytest.approx(1)
assert sol(f) == pytest.approx(0)

### Round 2 optimization: Cat 1 is fixed from before but then overridden; slightly different objective now
opti = asb.Opti(
Expand All @@ -236,12 +236,12 @@ def test_save_and_load_opti_freeze_override(tmp_path):
# Optimize, save to cache, print
sol = opti.solve()
for i in ["x", "y", "f"]:
print(f"{i}: {sol.value(eval(i))}")
print(f"{i}: {sol(eval(i))}")

# Test
assert sol.value(x) == pytest.approx(3)
assert sol.value(y) == pytest.approx(2)
assert sol.value(f) == pytest.approx(1)
assert sol(x) == pytest.approx(3)
assert sol(y) == pytest.approx(2)
assert sol(f) == pytest.approx(1)


if __name__ == '__main__':
Expand Down

0 comments on commit 773e253

Please sign in to comment.