Advanced Water Rocket Optimization Examples¶
This notebook demonstrates advanced usage of the water rocket optimizer including:
- Custom parameter bounds
- Different optimization targets (altitude, velocity, flight time)
- Custom optimizer settings
- Comparison of optimization strategies
In [1]:
Copied!
import time
import numpy as np
from waterrocketpy.optimization.water_rocket_optimizer import (
WaterRocketOptimizer,
optimize_for_altitude,
optimize_for_velocity,
optimize_for_flight_time,
)
import time
import numpy as np
from waterrocketpy.optimization.water_rocket_optimizer import (
WaterRocketOptimizer,
optimize_for_altitude,
optimize_for_velocity,
optimize_for_flight_time,
)
Helper Function for Results Display¶
In [2]:
Copied!
def print_results(result, test_name):
"""Print optimization results in a nice format."""
print(f"\n{'='*60}")
print(f"RESULTS FOR: {test_name}")
print(f"{'='*60}")
print(f"Success: {result['success']}")
print(f"Target: {result['target']}")
print(f"Best value: {result['best_value']:.4f}")
print(f"Evaluations: {result['n_evaluations']}")
print("\nOptimal Parameters:")
for param, value in result["best_params"].items():
if param == "p_max_bar":
print(f" {param:20}: {value:8.2f} bar")
elif param in ["L_body", "d_body"]:
print(f" {param:20}: {value:8.4f} m ({value*100:6.2f} cm)")
elif param == "nozzle_diameter":
print(f" {param:20}: {value:8.4f} m ({value*1000:6.2f} mm)")
elif param == "water_fraction":
print(f" {param:20}: {value:8.4f} ({value*100:6.2f}%)")
else:
print(f" {param:20}: {value:8.4f}")
if result["best_flight_data"] is not None:
fd = result["best_flight_data"]
print("\nFlight Performance:")
print(f" Max Altitude : {fd.max_altitude:8.4f} m")
print(f" Max Velocity : {fd.max_velocity:8.4f} m/s")
print(f" Flight Time : {fd.flight_time:8.4f} s")
print(f" Water Depletion Time: {fd.water_depletion_time:8.4f} s")
def print_results(result, test_name):
"""Print optimization results in a nice format."""
print(f"\n{'='*60}")
print(f"RESULTS FOR: {test_name}")
print(f"{'='*60}")
print(f"Success: {result['success']}")
print(f"Target: {result['target']}")
print(f"Best value: {result['best_value']:.4f}")
print(f"Evaluations: {result['n_evaluations']}")
print("\nOptimal Parameters:")
for param, value in result["best_params"].items():
if param == "p_max_bar":
print(f" {param:20}: {value:8.2f} bar")
elif param in ["L_body", "d_body"]:
print(f" {param:20}: {value:8.4f} m ({value*100:6.2f} cm)")
elif param == "nozzle_diameter":
print(f" {param:20}: {value:8.4f} m ({value*1000:6.2f} mm)")
elif param == "water_fraction":
print(f" {param:20}: {value:8.4f} ({value*100:6.2f}%)")
else:
print(f" {param:20}: {value:8.4f}")
if result["best_flight_data"] is not None:
fd = result["best_flight_data"]
print("\nFlight Performance:")
print(f" Max Altitude : {fd.max_altitude:8.4f} m")
print(f" Max Velocity : {fd.max_velocity:8.4f} m/s")
print(f" Flight Time : {fd.flight_time:8.4f} s")
print(f" Water Depletion Time: {fd.water_depletion_time:8.4f} s")
1. Custom Bounds for Velocity Optimization¶
Define custom parameter bounds around typical bottle rocket dimensions and optimize for maximum velocity.
In [3]:
Copied!
print("Velocity optimization with custom bounds")
# Define tighter bounds around typical values
custom_bounds = [
(0.20, 0.30), # L_body: 20-30 cm
(0.085, 0.095), # d_body: 8.5-9.5 cm (around 2L bottle)
(7.0, 9.0), # p_max_bar: 7-9 bar
(0.009, 0.012), # nozzle_diameter: 9-12 mm
(0.25, 0.40), # water_fraction: 25-40%
]
print("Custom bounds:")
param_names = ['L_body (m)', 'd_body (m)', 'p_max (bar)', 'nozzle_dia (m)', 'water_frac']
for i, (param_name, (min_val, max_val)) in enumerate(zip(param_names, custom_bounds)):
print(f" {param_name:15}: [{min_val:6.3f}, {max_val:6.3f}]")
start_time = time.time()
result_velocity = optimize_for_velocity(
bounds=custom_bounds,
method="differential_evolution",
maxiter=25,
popsize=10,
seed=123,
)
elapsed_time = time.time() - start_time
print(f"\nOptimization completed in {elapsed_time:.2f} seconds")
print_results(result_velocity, "Custom Bounds Velocity Optimization")
print("Velocity optimization with custom bounds")
# Define tighter bounds around typical values
custom_bounds = [
(0.20, 0.30), # L_body: 20-30 cm
(0.085, 0.095), # d_body: 8.5-9.5 cm (around 2L bottle)
(7.0, 9.0), # p_max_bar: 7-9 bar
(0.009, 0.012), # nozzle_diameter: 9-12 mm
(0.25, 0.40), # water_fraction: 25-40%
]
print("Custom bounds:")
param_names = ['L_body (m)', 'd_body (m)', 'p_max (bar)', 'nozzle_dia (m)', 'water_frac']
for i, (param_name, (min_val, max_val)) in enumerate(zip(param_names, custom_bounds)):
print(f" {param_name:15}: [{min_val:6.3f}, {max_val:6.3f}]")
start_time = time.time()
result_velocity = optimize_for_velocity(
bounds=custom_bounds,
method="differential_evolution",
maxiter=25,
popsize=10,
seed=123,
)
elapsed_time = time.time() - start_time
print(f"\nOptimization completed in {elapsed_time:.2f} seconds")
print_results(result_velocity, "Custom Bounds Velocity Optimization")
Velocity optimization with custom bounds Custom bounds: L_body (m) : [ 0.200, 0.300] d_body (m) : [ 0.085, 0.095] p_max (bar) : [ 7.000, 9.000] nozzle_dia (m) : [ 0.009, 0.012] water_frac : [ 0.250, 0.400] Starting optimization for max_velocity using differential_evolution Parameter bounds: [(0.2, 0.3), (0.085, 0.095), (7.0, 9.0), (0.009, 0.012), (0.25, 0.4)] New best max_velocity: 43.1361 at evaluation 1 Params: L_body=0.220, d_body=0.088, p_max=7.2bar, nozzle_d=0.0109, water_frac=0.380 New best max_velocity: 43.8598 at evaluation 2 Params: L_body=0.205, d_body=0.089, p_max=7.5bar, nozzle_d=0.0108, water_frac=0.285
New best max_velocity: 44.9703 at evaluation 3 Params: L_body=0.223, d_body=0.093, p_max=8.8bar, nozzle_d=0.0091, water_frac=0.399 New best max_velocity: 45.1061 at evaluation 4 Params: L_body=0.247, d_body=0.090, p_max=8.0bar, nozzle_d=0.0101, water_frac=0.385 New best max_velocity: 47.0032 at evaluation 5 Params: L_body=0.290, d_body=0.090, p_max=7.9bar, nozzle_d=0.0109, water_frac=0.325
New best max_velocity: 47.9715 at evaluation 6 Params: L_body=0.273, d_body=0.085, p_max=8.4bar, nozzle_d=0.0113, water_frac=0.254
New best max_velocity: 48.1689 at evaluation 17 Params: L_body=0.279, d_body=0.092, p_max=8.7bar, nozzle_d=0.0105, water_frac=0.342
New best max_velocity: 48.5003 at evaluation 21 Params: L_body=0.252, d_body=0.086, p_max=8.8bar, nozzle_d=0.0116, water_frac=0.261
New best max_velocity: 49.2145 at evaluation 34 Params: L_body=0.294, d_body=0.092, p_max=8.9bar, nozzle_d=0.0111, water_frac=0.264
New best max_velocity: 49.2683 at evaluation 91 Params: L_body=0.286, d_body=0.090, p_max=8.7bar, nozzle_d=0.0114, water_frac=0.326
New best max_velocity: 49.3114 at evaluation 98 Params: L_body=0.292, d_body=0.094, p_max=8.7bar, nozzle_d=0.0115, water_frac=0.290 differential_evolution step 1: f(x)= -49.31138280857241
New best max_velocity: 49.6391 at evaluation 110 Params: L_body=0.298, d_body=0.089, p_max=8.6bar, nozzle_d=0.0120, water_frac=0.306
differential_evolution step 2: f(x)= -49.63908917289738
New best max_velocity: 49.7768 at evaluation 168 Params: L_body=0.283, d_body=0.091, p_max=8.9bar, nozzle_d=0.0120, water_frac=0.341
differential_evolution step 3: f(x)= -49.7768361037334 New best max_velocity: 50.1259 at evaluation 201 Params: L_body=0.299, d_body=0.093, p_max=8.9bar, nozzle_d=0.0120, water_frac=0.332
differential_evolution step 4: f(x)= -50.12586612133661
New best max_velocity: 50.1771 at evaluation 254 Params: L_body=0.295, d_body=0.086, p_max=8.9bar, nozzle_d=0.0117, water_frac=0.315
differential_evolution step 5: f(x)= -50.177091069850064
differential_evolution step 6: f(x)= -50.177091069850064
New best max_velocity: 50.2426 at evaluation 358 Params: L_body=0.297, d_body=0.085, p_max=8.8bar, nozzle_d=0.0119, water_frac=0.311
New best max_velocity: 50.2632 at evaluation 368 Params: L_body=0.299, d_body=0.089, p_max=8.9bar, nozzle_d=0.0119, water_frac=0.331
differential_evolution step 7: f(x)= -50.26317455348622
New best max_velocity: 50.3238 at evaluation 437 Params: L_body=0.299, d_body=0.086, p_max=8.9bar, nozzle_d=0.0118, water_frac=0.317
New best max_velocity: 50.3527 at evaluation 443 Params: L_body=0.296, d_body=0.086, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.343
differential_evolution step 8: f(x)= -50.35270295469536
differential_evolution step 9: f(x)= -50.35270295469536 Polishing solution with 'L-BFGS-B' New best max_velocity: 50.3527 at evaluation 502 Params: L_body=0.296, d_body=0.086, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.343
New best max_velocity: 50.3527 at evaluation 505 Params: L_body=0.296, d_body=0.086, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.343
New best max_velocity: 50.5614 at evaluation 513 Params: L_body=0.298, d_body=0.086, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.308 New best max_velocity: 50.5614 at evaluation 514 Params: L_body=0.298, d_body=0.086, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.308
New best max_velocity: 50.5614 at evaluation 517 Params: L_body=0.298, d_body=0.086, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.308 New best max_velocity: 50.6156 at evaluation 519 Params: L_body=0.300, d_body=0.085, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.301
New best max_velocity: 50.6156 at evaluation 520 Params: L_body=0.300, d_body=0.085, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.301
New best max_velocity: 50.6331 at evaluation 525 Params: L_body=0.300, d_body=0.085, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.305
New best max_velocity: 50.6331 at evaluation 528 Params: L_body=0.300, d_body=0.085, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.305
New best max_velocity: 50.6417 at evaluation 531 Params: L_body=0.300, d_body=0.085, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.308
New best max_velocity: 50.6417 at evaluation 536 Params: L_body=0.300, d_body=0.085, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.308 New best max_velocity: 50.6428 at evaluation 537 Params: L_body=0.300, d_body=0.085, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.309
New best max_velocity: 50.6428 at evaluation 542 Params: L_body=0.300, d_body=0.085, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.309 New best max_velocity: 50.6432 at evaluation 543 Params: L_body=0.300, d_body=0.085, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.311
New best max_velocity: 50.6433 at evaluation 549 Params: L_body=0.300, d_body=0.085, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.311
New best max_velocity: 50.6433 at evaluation 638 Params: L_body=0.300, d_body=0.085, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.311
New best max_velocity: 50.6433 at evaluation 644 Params: L_body=0.300, d_body=0.085, p_max=9.0bar, nozzle_d=0.0120, water_frac=0.311
Optimization completed! Best max_velocity: 50.6433 Best parameters: L_body: 0.3000 d_body: 0.0850 p_max_bar: 9.0000 nozzle_diameter: 0.0120 water_fraction: 0.3106 Optimization completed in 57.57 seconds ============================================================ RESULTS FOR: Custom Bounds Velocity Optimization ============================================================ Success: True Target: max_velocity Best value: 50.6433 Evaluations: 662 Optimal Parameters: L_body : 0.3000 m ( 30.00 cm) d_body : 0.0850 m ( 8.50 cm) p_max_bar : 9.00 bar nozzle_diameter : 0.0120 m ( 12.00 mm) water_fraction : 0.3106 ( 31.06%) Flight Performance: Max Altitude : 95.4715 m Max Velocity : 50.6433 m/s Flight Time : 8.9409 s Water Depletion Time: 0.1410 s
2. Advanced Custom Optimizer Settings¶
Use the WaterRocketOptimizer class directly for more control over simulation settings and optimization parameters.
In [4]:
Copied!
print("Advanced optimization with custom settings")
# Create optimizer with custom simulation settings
optimizer = WaterRocketOptimizer(
L_cone=0.08, # Fixed nose cone length
material_name="PET", # Fixed material
simulation_settings={
"max_time": 40, # Longer simulation time
"time_step": 0.01, # High precision
"solver": "RK45",
},
)
# Use default bounds but modify pressure range
bounds = optimizer.get_default_bounds()
bounds[2] = (5.0, 11.0) # Wider pressure range: 5-11 bar
print("Modified bounds (pressure range widened):")
bound_names = ['L_body', 'd_body', 'p_max_bar', 'nozzle_diameter', 'water_fraction']
for name, (min_val, max_val) in zip(bound_names, bounds):
print(f" {name:15}: [{min_val:6.3f}, {max_val:6.3f}]")
start_time = time.time()
result_advanced = optimizer.optimize(
bounds=bounds,
target="max_altitude",
method="differential_evolution",
maxiter=30,
popsize=12,
atol=1e-6, # Higher accuracy
seed=456,
)
elapsed_time = time.time() - start_time
print(f"\nOptimization completed in {elapsed_time:.2f} seconds")
print_results(result_advanced, "Advanced Custom Settings")
print("Advanced optimization with custom settings")
# Create optimizer with custom simulation settings
optimizer = WaterRocketOptimizer(
L_cone=0.08, # Fixed nose cone length
material_name="PET", # Fixed material
simulation_settings={
"max_time": 40, # Longer simulation time
"time_step": 0.01, # High precision
"solver": "RK45",
},
)
# Use default bounds but modify pressure range
bounds = optimizer.get_default_bounds()
bounds[2] = (5.0, 11.0) # Wider pressure range: 5-11 bar
print("Modified bounds (pressure range widened):")
bound_names = ['L_body', 'd_body', 'p_max_bar', 'nozzle_diameter', 'water_fraction']
for name, (min_val, max_val) in zip(bound_names, bounds):
print(f" {name:15}: [{min_val:6.3f}, {max_val:6.3f}]")
start_time = time.time()
result_advanced = optimizer.optimize(
bounds=bounds,
target="max_altitude",
method="differential_evolution",
maxiter=30,
popsize=12,
atol=1e-6, # Higher accuracy
seed=456,
)
elapsed_time = time.time() - start_time
print(f"\nOptimization completed in {elapsed_time:.2f} seconds")
print_results(result_advanced, "Advanced Custom Settings")
Advanced optimization with custom settings Modified bounds (pressure range widened): L_body : [ 0.100, 0.500] d_body : [ 0.050, 0.120] p_max_bar : [ 5.000, 11.000] nozzle_diameter: [ 0.005, 0.025] water_fraction : [ 0.100, 0.800] Starting optimization for max_altitude using differential_evolution Parameter bounds: [(0.1, 0.5), (0.05, 0.12), (5.0, 11.0), (0.005, 0.025), (0.1, 0.8)] New best max_altitude: 88.5795 at evaluation 1 Params: L_body=0.448, d_body=0.105, p_max=8.0bar, nozzle_d=0.0063, water_frac=0.309
New best max_altitude: 105.1120 at evaluation 4 Params: L_body=0.412, d_body=0.120, p_max=7.7bar, nozzle_d=0.0235, water_frac=0.284 New best max_altitude: 119.1762 at evaluation 6 Params: L_body=0.395, d_body=0.106, p_max=10.0bar, nozzle_d=0.0188, water_frac=0.288
New best max_altitude: 120.2927 at evaluation 31 Params: L_body=0.405, d_body=0.114, p_max=10.6bar, nozzle_d=0.0130, water_frac=0.226
New best max_altitude: 126.0110 at evaluation 102 Params: L_body=0.451, d_body=0.104, p_max=10.7bar, nozzle_d=0.0148, water_frac=0.257
differential_evolution step 1: f(x)= -126.01103420643302
New best max_altitude: 130.2868 at evaluation 145 Params: L_body=0.475, d_body=0.112, p_max=10.3bar, nozzle_d=0.0209, water_frac=0.323
New best max_altitude: 131.8183 at evaluation 151 Params: L_body=0.425, d_body=0.114, p_max=11.0bar, nozzle_d=0.0215, water_frac=0.308
differential_evolution step 2: f(x)= -131.8182601015532
New best max_altitude: 134.4667 at evaluation 204 Params: L_body=0.466, d_body=0.115, p_max=10.9bar, nozzle_d=0.0182, water_frac=0.317
differential_evolution step 3: f(x)= -134.46674875982038
differential_evolution step 4: f(x)= -134.46674875982038
differential_evolution step 5: f(x)= -134.46674875982038
differential_evolution step 6: f(x)= -134.46674875982038
New best max_altitude: 134.7840 at evaluation 451 Params: L_body=0.475, d_body=0.117, p_max=10.9bar, nozzle_d=0.0162, water_frac=0.309
New best max_altitude: 134.9157 at evaluation 455 Params: L_body=0.490, d_body=0.111, p_max=10.9bar, nozzle_d=0.0200, water_frac=0.287
differential_evolution step 7: f(x)= -134.9156928899953
New best max_altitude: 135.4686 at evaluation 492 Params: L_body=0.488, d_body=0.119, p_max=10.9bar, nozzle_d=0.0160, water_frac=0.305
New best max_altitude: 135.7309 at evaluation 497 Params: L_body=0.486, d_body=0.119, p_max=10.8bar, nozzle_d=0.0226, water_frac=0.270
New best max_altitude: 136.0299 at evaluation 520 Params: L_body=0.478, d_body=0.116, p_max=10.8bar, nozzle_d=0.0231, water_frac=0.324
differential_evolution step 8: f(x)= -136.02992687080754
New best max_altitude: 136.5369 at evaluation 557 Params: L_body=0.488, d_body=0.119, p_max=10.9bar, nozzle_d=0.0187, water_frac=0.305
differential_evolution step 9: f(x)= -136.53689268388354
New best max_altitude: 136.7114 at evaluation 649 Params: L_body=0.496, d_body=0.118, p_max=11.0bar, nozzle_d=0.0179, water_frac=0.278
differential_evolution step 10: f(x)= -136.71139397806107
New best max_altitude: 138.3182 at evaluation 709 Params: L_body=0.491, d_body=0.119, p_max=10.9bar, nozzle_d=0.0246, water_frac=0.314
differential_evolution step 11: f(x)= -138.31820052344364
differential_evolution step 12: f(x)= -138.31820052344364
New best max_altitude: 138.5136 at evaluation 799 Params: L_body=0.499, d_body=0.120, p_max=10.8bar, nozzle_d=0.0246, water_frac=0.332
differential_evolution step 13: f(x)= -138.5135749996961
differential_evolution step 14: f(x)= -138.5135749996961
differential_evolution step 15: f(x)= -138.5135749996961
differential_evolution step 16: f(x)= -138.5135749996961
New best max_altitude: 138.8521 at evaluation 1080 Params: L_body=0.493, d_body=0.119, p_max=11.0bar, nozzle_d=0.0231, water_frac=0.310 differential_evolution step 17: f(x)= -138.8521101467835 New best max_altitude: 138.9940 at evaluation 1081 Params: L_body=0.493, d_body=0.120, p_max=11.0bar, nozzle_d=0.0228, water_frac=0.320
differential_evolution step 18: f(x)= -138.9939743694284
differential_evolution step 19: f(x)= -138.9939743694284
differential_evolution step 20: f(x)= -138.9939743694284
differential_evolution step 21: f(x)= -138.9939743694284
differential_evolution step 22: f(x)= -138.9939743694284
differential_evolution step 23: f(x)= -138.9939743694284
New best max_altitude: 139.1643 at evaluation 1474 Params: L_body=0.497, d_body=0.120, p_max=10.9bar, nozzle_d=0.0247, water_frac=0.320
differential_evolution step 24: f(x)= -139.1642872135296
differential_evolution step 25: f(x)= -139.1642872135296
New best max_altitude: 139.4352 at evaluation 1600 Params: L_body=0.499, d_body=0.120, p_max=11.0bar, nozzle_d=0.0244, water_frac=0.316
differential_evolution step 26: f(x)= -139.4351797044659 Polishing solution with 'L-BFGS-B' New best max_altitude: 139.4352 at evaluation 1622 Params: L_body=0.499, d_body=0.120, p_max=11.0bar, nozzle_d=0.0244, water_frac=0.316
New best max_altitude: 139.4352 at evaluation 1623 Params: L_body=0.499, d_body=0.120, p_max=11.0bar, nozzle_d=0.0244, water_frac=0.316
New best max_altitude: 139.4402 at evaluation 1633 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0244, water_frac=0.320 New best max_altitude: 139.4402 at evaluation 1634 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0244, water_frac=0.320
New best max_altitude: 139.4402 at evaluation 1635 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0244, water_frac=0.320
New best max_altitude: 139.6431 at evaluation 1639 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.319
New best max_altitude: 139.6431 at evaluation 1642 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.319
New best max_altitude: 139.7929 at evaluation 1645 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.316
New best max_altitude: 139.7929 at evaluation 1648 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.316
New best max_altitude: 139.8723 at evaluation 1651 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.314
New best max_altitude: 139.8723 at evaluation 1656 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.314 New best max_altitude: 139.8821 at evaluation 1657 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.317
New best max_altitude: 139.8821 at evaluation 1662 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.317 New best max_altitude: 139.8856 at evaluation 1663 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.319
New best max_altitude: 139.8856 at evaluation 1668 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.319 New best max_altitude: 139.8856 at evaluation 1669 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.319
New best max_altitude: 139.8856 at evaluation 1699 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.319
New best max_altitude: 139.8856 at evaluation 1705 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.319
New best max_altitude: 139.8856 at evaluation 1717 Params: L_body=0.500, d_body=0.120, p_max=11.0bar, nozzle_d=0.0250, water_frac=0.319
Optimization completed! Best max_altitude: 139.8856 Best parameters: L_body: 0.5000 d_body: 0.1200 p_max_bar: 11.0000 nozzle_diameter: 0.0250 water_fraction: 0.3189 Optimization completed in 168.56 seconds ============================================================ RESULTS FOR: Advanced Custom Settings ============================================================ Success: True Target: max_altitude Best value: 139.8856 Evaluations: 1722 Optimal Parameters: L_body : 0.5000 m ( 50.00 cm) d_body : 0.1200 m ( 12.00 cm) p_max_bar : 11.00 bar nozzle_diameter : 0.0250 m ( 25.00 mm) water_fraction : 0.3189 ( 31.89%) Flight Performance: Max Altitude : 139.8856 m Max Velocity : 59.3378 m/s Flight Time : 10.7734 s Water Depletion Time: 0.0992 s
3. Flight Time Optimization with Minimize Method¶
Use the minimize method with a good initial guess for gradient-based optimization.
In [5]:
Copied!
print("Flight time optimization using minimize method")
# Starting point based on reasonable rocket dimensions
initial_guess = [0.25, 0.088, 8.0, 0.01, 0.3]
print("Initial guess:")
guess_names = ['L_body (m)', 'd_body (m)', 'p_max (bar)', 'nozzle_dia (m)', 'water_frac']
for name, value in zip(guess_names, initial_guess):
print(f" {name:15}: {value:8.4f}")
start_time = time.time()
result_minimize = optimize_for_flight_time(
method="minimize",
x0=initial_guess,
options={"maxiter": 50, "disp": True},
)
elapsed_time = time.time() - start_time
print(f"\nOptimization completed in {elapsed_time:.2f} seconds")
print_results(result_minimize, "Flight Time Optimization (Minimize)")
print("Flight time optimization using minimize method")
# Starting point based on reasonable rocket dimensions
initial_guess = [0.25, 0.088, 8.0, 0.01, 0.3]
print("Initial guess:")
guess_names = ['L_body (m)', 'd_body (m)', 'p_max (bar)', 'nozzle_dia (m)', 'water_frac']
for name, value in zip(guess_names, initial_guess):
print(f" {name:15}: {value:8.4f}")
start_time = time.time()
result_minimize = optimize_for_flight_time(
method="minimize",
x0=initial_guess,
options={"maxiter": 50, "disp": True},
)
elapsed_time = time.time() - start_time
print(f"\nOptimization completed in {elapsed_time:.2f} seconds")
print_results(result_minimize, "Flight Time Optimization (Minimize)")
Flight time optimization using minimize method Initial guess: L_body (m) : 0.2500 d_body (m) : 0.0880 p_max (bar) : 8.0000 nozzle_dia (m) : 0.0100 water_frac : 0.3000 Starting optimization for flight_time using minimize Parameter bounds: [(0.1, 0.5), (0.05, 0.12), (2.0, 12.0), (0.005, 0.025), (0.1, 0.8)] New best flight_time: 8.3092 at evaluation 1 Params: L_body=0.250, d_body=0.088, p_max=8.0bar, nozzle_d=0.0100, water_frac=0.300 New best flight_time: 8.3092 at evaluation 2 Params: L_body=0.250, d_body=0.088, p_max=8.0bar, nozzle_d=0.0100, water_frac=0.300
New best flight_time: 8.3092 at evaluation 3 Params: L_body=0.250, d_body=0.088, p_max=8.0bar, nozzle_d=0.0100, water_frac=0.300
New best flight_time: 20.0000 at evaluation 7 Params: L_body=0.500, d_body=0.120, p_max=8.0bar, nozzle_d=0.0050, water_frac=0.800
Optimization completed! Best flight_time: 20.0000 Best parameters: L_body: 0.5000 d_body: 0.1200 p_max_bar: 7.9980 nozzle_diameter: 0.0050 water_fraction: 0.8000 Optimization completed in 1.93 seconds ============================================================ RESULTS FOR: Flight Time Optimization (Minimize) ============================================================ Success: True Target: flight_time Best value: 20.0000 Evaluations: 12 Optimal Parameters: L_body : 0.5000 m ( 50.00 cm) d_body : 0.1200 m ( 12.00 cm) p_max_bar : 8.00 bar nozzle_diameter : 0.0050 m ( 5.00 mm) water_fraction : 0.8000 ( 80.00%) Flight Performance: Max Altitude : 0.0000 m Max Velocity : 0.0000 m/s Flight Time : 20.0000 s Water Depletion Time: 0.0000 s
4. Comparison of Different Optimization Targets¶
Compare optimization results for altitude, velocity, and flight time using identical bounds and settings.
In [6]:
Copied!
print("Comparing different optimization targets")
# Use the same bounds and settings for fair comparison
common_bounds = [
(0.15, 0.35), # L_body: 15-35 cm
(0.07, 0.10), # d_body: 7-10 cm
(6.0, 10.0), # p_max_bar: 6-10 bar
(0.008, 0.015), # nozzle_diameter: 8-15 mm
(0.25, 0.45), # water_fraction: 25-45%
]
common_settings = {
"method": "differential_evolution",
"maxiter": 20,
"popsize": 8,
"seed": 789,
}
print("Common bounds for all targets:")
for i, (name, (min_val, max_val)) in enumerate(zip(param_names, common_bounds)):
print(f" {name:15}: [{min_val:6.3f}, {max_val:6.3f}]")
results = {}
print("\nOptimizing for altitude...")
results["altitude"] = optimize_for_altitude(
bounds=common_bounds, **common_settings
)
print("Optimizing for velocity...")
results["velocity"] = optimize_for_velocity(
bounds=common_bounds, **common_settings
)
print("Optimizing for flight time...")
results["flight_time"] = optimize_for_flight_time(
bounds=common_bounds, **common_settings
)
print("Comparing different optimization targets")
# Use the same bounds and settings for fair comparison
common_bounds = [
(0.15, 0.35), # L_body: 15-35 cm
(0.07, 0.10), # d_body: 7-10 cm
(6.0, 10.0), # p_max_bar: 6-10 bar
(0.008, 0.015), # nozzle_diameter: 8-15 mm
(0.25, 0.45), # water_fraction: 25-45%
]
common_settings = {
"method": "differential_evolution",
"maxiter": 20,
"popsize": 8,
"seed": 789,
}
print("Common bounds for all targets:")
for i, (name, (min_val, max_val)) in enumerate(zip(param_names, common_bounds)):
print(f" {name:15}: [{min_val:6.3f}, {max_val:6.3f}]")
results = {}
print("\nOptimizing for altitude...")
results["altitude"] = optimize_for_altitude(
bounds=common_bounds, **common_settings
)
print("Optimizing for velocity...")
results["velocity"] = optimize_for_velocity(
bounds=common_bounds, **common_settings
)
print("Optimizing for flight time...")
results["flight_time"] = optimize_for_flight_time(
bounds=common_bounds, **common_settings
)
Comparing different optimization targets Common bounds for all targets: L_body (m) : [ 0.150, 0.350] d_body (m) : [ 0.070, 0.100] p_max (bar) : [ 6.000, 10.000] nozzle_dia (m) : [ 0.008, 0.015] water_frac : [ 0.250, 0.450] Optimizing for altitude... Starting optimization for max_altitude using differential_evolution Parameter bounds: [(0.15, 0.35), (0.07, 0.1), (6.0, 10.0), (0.008, 0.015), (0.25, 0.45)] New best max_altitude: 80.6665 at evaluation 1 Params: L_body=0.305, d_body=0.087, p_max=7.7bar, nozzle_d=0.0114, water_frac=0.438
New best max_altitude: 91.8359 at evaluation 6 Params: L_body=0.260, d_body=0.092, p_max=9.0bar, nozzle_d=0.0137, water_frac=0.300
New best max_altitude: 95.0675 at evaluation 10 Params: L_body=0.322, d_body=0.088, p_max=9.2bar, nozzle_d=0.0116, water_frac=0.434
New best max_altitude: 98.9362 at evaluation 13 Params: L_body=0.292, d_body=0.100, p_max=9.2bar, nozzle_d=0.0146, water_frac=0.267
New best max_altitude: 100.8080 at evaluation 24 Params: L_body=0.305, d_body=0.079, p_max=9.9bar, nozzle_d=0.0140, water_frac=0.319
New best max_altitude: 104.2058 at evaluation 46 Params: L_body=0.341, d_body=0.089, p_max=9.5bar, nozzle_d=0.0112, water_frac=0.283
New best max_altitude: 105.7046 at evaluation 80 Params: L_body=0.328, d_body=0.091, p_max=9.9bar, nozzle_d=0.0103, water_frac=0.279 differential_evolution step 1: f(x)= -105.70464913716332
New best max_altitude: 107.8668 at evaluation 107 Params: L_body=0.347, d_body=0.092, p_max=10.0bar, nozzle_d=0.0091, water_frac=0.297
differential_evolution step 2: f(x)= -107.86681984569766
New best max_altitude: 107.9415 at evaluation 129 Params: L_body=0.347, d_body=0.090, p_max=10.0bar, nozzle_d=0.0097, water_frac=0.302
New best max_altitude: 108.5182 at evaluation 148 Params: L_body=0.347, d_body=0.091, p_max=10.0bar, nozzle_d=0.0102, water_frac=0.309
differential_evolution step 3: f(x)= -108.51819773653989
New best max_altitude: 109.6017 at evaluation 172 Params: L_body=0.348, d_body=0.095, p_max=9.8bar, nozzle_d=0.0133, water_frac=0.291
differential_evolution step 4: f(x)= -109.60171466569778
New best max_altitude: 110.0063 at evaluation 240 Params: L_body=0.346, d_body=0.098, p_max=9.7bar, nozzle_d=0.0148, water_frac=0.325 differential_evolution step 5: f(x)= -110.00634827593872
New best max_altitude: 110.3884 at evaluation 265 Params: L_body=0.347, d_body=0.100, p_max=9.8bar, nozzle_d=0.0143, water_frac=0.278
differential_evolution step 6: f(x)= -110.38838657123794
differential_evolution step 7: f(x)= -110.38838657123794 New best max_altitude: 111.2281 at evaluation 321 Params: L_body=0.347, d_body=0.099, p_max=9.9bar, nozzle_d=0.0141, water_frac=0.331
differential_evolution step 8: f(x)= -111.22806454445373 New best max_altitude: 111.9194 at evaluation 361 Params: L_body=0.347, d_body=0.099, p_max=9.9bar, nozzle_d=0.0149, water_frac=0.324
differential_evolution step 9: f(x)= -111.91940968715096
differential_evolution step 10: f(x)= -111.91940968715096
New best max_altitude: 111.9496 at evaluation 459 Params: L_body=0.347, d_body=0.099, p_max=10.0bar, nozzle_d=0.0136, water_frac=0.340
differential_evolution step 11: f(x)= -111.94961521919137 New best max_altitude: 112.2448 at evaluation 481 Params: L_body=0.348, d_body=0.099, p_max=10.0bar, nozzle_d=0.0142, water_frac=0.340
differential_evolution step 12: f(x)= -112.24477330429282
New best max_altitude: 112.3127 at evaluation 536 Params: L_body=0.347, d_body=0.099, p_max=10.0bar, nozzle_d=0.0147, water_frac=0.309
differential_evolution step 13: f(x)= -112.31273656662886
New best max_altitude: 112.4148 at evaluation 577 Params: L_body=0.349, d_body=0.100, p_max=10.0bar, nozzle_d=0.0148, water_frac=0.347
differential_evolution step 14: f(x)= -112.4147906484272
differential_evolution step 15: f(x)= -112.4147906484272 New best max_altitude: 112.4185 at evaluation 641 Params: L_body=0.349, d_body=0.100, p_max=10.0bar, nozzle_d=0.0143, water_frac=0.328
differential_evolution step 16: f(x)= -112.4185362523242
differential_evolution step 17: f(x)= -112.4185362523242 Polishing solution with 'L-BFGS-B' New best max_altitude: 112.4185 at evaluation 722 Params: L_body=0.349, d_body=0.100, p_max=10.0bar, nozzle_d=0.0143, water_frac=0.328 New best max_altitude: 112.4185 at evaluation 723 Params: L_body=0.349, d_body=0.100, p_max=10.0bar, nozzle_d=0.0143, water_frac=0.328
New best max_altitude: 112.4185 at evaluation 725 Params: L_body=0.349, d_body=0.100, p_max=10.0bar, nozzle_d=0.0143, water_frac=0.328
New best max_altitude: 112.5554 at evaluation 733 Params: L_body=0.349, d_body=0.100, p_max=10.0bar, nozzle_d=0.0145, water_frac=0.311 New best max_altitude: 112.5554 at evaluation 734 Params: L_body=0.349, d_body=0.100, p_max=10.0bar, nozzle_d=0.0145, water_frac=0.311 New best max_altitude: 112.5554 at evaluation 735 Params: L_body=0.349, d_body=0.100, p_max=10.0bar, nozzle_d=0.0145, water_frac=0.311
New best max_altitude: 112.5554 at evaluation 737 Params: L_body=0.349, d_body=0.100, p_max=10.0bar, nozzle_d=0.0145, water_frac=0.311 New best max_altitude: 112.9007 at evaluation 739 Params: L_body=0.350, d_body=0.100, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.317
New best max_altitude: 112.9007 at evaluation 742 Params: L_body=0.350, d_body=0.100, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.317
New best max_altitude: 112.9323 at evaluation 745 Params: L_body=0.350, d_body=0.100, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.320
New best max_altitude: 112.9323 at evaluation 748 Params: L_body=0.350, d_body=0.100, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.320
New best max_altitude: 113.0651 at evaluation 751 Params: L_body=0.350, d_body=0.100, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.329
New best max_altitude: 113.0898 at evaluation 757 Params: L_body=0.350, d_body=0.100, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.326
New best max_altitude: 113.1028 at evaluation 763 Params: L_body=0.350, d_body=0.100, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.321
New best max_altitude: 113.1028 at evaluation 769 Params: L_body=0.350, d_body=0.100, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.320
New best max_altitude: 113.1028 at evaluation 774 Params: L_body=0.350, d_body=0.100, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.320
New best max_altitude: 113.1028 at evaluation 793 Params: L_body=0.350, d_body=0.100, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.320
New best max_altitude: 113.1028 at evaluation 811 Params: L_body=0.350, d_body=0.100, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.320
Optimization completed! Best max_altitude: 113.1028 Best parameters: L_body: 0.3500 d_body: 0.1000 p_max_bar: 10.0000 nozzle_diameter: 0.0150 water_fraction: 0.3204 Optimizing for velocity... Starting optimization for max_velocity using differential_evolution Parameter bounds: [(0.15, 0.35), (0.07, 0.1), (6.0, 10.0), (0.008, 0.015), (0.25, 0.45)] New best max_velocity: 44.6989 at evaluation 1 Params: L_body=0.305, d_body=0.087, p_max=7.7bar, nozzle_d=0.0114, water_frac=0.438
New best max_velocity: 50.3795 at evaluation 6 Params: L_body=0.260, d_body=0.092, p_max=9.0bar, nozzle_d=0.0137, water_frac=0.300
New best max_velocity: 51.4356 at evaluation 13 Params: L_body=0.292, d_body=0.100, p_max=9.2bar, nozzle_d=0.0146, water_frac=0.267
New best max_velocity: 53.3564 at evaluation 24 Params: L_body=0.305, d_body=0.079, p_max=9.9bar, nozzle_d=0.0140, water_frac=0.319
New best max_velocity: 53.5319 at evaluation 64 Params: L_body=0.327, d_body=0.086, p_max=9.8bar, nozzle_d=0.0140, water_frac=0.298
differential_evolution step 1: f(x)= -53.5319260722355 New best max_velocity: 53.7087 at evaluation 81 Params: L_body=0.344, d_body=0.091, p_max=9.8bar, nozzle_d=0.0141, water_frac=0.298
differential_evolution step 2: f(x)= -53.70870263261544
differential_evolution step 3: f(x)= -53.70870263261544
New best max_velocity: 53.7351 at evaluation 175 Params: L_body=0.334, d_body=0.071, p_max=9.8bar, nozzle_d=0.0138, water_frac=0.311
differential_evolution step 4: f(x)= -53.735086322818084
New best max_velocity: 54.1104 at evaluation 224 Params: L_body=0.344, d_body=0.072, p_max=9.9bar, nozzle_d=0.0149, water_frac=0.365
differential_evolution step 5: f(x)= -54.11037537655638
New best max_velocity: 54.1261 at evaluation 244 Params: L_body=0.347, d_body=0.070, p_max=9.8bar, nozzle_d=0.0149, water_frac=0.305
New best max_velocity: 54.3321 at evaluation 251 Params: L_body=0.345, d_body=0.072, p_max=9.9bar, nozzle_d=0.0149, water_frac=0.319
differential_evolution step 6: f(x)= -54.33205112740322
differential_evolution step 7: f(x)= -54.33205112740322
New best max_velocity: 54.3798 at evaluation 354 Params: L_body=0.337, d_body=0.073, p_max=10.0bar, nozzle_d=0.0149, water_frac=0.327
differential_evolution step 8: f(x)= -54.379776449977136
differential_evolution step 9: f(x)= -54.379776449977136
differential_evolution step 10: f(x)= -54.379776449977136
New best max_velocity: 54.4292 at evaluation 470 Params: L_body=0.345, d_body=0.071, p_max=9.9bar, nozzle_d=0.0150, water_frac=0.333
New best max_velocity: 54.4864 at evaluation 474 Params: L_body=0.345, d_body=0.072, p_max=10.0bar, nozzle_d=0.0149, water_frac=0.319
differential_evolution step 11: f(x)= -54.48642350526417 Polishing solution with 'L-BFGS-B' New best max_velocity: 54.4864 at evaluation 482 Params: L_body=0.345, d_body=0.072, p_max=10.0bar, nozzle_d=0.0149, water_frac=0.319
New best max_velocity: 54.4864 at evaluation 485 Params: L_body=0.345, d_body=0.072, p_max=10.0bar, nozzle_d=0.0149, water_frac=0.319
New best max_velocity: 54.5086 at evaluation 493 Params: L_body=0.345, d_body=0.074, p_max=10.0bar, nozzle_d=0.0149, water_frac=0.330 New best max_velocity: 54.5086 at evaluation 494 Params: L_body=0.345, d_body=0.074, p_max=10.0bar, nozzle_d=0.0149, water_frac=0.330
New best max_velocity: 54.5086 at evaluation 497 Params: L_body=0.345, d_body=0.074, p_max=10.0bar, nozzle_d=0.0149, water_frac=0.330
New best max_velocity: 54.6100 at evaluation 505 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.325 New best max_velocity: 54.6100 at evaluation 506 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.325
New best max_velocity: 54.6167 at evaluation 511 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.326
New best max_velocity: 54.6167 at evaluation 514 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.326
New best max_velocity: 54.6173 at evaluation 517 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.325
New best max_velocity: 54.6173 at evaluation 520 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.325
New best max_velocity: 54.6183 at evaluation 523 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.325 New best max_velocity: 54.6183 at evaluation 525 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.325
New best max_velocity: 54.6183 at evaluation 526 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.325
New best max_velocity: 54.6223 at evaluation 529 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.325 New best max_velocity: 54.6223 at evaluation 531 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.325
New best max_velocity: 54.6223 at evaluation 532 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.325
New best max_velocity: 54.6352 at evaluation 535 Params: L_body=0.350, d_body=0.074, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.322 New best max_velocity: 54.6352 at evaluation 537 Params: L_body=0.350, d_body=0.074, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.322
New best max_velocity: 54.6464 at evaluation 541 Params: L_body=0.350, d_body=0.074, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.320 New best max_velocity: 54.6464 at evaluation 543 Params: L_body=0.350, d_body=0.074, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.320
New best max_velocity: 54.6670 at evaluation 547 Params: L_body=0.350, d_body=0.074, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.316 New best max_velocity: 54.6670 at evaluation 549 Params: L_body=0.350, d_body=0.074, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.316
New best max_velocity: 54.6685 at evaluation 553 Params: L_body=0.350, d_body=0.074, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.316 New best max_velocity: 54.6685 at evaluation 555 Params: L_body=0.350, d_body=0.074, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.316
New best max_velocity: 54.6733 at evaluation 559 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.318 New best max_velocity: 54.6733 at evaluation 561 Params: L_body=0.350, d_body=0.075, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.318
New best max_velocity: 54.6777 at evaluation 571 Params: L_body=0.350, d_body=0.076, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.321 New best max_velocity: 54.6777 at evaluation 573 Params: L_body=0.350, d_body=0.076, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.321
New best max_velocity: 54.6777 at evaluation 613 Params: L_body=0.350, d_body=0.076, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.321
New best max_velocity: 54.6777 at evaluation 630 Params: L_body=0.350, d_body=0.076, p_max=10.0bar, nozzle_d=0.0150, water_frac=0.321
Optimization completed! Best max_velocity: 54.6777 Best parameters: L_body: 0.3500 d_body: 0.0762 p_max_bar: 10.0000 nozzle_diameter: 0.0150 water_fraction: 0.3209 Optimizing for flight time... Starting optimization for flight_time using differential_evolution Parameter bounds: [(0.15, 0.35), (0.07, 0.1), (6.0, 10.0), (0.008, 0.015), (0.25, 0.45)] New best flight_time: 8.3140 at evaluation 1 Params: L_body=0.305, d_body=0.087, p_max=7.7bar, nozzle_d=0.0114, water_frac=0.438
New best flight_time: 8.7497 at evaluation 6 Params: L_body=0.260, d_body=0.092, p_max=9.0bar, nozzle_d=0.0137, water_frac=0.300
New best flight_time: 8.9937 at evaluation 10 Params: L_body=0.322, d_body=0.088, p_max=9.2bar, nozzle_d=0.0116, water_frac=0.434
New best flight_time: 9.0815 at evaluation 13 Params: L_body=0.292, d_body=0.100, p_max=9.2bar, nozzle_d=0.0146, water_frac=0.267
New best flight_time: 9.1516 at evaluation 24 Params: L_body=0.305, d_body=0.079, p_max=9.9bar, nozzle_d=0.0140, water_frac=0.319
New best flight_time: 9.2383 at evaluation 38 Params: L_body=0.347, d_body=0.093, p_max=8.7bar, nozzle_d=0.0084, water_frac=0.315
New best flight_time: 9.3988 at evaluation 51 Params: L_body=0.325, d_body=0.096, p_max=9.8bar, nozzle_d=0.0107, water_frac=0.259
New best flight_time: 9.4720 at evaluation 58 Params: L_body=0.330, d_body=0.092, p_max=9.8bar, nozzle_d=0.0110, water_frac=0.304 New best flight_time: 9.4793 at evaluation 60 Params: L_body=0.334, d_body=0.089, p_max=9.8bar, nozzle_d=0.0112, water_frac=0.338
differential_evolution step 1: f(x)= -9.479256162499592
New best flight_time: 9.5915 at evaluation 118 Params: L_body=0.337, d_body=0.093, p_max=10.0bar, nozzle_d=0.0106, water_frac=0.339 differential_evolution step 2: f(x)= -9.591477251880296
New best flight_time: 9.6262 at evaluation 142 Params: L_body=0.348, d_body=0.097, p_max=9.9bar, nozzle_d=0.0135, water_frac=0.299
differential_evolution step 3: f(x)= -9.626215880630067 New best flight_time: 9.6626 at evaluation 161 Params: L_body=0.348, d_body=0.097, p_max=9.9bar, nozzle_d=0.0114, water_frac=0.339
differential_evolution step 4: f(x)= -9.66258342809863
differential_evolution step 5: f(x)= -9.66258342809863
New best flight_time: 9.6753 at evaluation 267 Params: L_body=0.350, d_body=0.100, p_max=9.8bar, nozzle_d=0.0116, water_frac=0.309
differential_evolution step 6: f(x)= -9.675343257234163
differential_evolution step 7: f(x)= -9.675343257234163
New best flight_time: 9.6835 at evaluation 334 Params: L_body=0.349, d_body=0.096, p_max=9.9bar, nozzle_d=0.0092, water_frac=0.346
differential_evolution step 8: f(x)= -9.683450084206491
New best flight_time: 9.6964 at evaluation 370 Params: L_body=0.347, d_body=0.097, p_max=9.9bar, nozzle_d=0.0091, water_frac=0.347
New best flight_time: 9.7203 at evaluation 375 Params: L_body=0.349, d_body=0.097, p_max=10.0bar, nozzle_d=0.0088, water_frac=0.315
differential_evolution step 9: f(x)= -9.720277174510226
New best flight_time: 9.7306 at evaluation 434 Params: L_body=0.348, d_body=0.097, p_max=10.0bar, nozzle_d=0.0089, water_frac=0.310
differential_evolution step 10: f(x)= -9.730596378797376
differential_evolution step 11: f(x)= -9.730596378797376 Polishing solution with 'L-BFGS-B' New best flight_time: 9.7306 at evaluation 482 Params: L_body=0.348, d_body=0.097, p_max=10.0bar, nozzle_d=0.0089, water_frac=0.310 New best flight_time: 9.7306 at evaluation 483 Params: L_body=0.348, d_body=0.097, p_max=10.0bar, nozzle_d=0.0089, water_frac=0.310
New best flight_time: 9.7428 at evaluation 493 Params: L_body=0.348, d_body=0.097, p_max=10.0bar, nozzle_d=0.0088, water_frac=0.323 New best flight_time: 9.7428 at evaluation 494 Params: L_body=0.348, d_body=0.097, p_max=10.0bar, nozzle_d=0.0088, water_frac=0.323 New best flight_time: 9.7428 at evaluation 495 Params: L_body=0.348, d_body=0.097, p_max=10.0bar, nozzle_d=0.0088, water_frac=0.323
New best flight_time: 9.7467 at evaluation 505 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.332 New best flight_time: 9.7467 at evaluation 506 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.332 New best flight_time: 9.7467 at evaluation 507 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.332
New best flight_time: 9.7477 at evaluation 511 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.340 New best flight_time: 9.7477 at evaluation 512 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.340 New best flight_time: 9.7477 at evaluation 513 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.340
New best flight_time: 9.7478 at evaluation 523 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7478 at evaluation 526 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7478 at evaluation 528 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7481 at evaluation 547 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7481 at evaluation 548 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7481 at evaluation 549 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7483 at evaluation 553 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7483 at evaluation 554 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7483 at evaluation 555 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7484 at evaluation 565 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7484 at evaluation 566 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7484 at evaluation 567 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7495 at evaluation 571 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7495 at evaluation 572 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7495 at evaluation 573 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7495 at evaluation 577 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7495 at evaluation 578 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7495 at evaluation 579 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7495 at evaluation 583 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7495 at evaluation 584 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7495 at evaluation 585 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7498 at evaluation 589 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 590 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 591 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7498 at evaluation 631 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 632 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 633 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7498 at evaluation 637 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 638 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 639 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7498 at evaluation 649 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 650 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 651 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7498 at evaluation 655 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 656 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 657 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7498 at evaluation 669 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7498 at evaluation 674 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 675 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7498 at evaluation 687 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7498 at evaluation 693 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7498 at evaluation 697 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 698 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344 New best flight_time: 9.7498 at evaluation 699 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
New best flight_time: 9.7498 at evaluation 785 Params: L_body=0.349, d_body=0.098, p_max=10.0bar, nozzle_d=0.0087, water_frac=0.344
Optimization completed! Best flight_time: 9.7498 Best parameters: L_body: 0.3487 d_body: 0.0979 p_max_bar: 9.9959 nozzle_diameter: 0.0087 water_fraction: 0.3442
Comparison Table¶
In [7]:
Copied!
# Print comparison table
print(f"\n{'='*80}")
print("COMPARISON OF DIFFERENT TARGETS")
print(f"{'='*80}")
print(
f"{'Target':<12} {'Best Value':<12} {'L_body(cm)':<10} {'d_body(cm)':<10} "
f"{'P(bar)':<8} {'Nozzle(mm)':<10} {'Water%':<8}"
)
print("-" * 80)
for target, result in results.items():
params = result["best_params"]
print(
f"{target:<12} {result['best_value']:<12.4f} "
f"{params['L_body']*100:<10.2f} {params['d_body']*100:<10.2f} "
f"{params['p_max_bar']:<8.2f} {params['nozzle_diameter']*1000:<10.2f} "
f"{params['water_fraction']*100:<8.1f}"
)
# Print comparison table
print(f"\n{'='*80}")
print("COMPARISON OF DIFFERENT TARGETS")
print(f"{'='*80}")
print(
f"{'Target':<12} {'Best Value':<12} {'L_body(cm)':<10} {'d_body(cm)':<10} "
f"{'P(bar)':<8} {'Nozzle(mm)':<10} {'Water%':<8}"
)
print("-" * 80)
for target, result in results.items():
params = result["best_params"]
print(
f"{target:<12} {result['best_value']:<12.4f} "
f"{params['L_body']*100:<10.2f} {params['d_body']*100:<10.2f} "
f"{params['p_max_bar']:<8.2f} {params['nozzle_diameter']*1000:<10.2f} "
f"{params['water_fraction']*100:<8.1f}"
)
================================================================================ COMPARISON OF DIFFERENT TARGETS ================================================================================ Target Best Value L_body(cm) d_body(cm) P(bar) Nozzle(mm) Water% -------------------------------------------------------------------------------- altitude 113.1028 35.00 10.00 10.00 15.00 32.0 velocity 54.6777 35.00 7.62 10.00 15.00 32.1 flight_time 9.7498 34.87 9.79 10.00 8.66 34.4
5. Performance Analysis¶
Analyze how each optimized configuration performs across all metrics.
In [8]:
Copied!
print(f"\n{'='*80}")
print("CROSS-PERFORMANCE ANALYSIS")
print(f"{'='*80}")
print(
f"{'Optimized For':<15} {'Altitude(m)':<12} {'Velocity(m/s)':<13} {'Flight Time(s)':<14}"
)
print("-" * 80)
for target, result in results.items():
if result["best_flight_data"] is not None:
fd = result["best_flight_data"]
print(
f"{target:<15} {fd.max_altitude:<12.4f} {fd.max_velocity:<13.4f} {fd.flight_time:<14.4f}"
)
print("\n📊 Key Observations:")
print("• Different targets lead to different optimal configurations")
print("• There are trade-offs between altitude, velocity, and flight time")
print("• Custom bounds help focus the search on realistic parameter ranges")
print("• The minimize method can be faster for gradient-based optimization")
print(f"\n{'='*80}")
print("CROSS-PERFORMANCE ANALYSIS")
print(f"{'='*80}")
print(
f"{'Optimized For':<15} {'Altitude(m)':<12} {'Velocity(m/s)':<13} {'Flight Time(s)':<14}"
)
print("-" * 80)
for target, result in results.items():
if result["best_flight_data"] is not None:
fd = result["best_flight_data"]
print(
f"{target:<15} {fd.max_altitude:<12.4f} {fd.max_velocity:<13.4f} {fd.flight_time:<14.4f}"
)
print("\n📊 Key Observations:")
print("• Different targets lead to different optimal configurations")
print("• There are trade-offs between altitude, velocity, and flight time")
print("• Custom bounds help focus the search on realistic parameter ranges")
print("• The minimize method can be faster for gradient-based optimization")
================================================================================ CROSS-PERFORMANCE ANALYSIS ================================================================================ Optimized For Altitude(m) Velocity(m/s) Flight Time(s) -------------------------------------------------------------------------------- altitude 113.1028 54.1999 9.7232 velocity 105.6734 54.6777 9.3610 flight_time 108.3814 47.9386 9.7498 📊 Key Observations: • Different targets lead to different optimal configurations • There are trade-offs between altitude, velocity, and flight time • Custom bounds help focus the search on realistic parameter ranges • The minimize method can be faster for gradient-based optimization
Summary¶
This notebook demonstrated advanced optimization techniques:
- Custom Bounds: Restrict parameter ranges to realistic values
- Custom Settings: Use the
WaterRocketOptimizerclass for fine control - Different Methods: Compare
differential_evolutionvsminimize - Multi-Objective Analysis: Compare different optimization targets
- Performance Trade-offs: Understand the relationships between parameters
Next Steps¶
- Experiment with different bound ranges
- Try different optimization algorithms
- Add constraints for specific design requirements
- Implement multi-objective optimization