IMP logo
IMP Reference Guide  develop.549d75e6f4,2024/11/20
The Integrative Modeling Platform
domino/six_particles_optimization.py

Optimize six particles on a 2D unit grid. In order to remove translation degrees of freedom, the 0th particle is pinned at the origin by allowing it only a single conformation. To remove flips, the first particle is restrained to have a positive x coordinate.

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())