IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
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 and conjugate
4# gradients in conjunction in a non-trivial optimization.
5
6import IMP.core
7import IMP.display
8import IMP.container
9import sys
10
11IMP.setup_from_argv(sys.argv, "Optimize balls example")
12
14 IMP.algebra.Vector3D(10, 10, 10))
16 ni = 2
17 nj = 2
18 np = 2
19 radius = .45
20 k = 100
21 ncg = 10
22 nmc = 1
23 ninner = 1
24 nouter = 1
25else:
26 ni = 10
27 nj = 10
28 np = 10
29 radius = .45
30 k = 100
31 ncg = 1000
32 nmc = ni * nj * np * 100
33 ninner = 5
34 nouter = 11
35
36print(IMP.get_is_quick_test(), ni, nj, np, ninner, nouter)
37# using a HarmonicDistancePairScore for fixed length links is more
38# efficient than using a HarmonicSphereDistnacePairScore and works
39# better with the optimizer
40lps = IMP.core.HarmonicDistancePairScore(1.5 * radius, k)
42
43m = IMP.Model()
44# IMP.set_log_level(IMP.SILENT)
45aps = []
46filters = []
47movers = []
48restraints = []
49for i in range(0, ni):
50 for j in range(0, nj):
51 base = IMP.algebra.Vector3D(i, j, 0)
52 chain = []
53 for k in range(0, np):
54 p = IMP.Particle(m)
55 p.set_name("P" + str(i) + " " + str(j) + " " + str(k))
59 d.set_coordinates_are_optimized(True)
60 movers.append(IMP.core.BallMover(m, p, radius * 2))
61 movers[-1].set_was_used(True)
63 p, IMP.display.get_display_color(i * nj + j))
64 if k == 0:
65 d.set_coordinates(base)
66 else:
67 d.set_coordinates_are_optimized(True)
68 chain.append(p)
69 aps.append(p)
70 # set up a chain of bonds
73 restraints.append(r)
74
75# don't apply excluded volume to consecutive particles
80restraints.append(bbr)
81
84mc.set_name("MC")
85sm = IMP.core.SerialMover(movers)
86mc.add_mover(sm)
87
88# create a scoring function for conjugate gradients that includes the
89# ExcludedVolumeRestraint
91nbl.set_pair_filters(filters)
92sf = IMP.core.RestraintsScoringFunction(restraints + [nbl], "RSF")
93
94mc.set_scoring_function(sf)
95
96# Speed up the optimization by only rescoring terms involving particles
97# that moved at each MC step
98mc.set_score_moved(True)
99
100# first relax the bonds a bit
101rs = []
102for p in aps:
104 0))
105cg.set_scoring_function(sf)
106cg.optimize(ncg)
107for r in restraints:
108 print(r.get_name(), r.evaluate(False))
109
110# shrink each of the particles, relax the configuration, repeat
111for i in range(1, nouter):
112 rs = []
113 factor = .1 * i
114 for p in aps:
115 rs.append(
118 IMP.core.XYZR(p).get_radius() * factor))
119 # move each particle nmc times
120 print(factor)
121 for j in range(0, ninner):
122 print("stage", j)
123 mc.set_kt(100.0 / (3 * j + 1))
124 print("mc", mc.optimize((j + 1) * nmc), cg.optimize(nmc))
125 del rs
126 for r in restraints:
127 print(r.get_name(), r.evaluate(False))
128
129w = IMP.display.PymolWriter("final.pym")
130for p in aps:
132 w.add_geometry(g)
134g.set_name("bb")
135w.add_geometry(g)
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Class to handle individual particles of a Model object.
Definition Particle.h:45
Applies a PairScore to each Pair in a list.
Applies a SingletonScore to each Singleton in a list.
Move continuous particle variables by perturbing them within a ball.
Simple conjugate gradients optimizer.
Prevent a set of particles and rigid bodies from inter-penetrating.
Score distance between two particle centers using a harmonic function.
Upper bound harmonic function (non-zero when feature > mean).
A Monte Carlo optimizer.
Definition MonteCarlo.h:46
Create a scoring function on a list of restraints.
Applies a list of movers one at a time.
Definition SerialMover.h:26
Display an IMP::core::XYZR particle as a ball.
Definition XYZR.h:151
A decorator for a particle with x,y,z coordinates and a radius.
Definition XYZR.h:27
static XYZR setup_particle(Model *m, ParticleIndex pi)
Definition XYZR.h:48
static FloatKey get_radius_key()
Get the key for the radius.
Definition XYZR.h:76
static Colored setup_particle(Model *m, ParticleIndex pi, Color color)
Definition Colored.h:62
Write a CGO file with the geometry.
Definition PymolWriter.h:34
VectorD< 3 > Vector3D
Definition VectorD.h:407
VectorD< D > get_random_vector_in(const BoundingBoxD< D > &bb)
Generate a random vector in a box with uniform density.
Various classes to hold sets of particles.
Basic functionality that is expected to be used by a wide variety of IMP users.
Output IMP model data in various file formats.
Color get_display_color(unsigned int i)
bool get_is_quick_test()
Definition flags.h:183
void setup_from_argv(int argc, char **argv, std::string description)
Parse the command line flags and return the positional arguments.