IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
write_an_optimizer_state.py
1## \example kernel/write_an_optimizer_state.py
2# While we do not recommend doing serious work using optimizer states
3# written in Python, it is often useful when prototyping or testing code.
4# Copy this example and modify as needed.
5
6import IMP
7import sys
8
9IMP.setup_from_argv(sys.argv, "Optimizer state")
10
11
12class MyOptimizerState(IMP.OptimizerState):
13 "An optimizer state which prints out the last scores of some restraints"
14
15 def __init__(self, rs):
16 super().__init__(rs[0].get_model(), "MyOptimizerState%1%")
17 self.rs = rs
18
19 def update(self):
20 for r in self.rs:
21 print(r.get_name(), r.get_last_score())
22
23
24# some code to create and evaluate it
25k = IMP.FloatKey("a key")
26m = IMP.Model()
27# we don't have any real restraints in the kernel
28r0 = IMP._ConstRestraint(m, [], 1)
29r0.set_name("restraint 0")
30
31r1 = IMP._ConstRestraint(m, [], 2)
32r1.set_name("restraint 1")
33
34rs = IMP.RestraintSet([r0, r1], 1.0)
35sf = rs.create_scoring_function()
36
37os = MyOptimizerState([r0, r1])
38os.set_name("Python optimizer state")
39# we don't have any optimizers either
40co = IMP._ConstOptimizer(m)
41co.set_scoring_function(sf)
42co.add_optimizer_state(os)
43print(co.optimize(100))
44
45# this is needed to clean up memory properly for some reason
46co.remove_optimizer_state(os)
47del os
48del m
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Shared optimizer state that is invoked upon commitment of new coordinates.
Object used to hold a set of restraints.
void setup_from_argv(int argc, char **argv, std::string description)
Parse the command line flags and return the positional arguments.