IMP logo
IMP Reference Guide  develop.63b38c487d,2024/12/22
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 
7 import IMP
8 import IMP.domino
9 import IMP.core
10 import IMP.container
11 import sys
12 
13 IMP.setup_from_argv(sys.argv, "six particles optimization")
14 
15 # set restraints
16 
17 
18 def 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 
39 def 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 
49 def 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 
66 def 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
83  states = IMP.domino.BranchAndBoundAssignmentsTable(pst, filters)
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 
91 IMP.set_log_level(IMP.TERSE)
92 m = IMP.Model()
93 # don't print information during Model.evaluate
94 m.set_log_level(IMP.SILENT)
95 
96 print("creating representation")
97 ps = create_representation(m)
98 print("creating discrete states")
99 pst = create_discrete_states(m, ps)
100 print("creating score function")
101 rs = create_scoring(m, ps)
102 print("creating sampler")
103 s = create_sampler(m, rs, pst)
104 
105 print("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.
110 cs = s.create_sample()
111 
112 print("found ", cs.get_number_of_configurations(), "solutions")
114 for 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())
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
Various classes to hold sets of particles.
Upper bound harmonic function (non-zero when feature > mean)
Sample best solutions using Domino.
Definition: DominoSampler.h:32
Filter a configuration of the subset using the Model thresholds.
Create a scoring function on a list of restraints.
Score distance between two particle centers using a harmonic function.
static XYZ setup_particle(Model *m, ParticleIndex pi)
Definition: XYZ.h:51
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Do not allow two particles to be in the same state.
Store a list of ParticleIndexPairs.
Apply a function to the distance to a fixed point.
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
void set_log_level(LogLevel l)
Set the current global log level.
Basic functionality that is expected to be used by a wide variety of IMP users.
VectorD< 3 > Vector3D
Definition: VectorD.h:408
Applies a SingletonScore to a Singleton.
Applies a PairScore to each Pair in a list.
Divide-and-conquer inferential optimization in discrete space.