Added some error checking

This commit is contained in:
danielwilczak101
2020-10-22 00:03:51 -04:00
parent 8e2698fc0d
commit 74f6c8957b
2 changed files with 20 additions and 5 deletions

View File

@ -64,7 +64,7 @@ class attributes:
self.termination_impl = Termination_Methods.generation_based self.termination_impl = Termination_Methods.generation_based
# Example of how the setter error checking will look like # Getter and setters for all varibles
@property @property
def chromosome_length(self): def chromosome_length(self):
return self._chromosome_length return self._chromosome_length
@ -72,5 +72,20 @@ class attributes:
@chromosome_length.setter @chromosome_length.setter
def chromosome_length(self, value_input): def chromosome_length(self, value_input):
if(value_input == 0): if(value_input == 0):
raise ValueError("Sorry your chromosome length must be greater then 0") raise ValueError("Chromosome length must be greater then 0")
self._chromosome_length = value_input self._chromosome_length = value_input
@property
def population_size(self):
return self._population_size
@population_size.setter
def population_size(self, value_input):
if(value_input == 0):
raise ValueError("Population length must be greater then 0")
self._population_size = value_input
@property
def chromosome_impl(self):
return self._chromosome_impl

View File

@ -3,10 +3,10 @@ import EasyGA
# Create the Genetic algorithm # Create the Genetic algorithm
ga = EasyGA.GA() ga = EasyGA.GA()
ga.target_fitness_type = 'min' ga.target_fitness_type = 'max'
ga.chromosome_length = 10 ga.chromosome_length = 10
ga.population_size = 25 ga.population_size = 10
ga.generation_goal = 50 ga.generation_goal = 200
ga.gene_impl = lambda: random.randint(0, 10) ga.gene_impl = lambda: random.randint(0, 10)
def fitness_function(chromosome): def fitness_function(chromosome):