Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_sampler_centeredwalk.py
blob: 35e4aa4d8925a20a6d75fb2dd4d8312f27f56554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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 = 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))
        return ka_utils.explain_points(head, self.get_sample_points())

    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