IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
six_particles_optimization.py
1## \example domino/six_particles_optimization.py
2# Optimize six particles on a 2D unit grid. In order to remove translation
3# degrees of freedom, the 0th particle is pinned at the origin by allowing
4# it only a single conformation. To remove flips, the first particle is
5# restrained to have a positive x coordinate.
6
7import IMP
8import IMP.domino
9import IMP.core
10import IMP.container
11import sys
12
13IMP.setup_from_argv(sys.argv, "six particles optimization")
14
15# set restraints
16
17
18def create_scoring(m, ps):
19 pairs = [[0, 1], [0, 2], [1, 3], [2, 3], [3, 4], [4, 5], [1, 5]]
20 # we will restrain various pairs to be 1 apart
22 # the restraint will be broken apart during optimization
23 # map the indices above to actual particles
25 [(ps[p[0]], ps[p[1]]) for p in pairs],
26 "Restrained pairs")
27 pr = IMP.container.PairsRestraint(score, pc)
28 pr.set_maximum_score(.01)
30 IMP.algebra.Vector3D(2, 0, 0))
31 # force ps[1] to be on the positive side to remove flip degree of freedom
32 dr = IMP.core.SingletonRestraint(m, d, ps[1])
33 # we are not interested in conformations which don't fit the distances
34 # exactly, but using 0 is tricky
35 dr.set_maximum_score(.01)
36 return [pr, dr]
37
38
39def create_representation(m):
40 ps = []
41 # create size particles, initial coordinates don't matter.
42 for i in range(0, 6):
43 p = m.add_particle("P%d" % i)
45 ps.append(p)
46 return ps
47
48
49def create_discrete_states(m, ps):
51 vs = [IMP.algebra.Vector3D(1, 0, 0),
52 IMP.algebra.Vector3D(0, 1, 0),
53 IMP.algebra.Vector3D(1, 1, 0),
54 IMP.algebra.Vector3D(2, 1, 0),
55 IMP.algebra.Vector3D(2, 0, 0)]
56 vs = vs + [-v for v in vs]
57 print(len(vs), "states for each particle")
58 states = IMP.domino.XYZStates(vs)
59 # special case ps[0] to remove a sliding degree of freedom
60 # all other particles are given the same set of states
61 for p in ps[1:]:
62 pst.set_particle_states(m.get_particle(p), states)
63 return pst
64
65
66def create_sampler(m, r, pst):
67 # create the sampler and pass it the states for each patricle
68 s = IMP.domino.DominoSampler(m, pst)
69 s.set_restraints(r)
70 # the following lines recreate the defaults and so are optional
71 filters = []
72 # create a restraint cache to avoid re-evaluating restraints
74 # add the list of restraints we want to use
75 rc.add_restraints(r)
76 # do not allow particles with the same ParticleStates object
77 # to have the same state index
78 filters.append(IMP.domino.ExclusionSubsetFilterTable(pst))
79 # filter states that score worse than the cutoffs in the Model
81 filters[-1].set_log_level(IMP.SILENT)
82 # try to be intelligent about enumerating the states in each subset
84 states.set_log_level(IMP.SILENT)
85 s.set_assignments_table(states)
86 s.set_subset_filter_tables(filters)
87
88 return s
89
90
91IMP.set_log_level(IMP.TERSE)
92m = IMP.Model()
93# don't print information during Model.evaluate
94m.set_log_level(IMP.SILENT)
95
96print("creating representation")
97ps = create_representation(m)
98print("creating discrete states")
99pst = create_discrete_states(m, ps)
100print("creating score function")
101rs = create_scoring(m, ps)
102print("creating sampler")
103s = create_sampler(m, rs, pst)
104
105print("sampling")
106# get an IMP.ConfigurationSet with the sampled states. If there are very
107# many, it might be better to use s.get_sample_states() and then
108# IMP.domino.load_particle_states() to handle the states as that takes
109# much less memory, and time.
110cs = s.create_sample()
111
112print("found ", cs.get_number_of_configurations(), "solutions")
114for i in range(cs.get_number_of_configurations()):
115 cs.load_configuration(i)
116 print("solution number:", i, " is:", sf.evaluate(False))
117 for p in ps:
118 print(IMP.core.XYZ(m, p).get_x(), IMP.core.XYZ(m, p).get_y())
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Store a list of ParticleIndexPairs.
Applies a PairScore to each Pair in a list.
Score distance between two particle centers using a harmonic function.
Upper bound harmonic function (non-zero when feature > mean).
Create a scoring function on a list of restraints.
Applies a SingletonScore to a Singleton.
A decorator for a particle with x,y,z coordinates.
Definition XYZ.h:30
static XYZ setup_particle(Model *m, ParticleIndex pi)
Definition XYZ.h:51
Sample best solutions using Domino.
Do not allow two particles to be in the same state.
Filter a configuration of the subset using the Model thresholds.
VectorD< 3 > Vector3D
Definition VectorD.h:407
Various classes to hold sets of particles.
Basic functionality that is expected to be used by a wide variety of IMP users.
Divide-and-conquer inferential optimization in discrete space.
void set_log_level(LogLevel l)
Set the current global log level.
void setup_from_argv(int argc, char **argv, std::string description)
Parse the command line flags and return the positional arguments.