Skip to content

Instantly share code, notes, and snippets.

@nickinack
Created November 20, 2020 17:12
Show Gist options
  • Save nickinack/86c259e6da6525688aabc86ab3a3c532 to your computer and use it in GitHub Desktop.
Save nickinack/86c259e6da6525688aabc86ab3a3c532 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": 3
},
"orig_nbformat": 2
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class FeatureSolver(AbstractSolver):\n",
" def __init__(\n",
" self,\n",
" problem_type=float,\n",
" fitness_func= lambda a : fitness_func(a),\n",
" pop_cnt: int = 40,\n",
" gene_size: int = 50,\n",
" max_gen: int = 2,\n",
" mutation_ratio: float = 0.2,\n",
" selection_ratio: float = 0.2,\n",
" selection_type: str = \"roulette_wheel\",\n",
" mutation_type: str = \"insert\",\n",
" crossover_type: str = \"one_point\",\n",
" excluded_genes: Sequence = None,\n",
" variables_limits=(-10, 10),\n",
" verbose: bool = False,\n",
" cv=0,\n",
" **kwargs\n",
" ):\n",
" \"\"\"\n",
" :param fitness_function: can either be a fitness function or\n",
" a class implementing a fitness function + methods to override\n",
" the default ones: create_offspring, mutate_population, initialize_population\n",
" :param n_genes: number of genes (variables) to have in each chromosome\n",
" :param max_gen: maximum number of generations to perform the optimization\n",
" :param pop_size: population size\n",
" :param mutation_rate: rate at which random mutations occur\n",
" :param selection_rate: percentage of the population to be selected for crossover\n",
" :param selection_strategy: strategy to use for selection\n",
" :param verbose: whether to print iterations status\n",
" :param show_stats: whether to print stats at the end\n",
" :param plot_results: whether to plot results of the run at the end\n",
" :param variables_limits: limits for each variable [(x1_min, x1_max), (x2_min, x2_max), ...].\n",
" If only one tuple is provided, then it is assumed the same for every variable\n",
" :param problem_type: whether problem is of float or integer type\n",
" \"\"\"\n",
"\n",
" AbstractSolver.__init__(\n",
" self,\n",
" problem_type=problem_type,\n",
" gene_size=gene_size,\n",
" fitness_func=fitness_func,\n",
" pop_cnt=pop_cnt,\n",
" max_gen=max_gen,\n",
" mutation_ratio=mutation_ratio,\n",
" selection_ratio=selection_ratio,\n",
" selection_type=selection_type,\n",
" mutation_type=mutation_type,\n",
" crossover_type=crossover_type,\n",
" excluded_genes=excluded_genes,\n",
" verbose=verbose,\n",
" cv=cv,\n",
" **kwargs\n",
" )\n",
" \n",
" def initialize_population(self):\n",
" \"\"\"\n",
" Initializes the population of the problem according to the\n",
" population size and number of genes and according to the problem\n",
" type (either integers or floats).\n",
"\n",
" :return: a numpy array with a randomized initialized population\n",
" \"\"\"\n",
" population = np.empty(shape=(self.pop_cnt, self.gene_size))\n",
" for i in range(0,self.pop_cnt):\n",
" population[i] = random.sample(range(0,99) , self.gene_size)\n",
"\n",
"\n",
" return population\n",
"\n",
"random.seed(10) \n",
"solver = FeatureSolver(\n",
" gene_size=50,\n",
" cv=0 \n",
" )\n",
"\n",
"list(solver.solve())"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment