IMP logo
IMP Reference Guide  2.6.2
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 from __future__ import print_function
8 import IMP
9 import IMP.domino
10 import IMP.core
11 import IMP.container
12 import sys
13 
14 IMP.setup_from_argv(sys.argv, "six particles optimization")
15 
16 # set restraints
17 
18 
19 def create_scoring(m, ps):
20  pairs = [[0, 1], [0, 2], [1, 3], [2, 3], [3, 4], [4, 5], [1, 5]]
21  # we will restrain various pairs to be 1 apart
23  # the restraint will be broken apart during optimization
24  # map the indices above to actual particles
26  [(ps[p[0]], ps[p[1]]) for p in pairs],
27  "Restrained pairs")
28  pr = IMP.container.PairsRestraint(score, pc)
29  pr.set_maximum_score(.01)
31  IMP.algebra.Vector3D(2, 0, 0))
32  # force ps[1] to be on the positive side to remove flip degree of freedom
33  dr = IMP.core.SingletonRestraint(m, d, ps[1])
34  # we are not interested in conformations which don't fit the distances
35  # exactly, but using 0 is tricky
36  dr.set_maximum_score(.01)
37  return [pr, dr]
38 
39 
40 def create_representation(m):
41  ps = []
42  # create size particles, initial coordinates don't matter.
43  for i in range(0, 6):
44  p = m.add_particle("P%d" % i)
46  ps.append(p)
47  return ps
48 
49 
50 def create_discrete_states(m, ps):
52  vs = [IMP.algebra.Vector3D(1, 0, 0),
53  IMP.algebra.Vector3D(0, 1, 0),
54  IMP.algebra.Vector3D(1, 1, 0),
55  IMP.algebra.Vector3D(2, 1, 0),
56  IMP.algebra.Vector3D(2, 0, 0)]
57  vs = vs + [-v for v in vs]
58  print(len(vs), "states for each particle")
59  states = IMP.domino.XYZStates(vs)
60  # special case ps[0] to remove a sliding degree of freedom
61  # all other particles are given the same set of states
62  for p in ps[1:]:
63  pst.set_particle_states(m.get_particle(p), states)
64  return pst
65 
66 
67 def create_sampler(m, r, pst):
68  # create the sampler and pass it the states for each patricle
69  s = IMP.domino.DominoSampler(m, pst)
70  s.set_restraints(r)
71  # the following lines recreate the defaults and so are optional
72  filters = []
73  # create a restraint cache to avoid re-evaluating restraints
75  # add the list of restraints we want to use
76  rc.add_restraints(r)
77  # do not allow particles with the same ParticleStates object
78  # to have the same state index
79  filters.append(IMP.domino.ExclusionSubsetFilterTable(pst))
80  # filter states that score worse than the cutoffs in the Model
82  filters[-1].set_log_level(IMP.SILENT)
83  # try to be intelligent about enumerating the states in each subset
84  states = IMP.domino.BranchAndBoundAssignmentsTable(pst, filters)
85  states.set_log_level(IMP.SILENT)
86  s.set_assignments_table(states)
87  s.set_subset_filter_tables(filters)
88 
89  return s
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())