IMP  2.3.1
The Integrative Modeling Platform
kernel/write_an_optimizer_state.py

While we do not recomment doing serious work using optimizer states written it python, it is often useful when prototyping or testing code. Copy this example and modify as needed.

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 import IMP
7 
8 # an optimizer state which prints out the last scores of some restraints.
9 
10 
11 class MyOptimizerState(IMP.OptimizerState):
12 
13  def __init__(self, rs):
14  IMP.OptimizerState.__init__(self, rs[0].get_model(),
15  "MyOptimizerState%1%")
16  self.rs = rs
17 
18  def update(self):
19  for r in self.rs:
20  print r.get_name(), r.get_last_score()
21 
22 # some code to create and evaluate it
23 k = IMP.FloatKey("a key")
24 m = IMP.kernel.Model()
25 # we don't have any real restraints in the kernel
26 r0 = IMP.kernel._ConstRestraint(m, [], 1)
27 r0.set_name("restraint 0")
28 
29 r1 = IMP.kernel._ConstRestraint(m, [], 2)
30 r1.set_name("restraint 1")
31 
32 rs = IMP.kernel.RestraintSet([r0, r1], 1.0)
33 sf = rs.create_scoring_function()
34 
35 os = MyOptimizerState([r0, r1])
36 os.set_name("python optimizer state")
37 # we don't have any optimizers either
38 co = IMP.kernel._ConstOptimizer(m)
39 co.set_scoring_function(sf)
40 co.add_optimizer_state(os)
41 print co.optimize(100)
42 
43 # this is needed to clean up memory properly for some reason
44 co.remove_optimizer_state(os)
45 del os
46 del m