sme_contrib.optimize
[1]:
!pip install -q sme_contrib
import sme
import sme_contrib.optimize as smeopt
import sme_contrib.plot as smeplot
import numpy as np
from matplotlib import pyplot as plt
from IPython.display import HTML
from PIL import Image
Gray-Scott model
A simple two-species model with two reaction rate parameters, that forms spatial patterns and eventually reaches a steady state
[2]:
def simulated_gray_scott(f, k):
m = sme.open_example_model("gray-scott")
m.compartments[0].species[
"V"
].analytic_concentration = "exp(-((x-49.5)^2+(y-49.5)^2))"
m.parameters["f"].value = f"{f}"
m.parameters["k"].value = f"{k}"
m.simulate(5000, 50, return_results=False)
return m
def gray_scott_anim(f, k):
gray_scott = simulated_gray_scott(f, k)
return smeplot.concentration_heatmap_animation(
gray_scott.simulation_results(), ["V"]
)
[3]:
anim = gray_scott_anim(0.04, 0.06)
HTML(anim.to_html5_video())
2026-04-27 09:47:14,834 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.FFMpegWriter'>
2026-04-27 09:47:14,835 - matplotlib.animation - INFO - MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 640x480 -pix_fmt rgba -framerate 5.0 -loglevel error -i pipe: -vcodec h264 -pix_fmt yuv420p -y /tmp/tmpwnkxmzuy/temp.m4v
[3]:
[4]:
anim = gray_scott_anim(0.051, 0.061)
HTML(anim.to_html5_video())
2026-04-27 09:47:18,888 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.FFMpegWriter'>
2026-04-27 09:47:18,889 - matplotlib.animation - INFO - MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 640x480 -pix_fmt rgba -framerate 5.0 -loglevel error -i pipe: -vcodec h264 -pix_fmt yuv420p -y /tmp/tmpcztb0dgx/temp.m4v
[4]:
[5]:
anim = gray_scott_anim(0.028, 0.062)
HTML(anim.to_html5_video())
2026-04-27 09:47:22,965 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.FFMpegWriter'>
2026-04-27 09:47:22,966 - matplotlib.animation - INFO - MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 640x480 -pix_fmt rgba -framerate 5.0 -loglevel error -i pipe: -vcodec h264 -pix_fmt yuv420p -y /tmp/tmpo_u749ml/temp.m4v
[5]:
Try to fit to the target steady state
Increasing the number of particles and the number of iterations will improve the fit, but take longer to run.
[6]:
def create_target_image(f, k):
gray_scott = simulated_gray_scott(f, k)
conc = gray_scott.simulation_results()[-1].species_concentration["V"][0, :]
conc = 255 * conc / np.max(conc)
Image.fromarray(conc.astype("uint8")).save("tmp.png")
gray_scott.export_sbml_file("tmp.xml")
[7]:
def apply_params(model, params):
model.parameters["f"].value = f"{params[0]}"
model.parameters["k"].value = f"{params[1]}"
[8]:
create_target_image(0.04, 0.06)
ss = smeopt.SteadyState(
"tmp.xml",
"tmp.png",
["V"],
apply_params,
[0.01, 0.05],
[0.06, 0.07],
5000,
2000,
90,
)
ss.find(5, 5)
ss.plot_all()
2026-04-27 09:47:26,688 - pyswarms.single.global_best - INFO - Optimize for 5 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
pyswarms.single.global_best: 100%|██████████|5/5, best_cost=16.3
2026-04-27 09:47:33,371 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 16.268201726018972, best pos: [0.02464089 0.05495265]
[9]:
create_target_image(0.051, 0.061)
ss = smeopt.SteadyState(
"tmp.xml",
"tmp.png",
["V"],
apply_params,
[0.01, 0.05],
[0.06, 0.07],
5000,
2000,
90,
)
ss.find(5, 5)
ss.plot_all()
2026-04-27 09:47:35,412 - pyswarms.single.global_best - INFO - Optimize for 5 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
pyswarms.single.global_best: 100%|██████████|5/5, best_cost=54.2
2026-04-27 09:47:42,137 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 54.1962770835492, best pos: [0.03465443 0.05775772]
[10]:
create_target_image(0.028, 0.062)
ss = smeopt.SteadyState(
"tmp.xml",
"tmp.png",
["V"],
apply_params,
[0.01, 0.05],
[0.06, 0.07],
5000,
2000,
90,
)
ss.find(5, 5)
ss.plot_all()
2026-04-27 09:47:44,043 - pyswarms.single.global_best - INFO - Optimize for 5 iters with {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
pyswarms.single.global_best: 100%|██████████|5/5, best_cost=252
2026-04-27 09:47:50,840 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 252.40159938485198, best pos: [0.03089146 0.0684057 ]
[ ]: