IMP  2.0.0
The Integrative Modeling Platform
core/optimize_balls.py

This example optimizes a set of a balls to form 100 chains packed into a box. It illustrates using Monte Carlo (incremental) and conjugate gradients in conjunction in a non-trivial optimization.

1 ## \example core/optimize_balls.py
2 ## This example optimizes a set of a balls to form 100 chains packed into a box. It illustrates using Monte Carlo (incremental) and conjugate gradients in conjunction in a non-trivial optimization.
3 
4 import IMP.core
5 import IMP.display
6 import IMP.container
7 import IMP.rmf
8 
10  IMP.algebra.Vector3D(10,10,10));
11 # in fast do 10,10,10, for the purposes of testing we reduce it
12 ni=2
13 nj=2
14 np=2
15 radius=.45
16 k=100
17 
18 # using a HarmonicDistancePairScore for fixed length links is more
19 # efficient than using a HarmonicSphereDistnacePairScore and works
20 # better with the optimizer
21 lps= IMP.core.HarmonicDistancePairScore(1.5*radius, k)
23 
24 m= IMP.Model()
25 IMP.base.set_log_level(IMP.base.SILENT)
26 aps=[]
27 filters=[]
28 movers=[]
29 rss= IMP.RestraintSet(m, 1.0, "bonds")
30 for i in range(0,ni):
31  for j in range(0,nj):
32  base=IMP.algebra.Vector3D(i,j,0)
33  chain=[]
34  for k in range(0,np):
35  p= IMP.Particle(m)
36  p.set_name("P"+str(i)+" "+str(j)+" "+str(k))
39  movers.append(IMP.core.BallMover([p], radius*2))
40  movers[-1].set_was_used(True)
42  if k==0:
43  d.set_coordinates(base)
44  else:
45  d.set_coordinates_are_optimized(True)
46  chain.append(p)
47  aps.append(p)
48  # set up a chain of bonds
50  r= IMP.container.PairsRestraint(lps, cpc)
51  rss.add_restraint(r)
52 
53 # don't apply excluded volume to consecutive particles
57 rss.add_restraint(bbr)
58 
61 mc.set_name("MC")
62 sm= IMP.core.SerialMover(movers)
63 mc.add_mover(sm)
64 # we are special casing the nbl term
66 isf.set_name("I")
67 # use special incremental support for the non-bonded part
68 # apply the pair score sps to all touching ball pairs from the list of particles
69 # aps, using the filters to remove undersired pairs
70 # this is equivalent to the nbl construction above but optimized for incremental
71 isf.add_close_pair_score(sps, 0, aps, filters)
72 
73 # create a scoring function for conjugate gradients that includes the
74 # ExcludedVolumeRestraint
76 nbl.set_pair_filters(filters)
77 sf= IMP.core.RestraintsScoringFunction([rss, nbl], "RSF")
78 
79 if True:
80  mc.set_incremental_scoring_function(isf)
81 else:
82  # we could, instead do non-incremental scoring
83  mc.set_scoring_function(sf)
84 
85 # first relax the bonds a bit
86 rs=[]
87 for p in aps:
89  0))
90 cg.set_scoring_function(sf)
91 cg.optimize(1000)
92 print "collisions", nbl.evaluate(False), "bonds", rss.evaluate(False),
93 print bbr.evaluate(False)
94 
95 # shrink each of the particles, relax the configuration, repeat
96 for i in range(1,11):
97  rs=[]
98  factor=.1*i
99  for p in aps:
101  IMP.core.XYZR(p).get_radius()*factor))
102  # move each particle 100 times
103  print factor
104  for j in range(0,5):
105  print "stage", j
106  isf.set_log_level(IMP.base.TERSE)
107  mc.set_kt(100.0/(3*j+1))
108  print "mc", mc.optimize(ni*nj*np*(j+1)*100), m.evaluate(False), cg.optimize(10)
109  del rs
110  print "collisions", nbl.evaluate(False), "bonds", rss.evaluate(False),
111  print "bounding box", bbr.evaluate(False)
112 
113 w= IMP.display.PymolWriter("final.pym")
114 for p in aps:
116  w.add_geometry(g)
118 g.set_name("bb")
119 w.add_geometry(g)