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:15,337 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.FFMpegWriter'>
2026-04-27 09:47:15,339 - 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/tmp7cwpjybb/temp.m4v
[3]:
[4]:
anim = gray_scott_anim(0.051, 0.061)
HTML(anim.to_html5_video())
2026-04-27 09:47:19,613 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.FFMpegWriter'>
2026-04-27 09:47:19,614 - 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/tmpdczgmoxz/temp.m4v
[4]:
[5]:
anim = gray_scott_anim(0.028, 0.062)
HTML(anim.to_html5_video())
2026-04-27 09:47:24,106 - matplotlib.animation - INFO - Animation.save using <class 'matplotlib.animation.FFMpegWriter'>
2026-04-27 09:47:24,107 - 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/tmp2f6qrba7/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:28,198 - 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=38.8
2026-04-27 09:47:39,442 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 38.83164832425679, best pos: [0.03671167 0.06009149]
[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:41,684 - 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=160
2026-04-27 09:47:52,741 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 160.12046834109208, best pos: [0.05512727 0.06244415]
[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:54,860 - 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:48:06,063 - pyswarms.single.global_best - INFO - Optimization finished | best cost: 252.40159938485198, best pos: [0.01279114 0.06259098]
[ ]: