eckity.individual

 1from copy import deepcopy
 2
 3from eckity.fitness.fitness import Fitness
 4
 5
 6class Individual:
 7    """
 8    A candidate solution to the problem.
 9    This class is abstract and should not be instansiated directly.
10
11    Parameters
12    ----------
13    fitness: Fitness
14        This object manages the fitness state of the individual.
15
16    Attributes
17    ----------
18    id: int
19        The unique id of the individual.
20    gen: int
21        The generation number of the individual.
22    fitness: Fitness
23        This object manages the fitness state of the individual.
24    cloned_from: int
25        The id of the individual that this individual was cloned from.
26    selected_by: list
27        A list of selection methods that selected this individual in
28        the last generation.
29    applied_operators: list
30        A list of genetic operators that were applied on this individual
31        in the last generation.
32        *** Note that failed operators are still included in this list. ***
33    update_parents: bool, default=False
34        Whether the individual's parent(s) should be stored.
35    parents: list[int]
36        The ids of this individual's parents.
37    """
38
39    id = 1
40
41    def __init__(self, fitness: Fitness, update_parents: bool = False):
42        self.update_id()
43        self.gen = 0
44        self.fitness = fitness
45
46        # informational only
47        self.cloned_from = -1
48        self.selected_by = []
49        self.applied_operators = []
50
51        self.update_parents = update_parents
52        if update_parents:
53            self.parents = []
54
55    @property
56    def higher_is_better(self) -> bool:
57        return self.fitness.higher_is_better
58
59    def update_id(self):
60        self.id = Individual.id
61        Individual.id += 1
62
63    def set_fitness_not_evaluated(self):
64        self.fitness.set_not_evaluated()
65
66    def clone(self):
67        result = deepcopy(self)
68        result.cloned_from = self.id
69        if result.update_parents:
70            result.parents = []
71        result.update_id()
72        return result
73
74    def get_pure_fitness(self):
75        return self.fitness.get_pure_fitness()
76
77    def get_augmented_fitness(self):
78        return self.fitness.get_augmented_fitness(self)
79
80    def better_than(self, other):
81        if other is None:
82            return True
83
84        return self.fitness.better_than(self, other.fitness, other)
class Individual:
 7class Individual:
 8    """
 9    A candidate solution to the problem.
10    This class is abstract and should not be instansiated directly.
11
12    Parameters
13    ----------
14    fitness: Fitness
15        This object manages the fitness state of the individual.
16
17    Attributes
18    ----------
19    id: int
20        The unique id of the individual.
21    gen: int
22        The generation number of the individual.
23    fitness: Fitness
24        This object manages the fitness state of the individual.
25    cloned_from: int
26        The id of the individual that this individual was cloned from.
27    selected_by: list
28        A list of selection methods that selected this individual in
29        the last generation.
30    applied_operators: list
31        A list of genetic operators that were applied on this individual
32        in the last generation.
33        *** Note that failed operators are still included in this list. ***
34    update_parents: bool, default=False
35        Whether the individual's parent(s) should be stored.
36    parents: list[int]
37        The ids of this individual's parents.
38    """
39
40    id = 1
41
42    def __init__(self, fitness: Fitness, update_parents: bool = False):
43        self.update_id()
44        self.gen = 0
45        self.fitness = fitness
46
47        # informational only
48        self.cloned_from = -1
49        self.selected_by = []
50        self.applied_operators = []
51
52        self.update_parents = update_parents
53        if update_parents:
54            self.parents = []
55
56    @property
57    def higher_is_better(self) -> bool:
58        return self.fitness.higher_is_better
59
60    def update_id(self):
61        self.id = Individual.id
62        Individual.id += 1
63
64    def set_fitness_not_evaluated(self):
65        self.fitness.set_not_evaluated()
66
67    def clone(self):
68        result = deepcopy(self)
69        result.cloned_from = self.id
70        if result.update_parents:
71            result.parents = []
72        result.update_id()
73        return result
74
75    def get_pure_fitness(self):
76        return self.fitness.get_pure_fitness()
77
78    def get_augmented_fitness(self):
79        return self.fitness.get_augmented_fitness(self)
80
81    def better_than(self, other):
82        if other is None:
83            return True
84
85        return self.fitness.better_than(self, other.fitness, other)

A candidate solution to the problem. This class is abstract and should not be instansiated directly.

Parameters
  • fitness (Fitness): This object manages the fitness state of the individual.
Attributes
  • id (int): The unique id of the individual.
  • gen (int): The generation number of the individual.
  • fitness (Fitness): This object manages the fitness state of the individual.
  • cloned_from (int): The id of the individual that this individual was cloned from.
  • selected_by (list): A list of selection methods that selected this individual in the last generation.
  • applied_operators (list): A list of genetic operators that were applied on this individual in the last generation. *** Note that failed operators are still included in this list. ***
  • update_parents (bool, default=False): Whether the individual's parent(s) should be stored.
  • parents (list[int]): The ids of this individual's parents.
Individual( fitness: eckity.fitness.fitness.Fitness, update_parents: bool = False)
42    def __init__(self, fitness: Fitness, update_parents: bool = False):
43        self.update_id()
44        self.gen = 0
45        self.fitness = fitness
46
47        # informational only
48        self.cloned_from = -1
49        self.selected_by = []
50        self.applied_operators = []
51
52        self.update_parents = update_parents
53        if update_parents:
54            self.parents = []
id = 1
gen
fitness
cloned_from
selected_by
applied_operators
update_parents
higher_is_better: bool
56    @property
57    def higher_is_better(self) -> bool:
58        return self.fitness.higher_is_better
def update_id(self):
60    def update_id(self):
61        self.id = Individual.id
62        Individual.id += 1
def set_fitness_not_evaluated(self):
64    def set_fitness_not_evaluated(self):
65        self.fitness.set_not_evaluated()
def clone(self):
67    def clone(self):
68        result = deepcopy(self)
69        result.cloned_from = self.id
70        if result.update_parents:
71            result.parents = []
72        result.update_id()
73        return result
def get_pure_fitness(self):
75    def get_pure_fitness(self):
76        return self.fitness.get_pure_fitness()
def get_augmented_fitness(self):
78    def get_augmented_fitness(self):
79        return self.fitness.get_augmented_fitness(self)
def better_than(self, other):
81    def better_than(self, other):
82        if other is None:
83            return True
84
85        return self.fitness.better_than(self, other.fitness, other)