IMP  2.4.0
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 degrees
3 # of freedom, the 0th particle is pinned at the origin by allowing it only a
4 # single conformation. To remove flips, the first particle is restrained to
5 # have a positive x coordinate.
6 
7 import IMP
8 import IMP.domino
9 import IMP.core
10 import IMP.container
11 
12 # set restraints
13 
14 
15 def create_scoring(m, ps):
16  pairs = [[0, 1], [0, 2], [1, 3], [2, 3], [3, 4], [4, 5], [1, 5]]
17  # we will restrain various pairs to be 1 apart
19  # the restraint will be broken apart during optimization
20  # map the indices above to actual particles
21  pc = IMP.container.ListPairContainer([(ps[p[0]], ps[p[1]]) for p in pairs],
22  "Restrained pairs")
23  pr = IMP.container.PairsRestraint(score, pc)
24  pr.set_maximum_score(.01)
26  IMP.algebra.Vector3D(2, 0, 0))
27  # force ps[1] to be on the positive side to remove flip degree of freedom
28  dr = IMP.core.SingletonRestraint(d, ps[1])
29  # we are not interested in conformations which don't fit the distances
30  # exactly, but using 0 is tricky
31  dr.set_maximum_score(.01)
32  print(m.get_root_restraint_set())
33  return [pr, dr]
34 
35 
36 def create_representation(m):
37  ps = []
38  # create size particles, initial coordinates don't matter.
39  for i in range(0, 6):
40  p = IMP.kernel.Particle(m)
42  ps.append(p)
43  return ps
44 
45 
46 def create_discrete_states(ps):
48  vs = [IMP.algebra.Vector3D(1, 0, 0),
49  IMP.algebra.Vector3D(0, 1, 0),
50  IMP.algebra.Vector3D(1, 1, 0),
51  IMP.algebra.Vector3D(2, 1, 0),
52  IMP.algebra.Vector3D(2, 0, 0)]
53  vs = vs + [-v for v in vs]
54  print(len(vs), "states for each particle")
55  states = IMP.domino.XYZStates(vs)
56  # special case ps[0] to remove a sliding degree of freedom
57  # all other particles are given the same set of states
58  for p in ps[1:]:
59  pst.set_particle_states(p, states)
60  return pst
61 
62 
63 def create_sampler(m, r, pst):
64  # create the sampler and pass it the states for each patricle
65  s = IMP.domino.DominoSampler(m, pst)
66  # the following lines recreate the defaults and so are optional
67  filters = []
68  # create a restraint cache to avoid re-evaluating restraints
70  # add the list of restraints we want to use
71  rc.add_restraints(r)
72  # do not allow particles with the same ParticleStates object
73  # to have the same state index
74  filters.append(IMP.domino.ExclusionSubsetFilterTable(pst))
75  # filter states that score worse than the cutoffs in the Model
77  filters[-1].set_log_level(IMP.base.SILENT)
78  # try to be intelligent about enumerating the states in each subset
79  states = IMP.domino.BranchAndBoundAssignmentsTable(pst, filters)
80  states.set_log_level(IMP.base.SILENT)
81  s.set_assignments_table(states)
82  s.set_subset_filter_tables(filters)
83 
84  return s
85 
86 IMP.base.set_log_level(IMP.base.TERSE)
87 m = IMP.kernel.Model()
88 # don't print information during Model.evaluate
89 m.set_log_level(IMP.base.SILENT)
90 
91 print("creating representation")
92 ps = create_representation(m)
93 print("creating discrete states")
94 pst = create_discrete_states(ps)
95 print("creating score function")
96 rs = create_scoring(m, ps)
97 print("creating sampler")
98 s = create_sampler(m, rs, pst)
99 
100 print("sampling")
101 # get an IMP.ConfigurationSet with the sampled states. If there are very
102 # many, it might be better to use s.get_sample_states() and then
103 # IMP.domino.load_particle_states() to handle the states as that takes
104 # much less memory, and time.
105 cs = s.create_sample()
106 
107 print("found ", cs.get_number_of_configurations(), "solutions")
108 for i in range(cs.get_number_of_configurations()):
109  cs.load_configuration(i)
110  print("solution number:", i, " is:", m.evaluate(False))
111  for p in ps:
112  print(IMP.core.XYZ(p).get_x(), IMP.core.XYZ(p).get_y())