diff options
author | Thomas Jourdan <b.vehikel@googlemail.com> | 2010-02-22 21:08:31 (GMT) |
---|---|---|
committer | Thomas Jourdan <b.vehikel@googlemail.com> | 2010-02-22 21:08:31 (GMT) |
commit | 267e69b6ca86aa040921f04a0fecdc1f2de52148 (patch) | |
tree | c0922f1623178eedbcf947aabd62464035cf38cb /ep_sampler_centeredwalk.py | |
parent | e899cc8750b1368eb57d91ca7ded96cb88b1de19 (diff) |
Bug fixing samplers / rendering engine. The changes in the rendering are not backwards compatible.
Diffstat (limited to 'ep_sampler_centeredwalk.py')
-rw-r--r-- | ep_sampler_centeredwalk.py | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/ep_sampler_centeredwalk.py b/ep_sampler_centeredwalk.py new file mode 100644 index 0000000..6f48e4a --- /dev/null +++ b/ep_sampler_centeredwalk.py @@ -0,0 +1,104 @@ +# coding: UTF-8 +# Copyright 2009, 2010 Thomas Jourdan +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +import ka_utils + +import model_locus +import model_random +import model_constraintpool +import ep_sampler_randomwalk +from gettext import gettext as _ + +SECTIONS_CONSTRAINT = 'sectionsconstraint' + +class CenteredWalkSampler(ep_sampler_randomwalk.RandomWalkSampler): + """CenteredWalkSampler: + inv: len(self.direction_steps) > 0 + """ + + cdef = [{'bind' : SECTIONS_CONSTRAINT, + 'name' : 'Number of sections.', + 'domain': model_constraintpool.INT_RANGE, + 'min' : 1, 'max': 100}, + ] + + def __init__(self, trunk): + """Constructor for a random walk.""" + super(CenteredWalkSampler, self).__init__(trunk) + + def __eq__(self, other): + """Equality.""" + equal = isinstance(other, CenteredWalkSampler) \ + and super(CenteredWalkSampler, self).__eq__(other) + return equal + + def crossingover(self, other): + """ + pre: isinstance(other, CenteredWalkSampler) + pre: isinstance(self, CenteredWalkSampler) + # check for distinct references, needs to copy content, not references + post: __return__ is not self + post: __return__ is not other + post: model_locus.unique_check(__return__, self, other) == '' + """ + new_one = CenteredWalkSampler(self.get_trunk()) + new_one.direction_steps = model_random.crossingover_list(self.direction_steps, + other.direction_steps) + return new_one + + @staticmethod + def _numeric_compare(point1, point2): + """Comparison function based on Euklidian distance from center 0.0.""" + diff1 = point1[0]*point1[0] + point1[1]*point1[1] + diff2 = point2[0]*point2[0] + point2[1]*point2[1] + if diff1 > diff2: + return 1 + elif diff1 < diff2: + return -1 + return 0 + + def get_sample_points(self): + """ Produces a list of sampling points. + The points describes an random walk starting near (0.5, 0.5). + """ + sample_points = [] +# sample_points.append( (0.0, 0.0) ) +# return sample_points + sample_points = super(CenteredWalkSampler, self).get_sample_points() + sample_points.sort(cmp=CenteredWalkSampler._numeric_compare, + reverse=False) + return sample_points + + def explain(self): + """ + post: len(__return__) == 3 + """ + head = _('Centered random walk sampler: %d points') \ + % (len(self.direction_steps)) + description = ka_utils.explain_points(head, self.get_sample_points()) + return description, \ + None, \ + None + + def copy(self): + """A copy constructor. + post: isinstance(__return__, CenteredWalkSampler) + # check for distinct references, needs to copy content, not references + post: __return__ is not self + """ + new_one = CenteredWalkSampler(self.get_trunk()) + new_one.direction_steps = model_random.copy_list(self.direction_steps) + return new_one |