IMP  2.1.1
The Integrative Modeling Platform
optimize_balls.py
1 ## \example core/optimize_balls.py
2 # This example optimizes a set of a balls to form 100 chains packed into a
3 # box. It illustrates using Monte Carlo (incremental) and conjugate
4 # gradients in conjunction in a non-trivial optimization.
5 
6 import IMP.core
7 import IMP.display
8 import IMP.container
9 import IMP.rmf
10 import IMP.base
11 import sys
12 
13 IMP.base.setup_from_argv(sys.argv, "Optimize balls example")
14 
16  IMP.algebra.Vector3D(10, 10, 10))
17 # in fast do 10,10,10, for the purposes of testing we reduce it
18 ni = 2
19 nj = 2
20 np = 2
21 radius = .45
22 k = 100
23 
24 # using a HarmonicDistancePairScore for fixed length links is more
25 # efficient than using a HarmonicSphereDistnacePairScore and works
26 # better with the optimizer
27 lps = IMP.core.HarmonicDistancePairScore(1.5 * radius, k)
29 
30 m = IMP.kernel.Model()
31 IMP.base.set_log_level(IMP.base.SILENT)
32 aps = []
33 filters = []
34 movers = []
35 restraints = []
36 for i in range(0, ni):
37  for j in range(0, nj):
38  base = IMP.algebra.Vector3D(i, j, 0)
39  chain = []
40  for k in range(0, np):
41  p = IMP.kernel.Particle(m)
42  p.set_name("P" + str(i) + " " + str(j) + " " + str(k))
46  d.set_coordinates_are_optimized(True)
47  movers.append(IMP.core.BallMover([p], radius * 2))
48  movers[-1].set_was_used(True)
50  p, IMP.display.get_display_color(i * nj + j))
51  if k == 0:
52  d.set_coordinates(base)
53  else:
54  d.set_coordinates_are_optimized(True)
55  chain.append(p)
56  aps.append(p)
57  # set up a chain of bonds
59  r = IMP.container.PairsRestraint(lps, cpc)
60  restraints.append(r)
61 
62 # don't apply excluded volume to consecutive particles
66 bbr = IMP.container.SingletonsRestraint(ibss, aps)
67 restraints.append(bbr)
68 
70 mc = IMP.core.MonteCarlo(m)
71 mc.set_name("MC")
72 sm = IMP.core.SerialMover(movers)
73 mc.add_mover(sm)
74 # we are special casing the nbl term
75 isf = IMP.core.IncrementalScoringFunction(aps, restraints)
76 isf.set_name("I")
77 # use special incremental support for the non-bonded part
78 # apply the pair score sps to all touching ball pairs from the list of particles
79 # aps, using the filters to remove undersired pairs
80 # this is equivalent to the nbl construction above but optimized for
81 # incremental
82 isf.add_close_pair_score(sps, 0, aps, filters)
83 
84 # create a scoring function for conjugate gradients that includes the
85 # ExcludedVolumeRestraint
86 nbl = IMP.core.ExcludedVolumeRestraint(aps, k, 1)
87 nbl.set_pair_filters(filters)
88 sf = IMP.core.RestraintsScoringFunction(restraints + [nbl], "RSF")
89 
90 if True:
91  mc.set_incremental_scoring_function(isf)
92 else:
93  # we could, instead do non-incremental scoring
94  mc.set_scoring_function(sf)
95 
96 # first relax the bonds a bit
97 rs = []
98 for p in aps:
100  0))
101 cg.set_scoring_function(sf)
102 cg.optimize(1000)
103 for r in restraints:
104  print r.get_name(), r.evaluate(False)
105 
106 # shrink each of the particles, relax the configuration, repeat
107 for i in range(1, 11):
108  rs = []
109  factor = .1 * i
110  for p in aps:
111  rs.append(
113  IMP.core.XYZR(p).get_radius() * factor))
114  # move each particle 100 times
115  print factor
116  for j in range(0, 5):
117  print "stage", j
118  mc.set_kt(100.0 / (3 * j + 1))
119  print "mc", mc.optimize(ni * nj * np * (j + 1) * 100), cg.optimize(10)
120  del rs
121  for r in restraints:
122  print r.get_name(), r.evaluate(False)
123 
124 w = IMP.display.PymolWriter("final.pym")
125 for p in aps:
126  g = IMP.core.XYZRGeometry(p)
127  w.add_geometry(g)
129 g.set_name("bb")
130 w.add_geometry(g)
Applies a SingletonScore to each Singleton in a list.
A Monte Carlo optimizer.
Definition: MonteCarlo.h:47
static FloatKey get_radius_key()
Definition: XYZR.h:76
See IMP.container for more information.
Upper bound harmonic function (non-zero when feature > mean)
void set_log_level(LogLevel l)
Set the current global log level.
See IMP.base for more information.
Definition: base/Array.h:20
Simple conjugate gradients optimizer.
Modify a set of continuous variables by perturbing them within a ball.
Score particles based on how far outside a box they are.
Vector3D get_random_vector_in(const Cylinder3D &c)
Generate a random vector in a cylinder with uniform density.
static XYZR setup_particle(kernel::Model *m, ParticleIndex pi)
Definition: XYZR.h:48
static Colored setup_particle(kernel::Model *m, ParticleIndex pi, Color color)
Definition: Colored.h:62
Color get_display_color(unsigned int i)
Class to handle individual model particles.
See IMP.core for more information.
Prevent a set of particles and rigid bodies from inter-penetrating.
Write a CGO file with the geometry.
Definition: PymolWriter.h:34
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
See IMP.display for more information.
Definition: BildWriter.h:20
See IMP.rmf for more information.
Definition: associations.h:20
Apply a list of movers one at a time.
Definition: SerialMover.h:23
Applies a PairScore to each Pair in a list.
Class for storing model, its restraints, constraints, and particles.
Display an IMP::core::XYZR particle as a ball.
Definition: XYZR.h:160
A decorator for a particle with x,y,z coordinates and a radius.
Definition: XYZR.h:27