Skip to content

Commit cbca179

Browse files
committed
add some more references
1 parent a0c5ae4 commit cbca179

File tree

7 files changed

+153
-97
lines changed

7 files changed

+153
-97
lines changed

README.Rmd

+55-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
output: github_document
3+
bibliography: references.bib
34
---
45

56
<!-- README.md is generated from README.Rmd. Please edit that file -->
@@ -21,9 +22,9 @@ knitr::opts_chunk$set(
2122

2223
<!-- badges: end -->
2324

24-
Genetic algorithms (GAs) are metaheuristic optimization techniques inspired by the principles of natural selection and evolution. They operate on a population of potential solutions, applying genetic operators such as selection, crossover, and mutation to evolve better solutions over successive generations.
25+
Genetic algorithms (GAs) are metaheuristic optimization techniques inspired by the principles of natural selection and evolution. They operate on a population of potential solutions, applying genetic operators such as selection, crossover, and mutation to evolve better solutions over successive generations[@whitley1994].
2526

26-
GAs are particularly useful for optimizing complex, non-linear functions with multiple local optima, where traditional gradient-based methods may fail.
27+
GAs are particularly useful for optimizing complex, non-linear functions with multiple local optima, where traditional gradient-based methods may fail[@holland1992; @goldberg1988].
2728

2829
We come out with a simple example to explore how these components work together in our quadratic function optimization problem using `genetic.algo.optimizeR` package.
2930

@@ -62,7 +63,55 @@ Here's a breakdown of the aim and the results:
6263

6364
After multiple generations of repeating these steps, the genetic algorithm aims to converge towards an optimal or near-optimal solution. In this example, since it's simple and the solution space is small, we could expect the algorithm to converge relatively quickly towards the optimal solution $x = 2$, where $f(x) = 0$.
6465

65-
[***Explaining Graph***](https://danymukesha.github.io/genetic.algo.optimizeR/articles/explaining_graph.html) ![Web capture_8-2-2024_233215_mermaid live](https://github.com/danymukesha/genetic.algo.optimizeR/assets/45208254/a9dc80bc-a464-4151-b9ff-9630310cdf9f)
66+
[***wikipedia***](https://en.wikipedia.org/wiki/Genetic_algorithm)
67+
68+
```{r diagram_GAs, eval=FALSE, include=FALSE}
69+
library(DiagrammeR)
70+
71+
grViz("
72+
digraph genetic_algorithm {
73+
rankdir=LR;
74+
75+
A [label = 'Initialize Population'];
76+
B [label = 'Evaluate Fitness'];
77+
C [label = 'Selection'];
78+
D [label = 'Termination Condition?'];
79+
E [label = 'End'];
80+
F [label = 'Crossover and Mutation'];
81+
G [label = 'Replace Population'];
82+
83+
X1 [label = 'Individual 1: x = 1', shape = ellipse];
84+
X2 [label = 'Individual 2: x = 3', shape = ellipse];
85+
X3 [label = 'Individual 3: x = 0', shape = ellipse];
86+
87+
Y1 [label = 'Selected Parents: x = 1, x = 3', shape = ellipse];
88+
89+
Z1 [label = 'Offspring 1: x = 1', shape = ellipse];
90+
Z2 [label = 'Offspring 2: x = 3', shape = ellipse];
91+
92+
A -> B;
93+
B -> C;
94+
C -> D;
95+
D -> E [label = 'Yes'];
96+
D -> F [label = 'No'];
97+
F -> G;
98+
G -> B;
99+
100+
# Examples
101+
B -> X1 [label = 'Example:'];
102+
B -> X2;
103+
B -> X3;
104+
105+
C -> Y1 [label = 'Example:'];
106+
107+
F -> Z1 [label = 'Example:'];
108+
F -> Z2;
109+
}
110+
")
111+
112+
```
113+
114+
![](https://github.com/danymukesha/genetic.algo.optimizeR/assets/45208254/a9dc80bc-a464-4151-b9ff-9630310cdf9f)
66115

67116
## Usage
68117

@@ -121,7 +170,7 @@ The above example illustrates the process of a genetic algorithm, where individu
121170
(Note: the values are random and the population should be highly diversified)
122171
- The space of x value is kept integer type and on range from 0 to 3,for simplification.
123172

124-
```{r}
173+
```{r population}
125174
population <- c(1, 3, 0)
126175
population
127176
```
@@ -144,7 +193,7 @@ $$
144193
145194
In R, we write:
146195
147-
```{r}
196+
```{r f_function}
148197
a <- 1
149198
b <- -4
150199
c <- 4
@@ -295,7 +344,7 @@ By understanding these fundamental concepts and their implementation, we can tak
295344

296345
### Multi-objective optimization
297346

298-
While our example focuses on single-objective optimization, genetic algorithms are particularly powerful for multi-objective problems. In such cases, the concept of Pareto optimality is used to evaluate solutions, and techniques like NSGA-II (Non-dominated Sorting Genetic Algorithm II) can be employed.
347+
While our example focuses on single-objective optimization, genetic algorithms are particularly powerful for multi-objective problems. In such cases, the concept of Pareto optimality is used to evaluate solutions, and techniques like NSGA-II (Non-dominated Sorting Genetic Algorithm II) can be employed[@deb2002].
299348

300349
### Adaptive parameter control
301350

@@ -319,7 +368,3 @@ I would encourage the users to experiment with different parameter settings and
319368

320369
## References
321370

322-
1. Goldberg, D. E. (1989). Genetic Algorithms in Search, Optimization and Machine Learning. Addison-Wesley.
323-
2. Holland, J. H. (1992). Adaptation in Natural and Artificial Systems. MIT Press.
324-
3. Whitley, D. (1994). A genetic algorithm tutorial. Statistics and Computing, 4(2), 65-85
325-
4. Deb, K., Pratap, A., Agarwal, S., & Meyarivan, T. A. M. T. (2002). A fast and elitist multiobjective genetic algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2), 182-197.

README.md

+18-13
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,15 @@ In this example, since it’s simple and the solution space is small, we
7878
could expect the algorithm to converge relatively quickly towards the
7979
optimal solution $x = 2$, where $f(x) = 0$.
8080

81-
[***Explaining
82-
Graph***](https://danymukesha.github.io/genetic.algo.optimizeR/articles/explaining_graph.html)
83-
![Web capture_8-2-2024_233215_mermaid
84-
live](https://github.com/danymukesha/genetic.algo.optimizeR/assets/45208254/a9dc80bc-a464-4151-b9ff-9630310cdf9f)
81+
[***wikipedia***](https://en.wikipedia.org/wiki/Genetic_algorithm)
82+
83+
<figure>
84+
<img
85+
src="https://github.com/danymukesha/genetic.algo.optimizeR/assets/45208254/a9dc80bc-a464-4151-b9ff-9630310cdf9f"
86+
alt="Web capture_8-2-2024_233215_mermaid live" />
87+
<figcaption aria-hidden="true">Web capture_8-2-2024_233215_mermaid
88+
live</figcaption>
89+
</figure>
8590

8691
## Usage
8792

@@ -93,7 +98,7 @@ population <- initialize_population(population_size = 3, min = 0, max = 3)
9398
print("Initial Population:")
9499
#> [1] "Initial Population:"
95100
print(population)
96-
#> [1] 2 0 1
101+
#> [1] 3 2 1
97102

98103
generation <- 0 # Initialize generation/reputation counter
99104

@@ -128,17 +133,17 @@ while (TRUE) {
128133
print(population)
129134
}
130135
#> [1] "Evaluation:"
131-
#> [1] 0 4 1
136+
#> [1] 1 0 1
132137
#> [1] "Selection:"
133-
#> [1] 2 1
138+
#> [1] 2 3
134139
#> [1] "Crossover and Mutation:"
135-
#> [1] 2 2
140+
#> [1] 3 3
136141
#> [1] "Replacement:"
137-
#> [1] 2 0 2
142+
#> [1] 3 2 3
138143
#> [1] "Evaluation:"
139-
#> [1] 0 4 0
144+
#> [1] 1 0 1
140145
#> [1] "Selection:"
141-
#> [1] 2 2
146+
#> [1] 2 3
142147
#> [1] "Crossover and Mutation:"
143148
#> [1] 2 2
144149
#> [1] "Replacement:"
@@ -386,9 +391,9 @@ versions of selection, crossover, and mutation operations can
386391
significantly reduce computation time for large-scale optimization
387392
problems.
388393

389-
## Wrap-Up
394+
## Wrap-up
390395

391-
Currrently only the fundamental concepts and implementation of genetic
396+
Currently only the fundamental concepts and implementation of genetic
392397
algorithms are used in the `genetic.algo.optimizeR` package. By
393398
following these steps and understanding the underlying principles, users
394399
can apply this powerful optimization technique to a wide range of

inst/tutorials/theory/theory.Rmd inst/tutorials/theoretical_background/theory.Rmd

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Theory background"
2+
title: "Tutorial on GAs"
33
output: learnr::tutorial
44
runtime: shiny_prerendered
55
---
@@ -11,7 +11,28 @@ library(dplyr)
1111
knitr::opts_chunk$set(echo = FALSE)
1212
```
1313

14-
## 1. Initialize Population:
14+
## Theoretical Background
15+
16+
Genetic algorithms (GAs) are metaheuristic optimization techniques inspired by the principles of natural selection and evolution. They operate on a population of potential solutions, applying genetic operators such as selection, crossover, and mutation to evolve better solutions over successive generations.
17+
18+
The key components of a genetic algorithm include:
19+
20+
1. **Encoding**: Representing potential solutions as "chromosomes"
21+
2. **Fitness Function**: Evaluating the quality of solutions
22+
3. **Selection**: Choosing individuals for reproduction based on fitness
23+
4. **Crossover**: Combining genetic information from parents to create offspring
24+
5. **Mutation**: Introducing random changes to maintain genetic diversity
25+
6. **Replacement**: Updating the population with new individuals
26+
27+
GAs are particularly useful for optimizing complex, non-linear functions with multiple local optima, where traditional gradient-based methods may fail.
28+
29+
## Aim
30+
31+
Our primary objective is to optimize the quadratic function \( f(x) = x^2 - 4x + 4 \) to find the value of \( x \) that minimizes the function. This function has a global minimum at \( x = 2 \), which we aim to discover using our genetic algorithm implementation.
32+
33+
## Method
34+
35+
### 1. Initialize Population:
1536

1637
- Start with a population of individuals: X1(x=1), X2(x=3), X3(x=0).\
1738
(Note: the values are random and the population should be highly diversified)
@@ -21,7 +42,7 @@ knitr::opts_chunk$set(echo = FALSE)
2142
population <- c(1, 3, 0)
2243
```
2344

24-
## 2. Evaluate Fitness:
45+
### 2. Evaluate Fitness:
2546

2647
- Calculate fitness(`f(x)`) for each individual:
2748

@@ -85,7 +106,7 @@ ggplot2::ggplot(df, aes(x = x, y = y)) +
85106
theme_minimal()
86107
```
87108

88-
## 3. Selection:
109+
### 3. Selection:
89110

90111
- Select parents for crossover:
91112

@@ -101,17 +122,17 @@ ggplot(df, aes(x = x, y = y)) +
101122
theme_minimal() # Use a minimal theme
102123
```
103124

104-
## 4. Crossover and Mutation:
125+
### 4. Crossover and Mutation:
105126

106127
- Generate offspring through crossover and mutation:
107128
- Z1(x=1), Z2(x=3) (no mutation in this example)
108129

109-
## 5. Replacement:
130+
### 5. Replacement:
110131

111132
- Replace individuals in the population:
112133
- Replace X3 with Z1, maintaining the population size.
113134

114-
## 6. Repeat Steps 2-5 for multiple generations until a termination condition is met.
135+
### 6. Repeat Steps 2-5 for multiple generations until a termination condition is met.
115136

116137
The optimal/fitting individuals *F* of a quadratic equation, in this case the lowest point on the graph of f(x), is:
117138

references.bib

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
@article{holland1992,
3+
title = {Adaptation in Natural and Artificial Systems},
4+
author = {Holland, John H.},
5+
year = {1992},
6+
month = {04},
7+
date = {1992-04-29},
8+
doi = {10.7551/mitpress/1090.001.0001},
9+
url = {http://dx.doi.org/10.7551/mitpress/1090.001.0001},
10+
langid = {en}
11+
}
12+
13+
@article{goldberg1988,
14+
author = {Goldberg, David E. and Holland, John H.},
15+
year = {1988},
16+
date = {1988},
17+
journal = {Machine Learning},
18+
pages = {95--99},
19+
volume = {3},
20+
number = {2/3},
21+
doi = {10.1023/a:1022602019183},
22+
url = {http://dx.doi.org/10.1023/A:1022602019183}
23+
}
24+
25+
@article{whitley1994,
26+
title = {A genetic algorithm tutorial},
27+
author = {Whitley, Darrell},
28+
year = {1994},
29+
month = {06},
30+
date = {1994-06},
31+
journal = {Statistics and Computing},
32+
volume = {4},
33+
number = {2},
34+
doi = {10.1007/bf00175354},
35+
url = {http://dx.doi.org/10.1007/BF00175354},
36+
langid = {en}
37+
}
38+
39+
@article{deb2002,
40+
title = {A fast and elitist multiobjective genetic algorithm: NSGA-II},
41+
author = {Deb, K. and Pratap, A. and Agarwal, S. and Meyarivan, T.},
42+
year = {2002},
43+
month = {04},
44+
date = {2002-04},
45+
journal = {IEEE Transactions on Evolutionary Computation},
46+
pages = {182--197},
47+
volume = {6},
48+
number = {2},
49+
doi = {10.1109/4235.996017},
50+
url = {http://dx.doi.org/10.1109/4235.996017}
51+
}

vignettes/explaining_graph.Rmd

-66
This file was deleted.

vignettes/optimize_function_with_GA.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The application of genetic algorithms (GAs) offers a powerful approach to optimi
2424

2525
Genetic algorithms are inspired by the principles of natural selection and genetics. They are widely utilized in optimization problems where traditional methods may falter due to the complexity of the search space. We will illustrate the step-by-step implementation of a genetic algorithm to minimize the quadratic function \( f(x) = x^2 - 4x + 4 \). The objective is to identify the value of \( x \) that yields the minimum value of the function, thereby providing insights into the capabilities and workings of GAs.
2626

27-
## Theory
27+
## Demonstration
2828

2929
### Initialization of population
3030

0 commit comments

Comments
 (0)