-
Notifications
You must be signed in to change notification settings - Fork 404
/
Copy pathsimple_equation.py
63 lines (54 loc) · 2.25 KB
/
simple_equation.py
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
# simple_equation.py
# From Classic Computer Science Problems in Python Chapter 5
# Copyright 2018 David Kopec
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from typing import Tuple, List
from chromosome import Chromosome
from genetic_algorithm import GeneticAlgorithm
from random import randrange, random
from copy import deepcopy
class SimpleEquation(Chromosome):
def __init__(self, x: int, y: int) -> None:
self.x: int = x
self.y: int = y
def fitness(self) -> float: # 6x - x^2 + 4y - y^2
return 6 * self.x - self.x * self.x + 4 * self.y - self.y * self.y
@classmethod
def random_instance(cls) -> SimpleEquation:
return SimpleEquation(randrange(100), randrange(100))
def crossover(self, other: SimpleEquation) -> Tuple[SimpleEquation, SimpleEquation]:
child1: SimpleEquation = deepcopy(self)
child2: SimpleEquation = deepcopy(other)
child1.y = other.y
child2.y = self.y
return child1, child2
def mutate(self) -> None:
if random() > 0.5: # mutate x
if random() > 0.5:
self.x += 1
else:
self.x -= 1
else: # otherwise mutate y
if random() > 0.5:
self.y += 1
else:
self.y -= 1
def __str__(self) -> str:
return f"X: {self.x} Y: {self.y} Fitness: {self.fitness()}"
if __name__ == "__main__":
initial_population: List[SimpleEquation] = [SimpleEquation.random_instance() for _ in range(20)]
ga: GeneticAlgorithm[SimpleEquation] = GeneticAlgorithm(initial_population=initial_population, threshold=13.0, max_generations = 100, mutation_chance = 0.1, crossover_chance = 0.7)
result: SimpleEquation = ga.run()
print(result)