IMP  2.4.0
The Integrative Modeling Platform
write_an_optimizer_state.py
1 ## \example kernel/write_an_optimizer_state.py
2 # While we do not recomment doing serious work using optimizer states
3 # written it python, it is often useful when prototyping or testing code.
4 # Copy this example and modify as needed.
5 
6 from __future__ import print_function
7 import IMP
8 
9 # an optimizer state which prints out the last scores of some restraints.
10 
11 
12 class MyOptimizerState(IMP.OptimizerState):
13 
14  def __init__(self, rs):
15  IMP.OptimizerState.__init__(self, rs[0].get_model(),
16  "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 # some code to create and evaluate it
24 k = IMP.FloatKey("a key")
25 m = IMP.kernel.Model()
26 # we don't have any real restraints in the kernel
27 r0 = IMP.kernel._ConstRestraint(m, [], 1)
28 r0.set_name("restraint 0")
29 
30 r1 = IMP.kernel._ConstRestraint(m, [], 2)
31 r1.set_name("restraint 1")
32 
33 rs = IMP.kernel.RestraintSet([r0, r1], 1.0)
34 sf = rs.create_scoring_function()
35 
36 os = MyOptimizerState([r0, r1])
37 os.set_name("python optimizer state")
38 # we don't have any optimizers either
39 co = IMP.kernel._ConstOptimizer(m)
40 co.set_scoring_function(sf)
41 co.add_optimizer_state(os)
42 print(co.optimize(100))
43 
44 # this is needed to clean up memory properly for some reason
45 co.remove_optimizer_state(os)
46 del os
47 del m
Shared optimizer state that is invoked upon commitment of new coordinates.
Object used to hold a set of restraints.
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:73