Beispiel #1
0
    def pulse_random(self, iterations):
        '''
			Randomly pulse iteration times.
		'''
        for i in range(iterations):
            self.pulse(random_random())
            time.sleep(random_random())
Beispiel #2
0
def generator_skewed(floors, number_people, max_in_lift=6, skew_type="day-end",
                     skew_weighting=0.8):
    """Generates a new lift structure, skewed for either beginning or end of the day

    At the beginning or end of the day a much greater number of people want to either go
    from ground to their floor (beginning) or from their floor to ground (end).  This function
    acts like its uniform equivalent except some percentage of the people will want to do
    one of the things specified above.

    Arguments:
        floors         - The number of floors that the people should be generated over.
        number_people  - The number of people to generate.
        max_in_lift    - The max number of people who can fit in a lift. (Default: 6)
        skew_type      - Either 'day-end' of 'day-start', Instructs the function how to skew
                         the data. (Default: 'day-end')
        skew_weighting - The percentage chance of people following the skew.

    Returns:
        A valid lift_data structure - Follows the same structure as the BLANK_LIFT_DATA
                                      constant except it is now populated.
    """
    if floors < 2:
        raise ValueError("Floors must be at least 2")
    if skew_type not in ["day-end", "day-start"]:
        raise ValueError("skew_type must be either 'day-end' or 'day-start'")
    lift_struct = deepcopy(BLANK_LIFT_DATA)#deep copy as the black case contains lists
    #initialize the floors with lists to later put people in
    floor_people = []
    for _ in range(floors):
        floor_people.append([])
    #while you have people to put on a floor do:
    people_left = number_people
    while people_left >= 1:
        if skew_type == "day-end" and random_random() <= skew_weighting:
            person_want = 0
            person_floor = random_randint(0, floors-1)
        elif skew_type == "day-start" and random_random() <= skew_weighting:
            person_want = random_randint(0, floors-1)
            person_floor = 0
        else:
            person_want = random_randint(0, floors-1)
            person_floor = random_randint(0, floors-1)
        #check the person doesn't want to go to their own floor
        if person_floor == person_want:
            continue
        floor_people[person_floor].append((person_want, 0))
        people_left -= 1

    #populate remaining fields with people and returns
    lift_struct["floor info"] = floor_people
    lift_struct["lift max people"] = max_in_lift
    return lift_struct
Beispiel #3
0
def ununiform_mutation(self):
    for i in self.chromosome:
        for j in self.mutation_dict.items():
            value = random_random()
            if value < j[1][0]:
                i[j[0]] = random_uniform(
                    j[1][1] + i[j[0]],
                    j[1][2] + i[j[0]])
def stochastic_tournament(self):
    size = int(len(self.chromosome) * self.ratio)
    a = dict(enumerate(self.fitness()))
    b = []
    s = sum(a)
    for i in range(size):
        k = random_random() * s
        n = 0
        x = iter(a)
        while n < k:
            p = next(x)
            n += a[p]

        k = random_random() * s
        n = 0
        x = iter(a)
        while n < k:
            q = next(x)
            n += a[q]
        b.append(self.chromosome[p if a[p] > a[q] else q])
        s -= a.pop(p if a[p] > a[q] else q)
    self.chromosome = b
def roulette_wheel_selection(self):
    size = int(len(self.chromosome) * self.ratio)
    a = dict(enumerate(self.fitness()))
    b = []
    s = sum(a)
    for i in range(size):
        k = random_random() * s
        n = 0
        x = iter(a)
        while n < k:
            m = next(x)
            n += a[m]
        s -= a.pop(m)
        b.append(self.chromosome[m])
    self.chromosome = b
Beispiel #6
0
def coin(p: float) -> bool:
    """
    Flips a biased coin; returns ``True`` or ``False``, with the specified
    probability being that of ``True``.
    """
    # Slower code:

    # assert 0 <= p <= 1
    # r = random.random()  # range [0.0, 1.0), i.e. 0 <= r < 1
    # return r < p

    # Check edge cases:
    # - if p == 0, impossible that r < p, since r >= 0
    # - if p == 1, always true that r < p, since r < 1

    # Faster code:

    return random_random() < p
Beispiel #7
0
def simple_mutation(self):
    for i in self.chromosome:
        for j in self.mutation_dict.items():
            value = random_random()
            if value < j[1][0]:
                i[j[0]] = not i[j[0]]
Beispiel #8
0
def gauss_mutation(self, p = 1):
    for i in self.chromosome:
        for j in self.mutation_dict.items():
            value = random_random()
            if value < j[1][0]:
                i[j[0]] += random_gauss(0, p)
Beispiel #9
0
def boundary_mutation(self):
    for i in self.chromosome:
        for j in self.mutation_dict.items():
            value = random_random()
            if value < j[1][0]:
                i[j[0]] = random_choice([j[1][1], j[1][2]])
Beispiel #10
0
def random():
    return random_random()
Beispiel #11
0
 def random():
     """Normalised vector containing random entries in all dimensions"""
     vec = Vector([random_random(), random_random(), random_random()])
     vec.normalize()
     return vec
 def __random(self):
     return int(random_random() * 10**8)
Beispiel #13
0
def random()-> float:
    return random_random()
Beispiel #14
0
def fuzzy(extent=1.0):
    return (random_random() - 0.5) * extent * 2