IMP logo
IMP Reference Guide  develop.d97d4ead1f,2024/11/21
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 
6 import IMP
7 import sys
8 
9 IMP.setup_from_argv(sys.argv, "Optimizer state")
10 
11 
12 class MyOptimizerState(IMP.OptimizerState):
13  "An optimizer state which prints out the last scores of some restraints"
14 
15  def __init__(self, rs):
16  IMP.OptimizerState.__init__(self, rs[0].get_model(),
17  "MyOptimizerState%1%")
18  self.rs = rs
19 
20  def update(self):
21  for r in self.rs:
22  print(r.get_name(), r.get_last_score())
23 
24 
25 # some code to create and evaluate it
26 k = IMP.FloatKey("a key")
27 m = IMP.Model()
28 # we don't have any real restraints in the kernel
29 r0 = IMP._ConstRestraint(m, [], 1)
30 r0.set_name("restraint 0")
31 
32 r1 = IMP._ConstRestraint(m, [], 2)
33 r1.set_name("restraint 1")
34 
35 rs = IMP.RestraintSet([r0, r1], 1.0)
36 sf = rs.create_scoring_function()
37 
38 os = MyOptimizerState([r0, r1])
39 os.set_name("Python optimizer state")
40 # we don't have any optimizers either
41 co = IMP._ConstOptimizer(m)
42 co.set_scoring_function(sf)
43 co.add_optimizer_state(os)
44 print(co.optimize(100))
45 
46 # this is needed to clean up memory properly for some reason
47 co.remove_optimizer_state(os)
48 del os
49 del m
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
Object used to hold a set of restraints.
Definition: RestraintSet.h:41
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Shared optimizer state that is invoked upon commitment of new coordinates.