Skip to content

Commit

Permalink
FAC_LOC: fixed exercises 3.1 text which was duplicate of 3.2!
Browse files Browse the repository at this point in the history
  • Loading branch information
TomMonks committed Jan 27, 2021
1 parent c2c6d62 commit 795ffdf
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 802 deletions.
42 changes: 25 additions & 17 deletions optimisation/02_facility_location.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -395,32 +395,40 @@
"source": [
"## Exercise 3.1 Generating an initial population.\n",
"\n",
"Basic evolutionary strategies work by mutating the most promising solutions in the population. There are many ways to implement mutation. Here you will use `BasicFacilityLocationMutator`. Each element in a solution has a constant probability of mutation (by default 1 / no. of facilities in a solution, but you may wish to set this higher.). If a facility is chosen then it is replaced by a random facility current not in the solution.\n",
"\n",
"You can create a `BasicFacilityLocationMutator` as follows:\n",
"The first task when using a population based method is to create an initial random population of solutions! For our purposes, this is a multi-dimensional array. We can use an object of type `FacilityLocationPopulationGenerator` to do the work for us here.\n",
"\n",
"```python\n",
"mutator = FacilityLocationMutator(n_candidates=28, solution_size=4)\n",
"solution = np.array([1, 2, 3, 4])\n",
"\n",
"mutant = mutator.mutate(solution)\n",
"print(mutant)\n",
"from metapy.evolutionary.evolutionary import FacilityLocationPopulationGenerator\n",
"```\n",
"\n",
"To use a higher mutation rate:\n",
"`FacilityLocationPopulationGenerator` accepts three arguments when it is created:\n",
"\n",
"* n_candidates: int. This is $P$ the number of candidate locations\n",
"* n_facilities: int. This is $p$ the number of facilities to place.\n",
"* random_seed: int, optional (default=None). Set if you want a reproducible result. For example = 42.\n",
"\n",
"`FacilityLocationPopulationGenerator` has a single method `generate` that accepts a parameter specifying the population size. It returns a multi-dimensional numpy array.\n",
"\n",
"Let's assume you want have a problem with $P$ = 28, $p$ = 8 and we want to create a population of size 10.\n",
"\n",
"```python\n",
"mutator = FacilityLocationMutator(n_candidates=28, solution_size=4\n",
" mutation_rate=0.6)\n",
"solution = np.array([1, 2, 3, 4])\n",
"#example solution\n",
"N_CANDIDATES = 28\n",
"N_FACILITIES = 8\n",
"SEED = 42\n",
"POPULATION_SIZE = 10\n",
"\n",
"mutant = mutator.mutate(solution)\n",
"print(mutant)\n",
"gen = FacilityLocationPopulationGenerator(n_candidates=N_CANDIDATES,\n",
" n_facilities=N_FACILITIES,\n",
" random_seed=SEED)\n",
"\n",
"\n",
"gen.generate(population_size=POPULATION_SIZE)\n",
"```\n",
"\n",
"**Task**:\n",
"* Create a `FacilityLocationMutator` with 50 candidate solutions and a solution size of 10\n",
"* mutate the solution a total of 10 times and printout the generations."
"**Task:**\n",
"\n",
"* Run the example code given above to create an initial population. You"
]
},
{
Expand Down
Loading

0 comments on commit 795ffdf

Please sign in to comment.