IMP  2.0.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 written it python, it is often useful when prototyping or testing code. Copy this example and modify as needed.
3 
4 import IMP
5 
6 # an optimizer state which prints out model statistics.
7 class MyOptimizerState(IMP.OptimizerState):
8  def __init__(self):
9  IMP.OptimizerState.__init__(self)
10  def update(self):
11  self.get_optimizer().get_model().show_restraint_score_statistics()
12 
13 # some code to create and evaluate it
14 k= IMP.FloatKey("a key")
15 m= IMP.Model()
16 # we don't have any real restraints in the kernel
17 r0=IMP.kernel._ConstRestraint(1)
18 r0.set_name("restraint 0")
19 m.add_restraint(r0)
20 r1=IMP.kernel._ConstRestraint(2)
21 r1.set_name("restraint 1")
22 m.add_restraint(r1)
23 
24 os= MyOptimizerState()
25 os.set_name("python optimizer state")
26 # we don't have any optimizers either
27 co= IMP.kernel._ConstOptimizer(m)
28 co.add_optimizer_state(os)
29 m.set_gather_statistics(True)
30 # so we only see the statistics
31 IMP.base.set_log_level(IMP.base.SILENT)
32 print co.optimize(100)
33 
34 # this is needed to clean up memory properly for some reason
35 co.remove_optimizer_state(os)
36 del os
37 del m