├── .github
└── ISSUE_TEMPLATE
│ ├── feature_request.md
│ └── bug_report.md
├── LICENSE
├── .gitignore
├── README.md
└── Assignment 1.ipynb
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | labels:
5 |
6 | ---
7 |
8 | **Is your feature request related to a problem? Please describe.**
9 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
10 |
11 | **Describe the solution you'd like**
12 | A clear and concise description of what you want to happen.
13 |
14 | **Describe alternatives you've considered**
15 | A clear and concise description of any alternative solutions or features you've considered.
16 |
17 | **Additional context**
18 | Add any other context or screenshots about the feature request here.
19 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | labels:
5 |
6 | ---
7 |
8 | **Describe the bug**
9 | A clear and concise description of what the bug is.
10 |
11 | **To Reproduce**
12 | Steps to reproduce the behavior:
13 | 1. Go to '...'
14 | 2. Click on '....'
15 | 3. Scroll down to '....'
16 | 4. See error
17 |
18 | **Expected behavior**
19 | A clear and concise description of what you expected to happen.
20 |
21 | **Screenshots**
22 | If applicable, add screenshots to help explain your problem.
23 |
24 | **Desktop (please complete the following information):**
25 | - OS: [e.g. iOS]
26 | - Browser [e.g. chrome, safari]
27 | - Version [e.g. 22]
28 |
29 | **Additional context**
30 | Add any other context about the problem here.
31 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Computational Intelligence - University of Guilan
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | *.egg-info/
24 | .installed.cfg
25 | *.egg
26 | MANIFEST
27 |
28 | # PyInstaller
29 | # Usually these files are written by a python script from a template
30 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
31 | *.manifest
32 | *.spec
33 |
34 | # Installer logs
35 | pip-log.txt
36 | pip-delete-this-directory.txt
37 |
38 | # Unit test / coverage reports
39 | htmlcov/
40 | .tox/
41 | .coverage
42 | .coverage.*
43 | .cache
44 | nosetests.xml
45 | coverage.xml
46 | *.cover
47 | .hypothesis/
48 | .pytest_cache/
49 |
50 | # Translations
51 | *.mo
52 | *.pot
53 |
54 | # Django stuff:
55 | *.log
56 | local_settings.py
57 | db.sqlite3
58 |
59 | # Flask stuff:
60 | instance/
61 | .webassets-cache
62 |
63 | # Scrapy stuff:
64 | .scrapy
65 |
66 | # Sphinx documentation
67 | docs/_build/
68 |
69 | # PyBuilder
70 | target/
71 |
72 | # Jupyter Notebook
73 | .ipynb_checkpoints
74 |
75 | # pyenv
76 | .python-version
77 |
78 | # celery beat schedule file
79 | celerybeat-schedule
80 |
81 | # SageMath parsed files
82 | *.sage.py
83 |
84 | # Environments
85 | .env
86 | .venv
87 | env/
88 | venv/
89 | ENV/
90 | env.bak/
91 | venv.bak/
92 |
93 | # Spyder project settings
94 | .spyderproject
95 | .spyproject
96 |
97 | # Rope project settings
98 | .ropeproject
99 |
100 | # mkdocs documentation
101 | /site
102 |
103 | # mypy
104 | .mypy_cache/
105 |
106 | # .idea
107 | .idea
108 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # Genetics
3 | In this assignment, you will implement main parts of genetic algorithms.
4 | There are lots of algorithms that researchers use in each part and your are going to implement the famous ones. And of course there are some rare ideas to implement here(the harder you work the better grade you get)!
5 |
6 |
7 | ### Implementations
8 | 1. First of all, you should implement everything **only using numpy or built-in libraries**.
9 | 2. You should only implement **functions** to do the task for each module. This means that you have to assume some predefined code and implement your own code,using that.
10 | 3. You can define sub functions for simplifying your task.
11 | 3. We've provided a **sample class of chromosomes** with some attributes. You can assume that this class will be parsed as the chromosome in your functions.
12 | 4. In some of the modules, **you need to change the class** to add some new attributes. To do so, you should **extend the class and add new attributes** to your new implemented class.
13 |
14 | ### Grading
15 | 1. **Modules have weighted grades** so the more work you put in the more points you get.
16 | 2. **Some modules are optional for some groups**.
17 | 3. **Groups are categorized based on theirs skills**. So the more skilled you are the more tasks and different points for each task you get.
18 | 4. (optional) **Unit testing** has bonus points. If you use online CI tools like Travis CI or Azure DevOps for testing, you'll get more bonus points!!!
19 |
20 | Note: If you want to use unit testing in your codes, please provide high quality tests. writing low quality test is just a waste time (Go play GTA instead!)
21 |
22 | ### Documentation
23 | 1. For god's sake write something! maybe someday you have to use your own project(hopefully).
24 | 2. You have to provide documentation for all of the function **arguments** and **return values** and of course a **simple explanation about your function** and a **numerical example** of how would your function work.
25 | 3.Don't know how to write docs in python? OK, google it!
26 |
27 | This tutorial may help you to write better docs.
28 |
29 | ### Modules
30 | 1. we've provided some modules for each section.
31 | 2. Each **module will be a function** as described in implementation section.
32 | 3. There is a **little description** on what each module is and some **links as reference**. You must read those links and implement modules based on algorithms provided in the references. In some modules, we've provided **a paper,related to the module**. It is your task to **study the papers and find out how to implement the modules**.
33 |
34 | ## Genetics
35 | This assignment includes these parts:
36 | 1. Initialization (Population and Chromosome)
37 | 2. Selection Methods
38 | 3. Crossover Methods
39 | 4. Mutation Methods
40 |
41 | We will describe each section later on
42 |
43 | ## Initialization
44 | In this step we talk about initializing chromosomes and population.
45 | So here are the contents:
46 | 1. Chromosome
47 | 2. Population
48 |
49 | ### Chromosome
50 | Here we assume that every problem can be encoded to chromosomes with 1 dimensional vector genes.
51 | But the structure of this genes depends on the problem. We describe most famous ones here so we have 3 types of gene vectors:
52 | 1. A binary vector in which 1 represents the existence of the genes and 0 represents non-existence.
53 | Numeric example:
54 | ```python
55 | genes = [0, 1, 0, 0, 0, 1, ... , 0]
56 | print(chromosome.genes)
57 | output: [0, 1, 0, 0, 0, 1, ... , 0]
58 | ```
59 |
60 | 2. An integer vector of vectors which each element represents a vector of the batch of the genes with the same size. something like a flatten 2 dimensional matrix.
61 | ```python
62 | genes = [
63 | [1,2,3],
64 | [2,1,3],
65 | [3,2,1],
66 | [3,1,2]
67 | ]
68 | chromosome.genes = genes.flatten()
69 | print(chromosome.genes)
70 | output: [1,2,3,2,1,3,3,2,1,3,1,2]
71 | ```
72 |
73 | 3.An integer vector of vectors like state 2, but each element has different size. So this is your duty to think about the best encoding way for it.
74 | ```python
75 | genes = [
76 | [1,2,3,4,5],
77 | [3,2,1],
78 | [2,3,1,4],
79 | [1]
80 | ]
81 | chromosome.genes = genes.flatten()
82 | print(chromosome.genes)
83 | output: [1,2,3,4,5,3,2,1,2,3,1,4,1]
84 | ```
85 | `lengths` is important. So we add another attribute to the `chromosome` class.
86 | ```python
87 | chromosome.lengths = [5,3,4,1]
88 | ```
89 | So every time you want to apply any operation on these features, you have to do it with respect to `lengths`.
90 |
91 | **Note: These numbers are just for demonstration purposes and in real world example, they are data points (features).**
92 |
93 | We've provided a class representing a simple chromosome. You need to complete all the functions with respect to this class.
94 |
95 |
96 | ```python
97 | import numpy as np
98 | ```
99 |
100 |
101 | ```python
102 | class chromosome():
103 | def __init__(self, genes, id=None, fitness=-1, flatten= False, lengths=None):
104 | self.id = id
105 | self.genes = genes
106 | self.fitness = fitness
107 |
108 | # if you need more parameters for specific problem, extend this class
109 | def flatten_feaures():
110 | # in this function you should implement your logic to flatten features
111 | pass
112 |
113 |
114 | def describe(self):
115 | print('ID=#{}, fitenss={}, \ngenes=\n{}'.format(self.id, self.fitness, self.genes))
116 | ```
117 |
118 |
119 | ```python
120 | c = chromosome(genes=np.array([1,2,3]),id=1,fitness = 125.2)
121 | c.describe()
122 |
123 | c2 = chromosome(genes = np.array([[1,2],[2,1]]).flatten(), id=2,
124 | fitness=140, flatten=True)
125 | c2.describe()
126 | ```
127 |
128 | ID=#1, fitenss=125.2,
129 | genes=
130 | [1 2 3]
131 | ID=#2, fitenss=140,
132 | genes=
133 | [1 2 2 1]
134 |
135 |
136 | ### Population
137 | In the population,chromosomes are like friends and enemies. So all operators in mutation and crossover will be applied on these chromosomes based on nature selection ideas and survival of the fittest.
138 | Initialization can help you to find global optima faster but can even get you stuck in the local optima on the other hand. Hence, it is an important part.
139 | There are some methods to initialize population and they are hyperparameters.
140 | **Hyperparameters** are:
141 | 1. **Population Size**
142 | 2. **Chromosome Genes**
143 |
144 | The most famous one is random initialization.
145 |
146 | **Population** initialization:
147 | 1. **Pseudo Random**
148 | 2. **Quasi Random Sequence**
149 | 3. **Centroid Voronoi Tessellation**
150 |
151 | #### Pseudo Random
152 | This is the simplest method. Actually the `random` class in every programming language is a pseudo random number. And of course you can make a uniform random using this pseudo randoms.
153 | So in your implementation, you must use `numpy.random` to initialize population and chromosomes.
154 | Just do everything random! If you need more information, check more reading section below.
155 |
156 | #### Quasi Random Sequence
157 | the initial population is often selected randomly using pseudo random numbers.It’s usually more important that the points are as **evenly distributed** as possible than that they imitate random points. In this paper, we study the use of quasi-random sequences in the initial population of a genetic algorithm. Sample points in a **quasi-random sequence are designed to have good distribution properties**.
158 | Use this link as reference.
159 |
160 |
161 | #### Centroid Voronoi Tessellation
162 | In this method, we consider data points as clusters and will separate them based on some criteria. And then initialize random points with respect to these regions.
163 | Use this paper as reference.
164 |
165 | ##### More Reading
166 | If you need to know more about random numbers, this and this about different methods.
167 |
168 | ## Selection
169 | The main purpose of selection phase is to select the fittest individuals and let them pass their genes to the next generation.
170 |
171 | These are your tasks:
172 |
173 | 1. **Truncation Selection**
174 | 2. **Tournament Selection**
175 | 3. **stochastic Universal Sampling**
176 | 4. **Roulette Wheel Selection**
177 | 5. **Roulette Wheel Selection with stochastic Acceptance**
178 | 6. **Linear Ranking Selection**
179 | 7. **Exponential Ranking Selection**
180 | 8. **Self-Adaptive Tournament Selection**
181 |
182 | ### 1. Truncation Selection
183 | Use This as reference.
184 | More reading:
185 | Muhlenbein's Breeder Genetic Algorithm.
186 |
187 | ### 2. Tournament Selection
188 | Use this as reference.
189 |
190 | ### 3. Stockasting Universal Sampling
191 | Use this as reference.
192 |
193 | ### 4. Roulette-wheel Selection
194 | Use this as reference.
195 |
196 | ### 5. Roulette-wheel Selection with Stocastic Acceptance
197 | Use this as reference.
198 |
199 | ### 6. Linear Ranking Selection
200 | Use this and this as reference.
201 |
202 | ### 7. Exponential Ranking Selection
203 | Use this and this as reference.
204 |
205 | ### 8. Self-Adaptive Tournament Selection
206 | Use this as reference.
207 |
208 | #### More readings
209 | 1. https://1drv.ms/b/s!ApJ0ieVzUhjim0AVEP9zJ-irxfVN
210 | 2. http://www.geatbx.com/docu/algindex-02.html
211 |
212 | ## Crossover
213 | The idea is to have better individuals at the next generations. So we have to do something. Here we try to use top individuals offspring for next generations as parents. This help us to exploit.
214 |
215 | Here is some of crossover operators:
216 | 1. **Single-point Crossover**
217 | 2. **Multi-point Crossover (N-point)**
218 | 3. **Uniform Crossover**
219 | 4. **Flat Crossover**
220 | 5. **Order Crossover (OX)**
221 | 6. **Partially Mapped Crossover(PMX)**
222 | 7. **Edge Recombination Crossover (ERX)**
223 | 8. **Cycle Crossover (CX)**
224 | 9. **Alternating Edges Crossover (AEX)**
225 |
226 | **Note: You must implement each operator for 3 types of chromosome classes**. (See Initialization section).
227 |
228 | Reference:
229 | 1. For 1 to 4 oeprators, use this link.
230 | 2. For 5 to 9 operators use this link.
231 |
232 | ### 1. Single-Point crossover
233 |
234 | ### 2. Multi-point Crossover (N-point)
235 |
236 | ### 3. Uniform Crossover
237 |
238 | ### 4. Flat Crossover
239 |
240 | ### 5. Order Crossover (OX)
241 |
242 | ### 6. Partially Mapped Crossover(PMX)
243 |
244 | ### 7. Edge Recombination Crossover (ERX)
245 |
246 | ### 8. Cycle Crossover (CX)
247 |
248 | ### 9. Alternating Edges Crossover (AEX)
249 |
250 | ## Mutation
251 | The main goal of mutation phase is to change the values of genes in a chromosome randomly and introduce new genetic material into the population,to increase genetic diversity.
252 |
253 | These are your tasks:
254 |
255 | 1. **Uniform/Random Mutation**
256 | 2. **Inorder Mutation**
257 | 3. **Twors Mutation**
258 | 4. **Centre inverse mutation (CIM)**
259 | 5. **Reverse Sequence Mutation (RSM)**
260 | 6. **Throas Mutation**
261 | 7. **Thrors Mutation**
262 | 8. **Partial Shuffle Mutation (PSM)**
263 | 9. **Scrabmle Mutation**
264 | 10. **Distance-Based Mutation Operator (DMO)**
265 |
266 | The reference for 9/10 of these operators here.
267 |
268 | ### 1. Uniform/Random Mutation
269 |
270 | ### 2. Inorder Mutation
271 |
272 | ### 3. Twors Mutation
273 |
274 | ### 4. Centre Inverse Mutation (CIM)
275 |
276 |
277 | ### 5. Reverse Sequence Mutation (RSM)
278 |
279 | ### 6. Throas Mutation
280 |
281 | ### 7. Thrors Mutation
282 |
283 | ### 8. Partial Shuffle Mutation (PSM)
284 |
285 | ### 9. Scramble Mutation
286 | Use this link.
287 |
288 | ### 10. Distance-Based Mutation Operator (DMO)
289 | You can get paper here.
290 |
291 |
292 |
293 | # License
294 | MIT License
295 |
296 | Copyright (c) 2018 Computational Intelligence - University of Guilan
297 |
298 | Permission is hereby granted, free of charge, to any person obtaining a copy
299 | of this software and associated documentation files (the "Software"), to deal
300 | in the Software without restriction, including without limitation the rights
301 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
302 | copies of the Software, and to permit persons to whom the Software is
303 | furnished to do so, subject to the following conditions:
304 |
305 | The above copyright notice and this permission notice shall be included in all
306 | copies or substantial portions of the Software.
307 |
308 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
309 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
310 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
311 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
312 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
313 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
314 | SOFTWARE.
315 | Get in touch with Authors: Mohammad Doosti Lakhani, Hamed Faraji
316 |
--------------------------------------------------------------------------------
/Assignment 1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Genetics\n",
8 | "In this assignment, you will implement main parts of genetic algorithms.
\n",
9 | "There is a lot of algorithms researchers use in each part and your are going to implement the famous ones. And of course there are some new ideas to implement here(the harder you work the better grade you get)!\n",
10 | "
\n",
11 | "\n",
12 | "### Implementations\n",
13 | "1. First of all, you should implement everything **only using numpy or built-in libraries**.\n",
14 | "2. You should only implement **functions** to do the task for each module. This means, you have to assume some predefined code and you implementd your code,using that.\n",
15 | "3. You can define sub functions for simplifying your task.\n",
16 | "3. we've provided a **sample class of chromosome** with some attributes. You can assume that this class will be parsed as the chromosome in your functions. \n",
17 | "4. In some of modules, **you need to change the class** to add some new attribute. To do so, you should **extend class and add new attributes** to your new implemented class.\n",
18 | "\n",
19 | "### Grading\n",
20 | "1. **Modules have weighted grades** so the more work you put in the more points you get.\n",
21 | "2. **Some modules are optional for some groups**, implementing them will have bonus points with respect to the it's weight.\n",
22 | "3. **Groups are categorized based on theirs skills**. So the more skilled you are the more tasks and different points for each task you get..\n",
23 | "4. (optional) **Unit testing** has bonus points. If you use online CI tools like Travis CI or Azure DevOps for testing, you'll get more bonus points!!!\n",
24 | "\n",
25 | "**Note**: If you want to use unit testing in your codes, please provide high quality tests. writing low quality test is just a waste time (Go play GTA instead!)\n",
26 | "\n",
27 | "### Documentation\n",
28 | "1. For god's sake write something! maybe someday you have to use your own project(hopefully).\n",
29 | "2. You have to provide documentation for all of the function **arguments** and **return value** and of course a **simple explanation about your function** and a **numerical example** of how would your function work.\n",
30 | "3.Don't know how to write docs in python? OK,google it!\n",
31 | "\n",
32 | "This tutorial may help you to write better docs.\n",
33 | "\n",
34 | "### Modules\n",
35 | "1. We've provided some modules for each section.\n",
36 | "2. Each **module will be a function** as described in **implementation** section.\n",
37 | "3. There is a **little description** on what each module is and some **links as reference**. You must read those links and implement modules based on algorithms provided in the references. In some modules, we've provided **a paper,related to the module**. It is your task to **study the papers and find out how to implement the modules**.\n",
38 | "4. Your functions should work for all type of chromosome class(flattened or not). So you have to pass a mendatory argument to determine **what type of choromosome you have**."
39 | ]
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "metadata": {},
44 | "source": [
45 | "## What is Weighted Grades?\n",
46 | "Each team has a value of ('A'=high, B='normal', C='low'). And each training (sub module) also has a value same as teams.
\n",
47 | "And we decide how much points assign to each module based on some criteria.
\n",
48 | "**Each team should complete trainings equal and lower.**
\n",
49 | "Example:
\n",
50 | "\n",
51 | "modules:\n",
52 | "1. m1 A\n",
53 | "2. m2 A\n",
54 | "3. m3 B\n",
55 | "4. m4 B\n",
56 | "5. m5 C\n",
57 | "\n",
58 | "teams:\n",
59 | "1. t1 A\n",
60 | "2. t2 B\n",
61 | "3. t3 C\n",
62 | "\n",
63 | "Assigned tasks:\n",
64 | "1. t1 = (m1, m2, m3, m4, m5)\n",
65 | "2. t2 = (m3, m4, m5)\n",
66 | "3. t3 = (m5)\n",
67 | "\n",
68 | "\n",
69 | "
\n",
70 | "**Note**: You are allowed to use other resources but only documents and examples, **Not codes**. And you can pass those example as test to you function and check it that your function is correct or not."
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "## HOW TO START\n",
78 | "1. Fork repository using your github account. (A member of each team is enough.)\n",
79 | "2. Implement your tasks\n",
80 | "3. Push codes before deadline\n",
81 | "4. We will review your submitted codes\n",
82 | "5. To best teams, we pull their code as pull request and build other tasks top of it.\n",
83 | "\n",
84 | "
\n",
85 | "Actually task 1 to 3 should be your concern.\n",
86 | "
\n",
87 | "\n",
88 | "**Note**: If you do not have Jupyter notebook, view this notebook using your web browser though github page and implement your tasks on any IDE you want. You should do **all task in a single `.py` file and do not add your implemented codes in jupyter we provided**.
\n",
89 | "**Note: Because github is not private, you should not push your codes before deadline, but if you like to using Git Version Control system, use Bitbucket or Microsoft Azure Devops.
\n",
90 | "Both of these services are free to use (lifetime) and easy to setup. So please use them, they makes your life much more easier in developing!**
\n",
91 | "And it is easy to transfer your project from this systems to github.
"
92 | ]
93 | },
94 | {
95 | "cell_type": "markdown",
96 | "metadata": {},
97 | "source": [
98 | "## F.A.Q\n",
99 | "#### Questions:\n",
100 | "Ask on Quera forum or TAs (Hamed Faraji, Mohammad Doosti Lakhani) on office days (we prefer Quera)\n",
101 | "#### Problems with documentation or links or etc?\n",
102 | "Add a issue in github page or comment on Quera forum.\n"
103 | ]
104 | },
105 | {
106 | "cell_type": "markdown",
107 | "metadata": {},
108 | "source": [
109 | "## Genetics\n",
110 | "This assignment includes these parts:\n",
111 | "1. Initialization (Population and Chromosome)\n",
112 | "2. Selection Methods\n",
113 | "3. Crossover Methods\n",
114 | "4. Mutation Methods\n",
115 | "\n",
116 | "We will describe each section later on"
117 | ]
118 | },
119 | {
120 | "cell_type": "markdown",
121 | "metadata": {},
122 | "source": [
123 | "## Initialization\n",
124 | "In this step we talk about initializing chromosomes and population.\n",
125 | "So here is the contents:\n",
126 | "1. Chromosome\n",
127 | "2. Population"
128 | ]
129 | },
130 | {
131 | "cell_type": "markdown",
132 | "metadata": {},
133 | "source": [
134 | "### Chromosome\n",
135 | "Here we assume that every problem can be encoded to chromosomes with 1 dimensional vector genes.
\n",
136 | "But the structure of this genes depends on the problem. We describe most famous ones here so we have 3 types of gene vectors:\n",
137 | "1. C A binary vector in which 1 represents the existence of the genes and 0 represents non-existence.
\n",
138 | "Numeric example:
\n",
139 | "```python\n",
140 | "genes = [0, 1, 0, 0, 0, 1, ... , 0]\n",
141 | "print(chromosome.genes)\n",
142 | "output: [0, 1, 0, 0, 0, 1, ... , 0]\n",
143 | "```\n",
144 | "\n",
145 | "2. C An integer vector of vectors which each element represents a vector of the batch of the genes with the same size. something like a flatten 2 dimensional matrix.
\n",
146 | "```python\n",
147 | "genes = [\n",
148 | " [1,2,3],\n",
149 | " [2,1,3],\n",
150 | " [3,2,1],\n",
151 | " [3,1,2]\n",
152 | " ]\n",
153 | "chromosome.genes = genes.flatten()\n",
154 | "print(chromosome.genes)\n",
155 | "output: [1,2,3,2,1,3,3,2,1,3,1,2]\n",
156 | "```\n",
157 | "\n",
158 | "3. B An integer vector of vectors like state 2, but each element has different size. So this is your duty to think about the best encoding way for it.\n",
159 | "```python\n",
160 | "genes = [\n",
161 | " [1,2,3,4,5],\n",
162 | " [3,2,1],\n",
163 | " [2,3,1,4],\n",
164 | " [1]\n",
165 | " ]\n",
166 | "chromosome.genes = genes.flatten()\n",
167 | "print(chromosome.genes)\n",
168 | "output: [1,2,3,4,5,3,2,1,2,3,1,4,1]\n",
169 | "```\n",
170 | "`lengths` is important. So we add another attribute to the `chromosome` class.
\n",
171 | "```python\n",
172 | "chromosome.lengths = [5,3,4,1]\n",
173 | "```\n",
174 | "So every time you want to apply any operation on these features, you have to do it with respect to `lengths`.
\n",
175 | "\n",
176 | "**Note: These numbers are just for demonestration purposes and in real world example, they are data points (features).**"
177 | ]
178 | },
179 | {
180 | "cell_type": "markdown",
181 | "metadata": {},
182 | "source": [
183 | "We've provided a class representing a simple chromosome. You need to complete all the functions with respect to this class."
184 | ]
185 | },
186 | {
187 | "cell_type": "code",
188 | "execution_count": 4,
189 | "metadata": {},
190 | "outputs": [],
191 | "source": [
192 | "# Just run this column, dont change seed value\n",
193 | "import numpy as np\n",
194 | "np.random.seed(75)"
195 | ]
196 | },
197 | {
198 | "cell_type": "code",
199 | "execution_count": 5,
200 | "metadata": {},
201 | "outputs": [],
202 | "source": [
203 | "class chromosome():\n",
204 | " def __init__(self, genes, id=None, fitness=-1, flatten= False, lengths=None):\n",
205 | " self.id = id\n",
206 | " self.genes = genes\n",
207 | " self.fitness = fitness\n",
208 | " \n",
209 | " # if you need more parameters for specific problem, extend this class\n",
210 | " def flatten_feaures():\n",
211 | " # in this function you should implement your logic to flatten features\n",
212 | " pass\n",
213 | " \n",
214 | " \n",
215 | " def describe(self):\n",
216 | " print('ID=#{}, fitenss={}, \\ngenes=\\n{}'.format(self.id, self.fitness, self.genes))"
217 | ]
218 | },
219 | {
220 | "cell_type": "code",
221 | "execution_count": 6,
222 | "metadata": {
223 | "scrolled": true
224 | },
225 | "outputs": [
226 | {
227 | "name": "stdout",
228 | "output_type": "stream",
229 | "text": [
230 | "ID=#1, fitenss=125.2, \n",
231 | "genes=\n",
232 | "[1 2 3]\n",
233 | "ID=#2, fitenss=140, \n",
234 | "genes=\n",
235 | "[1 2 2 1]\n"
236 | ]
237 | }
238 | ],
239 | "source": [
240 | "# Provide some example if you want!\n",
241 | "c = chromosome(genes=np.array([1,2,3]),id=1,fitness = 125.2)\n",
242 | "c.describe()\n",
243 | "\n",
244 | "c2 = chromosome(genes = np.array([[1,2],[2,1]]).flatten(), id=2,\n",
245 | " fitness=140, flatten=True)\n",
246 | "c2.describe()"
247 | ]
248 | },
249 | {
250 | "cell_type": "markdown",
251 | "metadata": {},
252 | "source": [
253 | "### Population\n",
254 | "In the population,chromosomes are like friends and enemies. So all operators in mutation and crossover will be applied on these chromosomes based on nature selection ideas and survival of the fittest.
\n",
255 | "Initialization can help you to find global optima faster but can even get you stuck in the local optima on the other hand.Hence, it is an important part.
\n",
256 | "There are some methods to initialize population and they are hyperparameters.
\n",
257 | "Hyperparameters are \n",
258 | "1. Population Size\n",
259 | "2. Chromosome Genes\n",
260 | "\n",
261 | "The most famous one is random initialization.
\n",
262 | "\n",
263 | "Population initialization:\n",
264 | "1. Pseudo Random\n",
265 | "2. Quasi Random Sequence\n",
266 | "3. Centroid Voronoi Tessellation"
267 | ]
268 | },
269 | {
270 | "cell_type": "markdown",
271 | "metadata": {},
272 | "source": [
273 | "#### C Pseudo Random\n",
274 | "This is the simplest method. Actually the `random` class in every programming language is a pseudo random number. And of course you can make a uniform random using this pseudo randoms.
\n",
275 | "So in you implementation, you must use `numpy.random` to initialize population and chromosomes.
\n",
276 | "Just do everything random! If you need more information, check more reading section below."
277 | ]
278 | },
279 | {
280 | "cell_type": "code",
281 | "execution_count": null,
282 | "metadata": {},
283 | "outputs": [],
284 | "source": []
285 | },
286 | {
287 | "cell_type": "markdown",
288 | "metadata": {},
289 | "source": [
290 | "#### A Quasi Random Sequence\n",
291 | "the initial population is often selected randomly using pseudorandom numbers.It’s usually more important that the points are as **evenly distributed** as possible than that they imitate random points. In this paper, we study the use of quasi-random sequences in the initial population of a genetic algorithm. Sample points in a **quasi-random sequence are designed to have good distribution properties**.
\n",
292 | "Use this link as reference.\n",
293 | "
"
294 | ]
295 | },
296 | {
297 | "cell_type": "code",
298 | "execution_count": null,
299 | "metadata": {},
300 | "outputs": [],
301 | "source": []
302 | },
303 | {
304 | "cell_type": "markdown",
305 | "metadata": {},
306 | "source": [
307 | "#### B Centroid Voronoi Tessellation\n",
308 | "In this method, we consider data points as clusters and will separate them based on some criteria. And then initialize random points with respect to these regions.
\n",
309 | "Use this paper as reference.
"
310 | ]
311 | },
312 | {
313 | "cell_type": "code",
314 | "execution_count": null,
315 | "metadata": {},
316 | "outputs": [],
317 | "source": []
318 | },
319 | {
320 | "cell_type": "markdown",
321 | "metadata": {},
322 | "source": [
323 | "##### More Reading\n",
324 | "If you need to more about random numbers, this and this about different methods."
325 | ]
326 | },
327 | {
328 | "cell_type": "markdown",
329 | "metadata": {},
330 | "source": [
331 | "## Selection\n",
332 | "The main purpose of selection phase is to select the fittest individuals and let them pass their genes to the next generation.\n",
333 | "\n",
334 | "These are your tasks:\n",
335 | "\n",
336 | "1. **Truncation Selection**\n",
337 | "2. **Tournament Selection**\n",
338 | "3. **Stochastic Universal Sampling**\n",
339 | "4. **Roulette Wheel Selection**\n",
340 | "5. **Roulette Wheel Selection with stochastic Acceptance**\n",
341 | "6. **Linear Ranking Selection**\n",
342 | "7. **Exponential Ranking Selection**\n",
343 | "8. **Self-Adaptive Tournament Selection**"
344 | ]
345 | },
346 | {
347 | "cell_type": "markdown",
348 | "metadata": {},
349 | "source": [
350 | "### 1. B Truncation Selection\n",
351 | "Use This as reference.
\n",
352 | "More reading:\n",
353 | "Muhlenbein's Breeder Genetic Algorithm."
354 | ]
355 | },
356 | {
357 | "cell_type": "code",
358 | "execution_count": null,
359 | "metadata": {},
360 | "outputs": [],
361 | "source": []
362 | },
363 | {
364 | "cell_type": "markdown",
365 | "metadata": {},
366 | "source": [
367 | "### 2. C Tournament Selection\n",
368 | "Use this as reference."
369 | ]
370 | },
371 | {
372 | "cell_type": "code",
373 | "execution_count": 1,
374 | "metadata": {},
375 | "outputs": [],
376 | "source": []
377 | },
378 | {
379 | "cell_type": "markdown",
380 | "metadata": {},
381 | "source": [
382 | "### 3. C Stockasting Universal Sampling\n",
383 | "Use this as reference."
384 | ]
385 | },
386 | {
387 | "cell_type": "code",
388 | "execution_count": null,
389 | "metadata": {},
390 | "outputs": [],
391 | "source": []
392 | },
393 | {
394 | "cell_type": "markdown",
395 | "metadata": {},
396 | "source": [
397 | "### 4. C Roulette-wheel Selection\n",
398 | "Use this as reference."
399 | ]
400 | },
401 | {
402 | "cell_type": "code",
403 | "execution_count": null,
404 | "metadata": {},
405 | "outputs": [],
406 | "source": []
407 | },
408 | {
409 | "cell_type": "markdown",
410 | "metadata": {},
411 | "source": [
412 | "### 5. B Roulette-wheel Selection with Stocastic Acceptance\n",
413 | "Use this as reference."
414 | ]
415 | },
416 | {
417 | "cell_type": "code",
418 | "execution_count": null,
419 | "metadata": {},
420 | "outputs": [],
421 | "source": []
422 | },
423 | {
424 | "cell_type": "markdown",
425 | "metadata": {},
426 | "source": [
427 | "### 6. C Linear Ranking Selection\n",
428 | "Use this and this as reference."
429 | ]
430 | },
431 | {
432 | "cell_type": "code",
433 | "execution_count": null,
434 | "metadata": {},
435 | "outputs": [],
436 | "source": []
437 | },
438 | {
439 | "cell_type": "markdown",
440 | "metadata": {},
441 | "source": [
442 | "### 7. B Exponential Ranking Selection\n",
443 | "Use this and this as reference."
444 | ]
445 | },
446 | {
447 | "cell_type": "code",
448 | "execution_count": null,
449 | "metadata": {},
450 | "outputs": [],
451 | "source": []
452 | },
453 | {
454 | "cell_type": "markdown",
455 | "metadata": {},
456 | "source": [
457 | "### 8. A Self-Adaptive Tournament Selection\n",
458 | "Use this as reference."
459 | ]
460 | },
461 | {
462 | "cell_type": "code",
463 | "execution_count": null,
464 | "metadata": {},
465 | "outputs": [],
466 | "source": []
467 | },
468 | {
469 | "cell_type": "markdown",
470 | "metadata": {},
471 | "source": [
472 | "#### More readings\n",
473 | "1. https://1drv.ms/b/s!ApJ0ieVzUhjim0AVEP9zJ-irxfVN\n",
474 | "2. http://www.geatbx.com/docu/algindex-02.html"
475 | ]
476 | },
477 | {
478 | "cell_type": "markdown",
479 | "metadata": {},
480 | "source": [
481 | "## Crossover\n",
482 | "The idea is to have better individuals at next generations. So we have to do something. Here we try to use top indviduals offsprings for next generations as parents. This help us exploit.\n",
483 | "\n",
484 | "Here is some of crossover operators:\n",
485 | "1. Single-point Crossover\n",
486 | "2. Multi-point Crossover (N-point)\n",
487 | "3. Uniform Crossover\n",
488 | "4. Flat Crossover\n",
489 | "5. Order Crossover (OX)\n",
490 | "6. Partially Mapped Crossover(PMX)\n",
491 | "7. Edge Recombination Crossover (ERX)\n",
492 | "8. Cycle Crossover (CX)\n",
493 | "9. Alternating Edges Crossover (AEX)\n",
494 | "\n",
495 | "**Note: You must implement each operator for 3 types of chromosome classes**. (See Initialization section)."
496 | ]
497 | },
498 | {
499 | "cell_type": "markdown",
500 | "metadata": {},
501 | "source": [
502 | "Reference:\n",
503 | "1. For 1 to 4 oeprators, use this link.\n",
504 | "2. For 5 to 9 operators use this link."
505 | ]
506 | },
507 | {
508 | "cell_type": "markdown",
509 | "metadata": {},
510 | "source": [
511 | "### C 1. Single-Point crossover"
512 | ]
513 | },
514 | {
515 | "cell_type": "code",
516 | "execution_count": null,
517 | "metadata": {},
518 | "outputs": [],
519 | "source": []
520 | },
521 | {
522 | "cell_type": "markdown",
523 | "metadata": {},
524 | "source": [
525 | "### B 2. Multi-point Crossover (N-point)"
526 | ]
527 | },
528 | {
529 | "cell_type": "code",
530 | "execution_count": null,
531 | "metadata": {},
532 | "outputs": [],
533 | "source": []
534 | },
535 | {
536 | "cell_type": "markdown",
537 | "metadata": {},
538 | "source": [
539 | "### C 3. Uniform Crossover"
540 | ]
541 | },
542 | {
543 | "cell_type": "code",
544 | "execution_count": null,
545 | "metadata": {},
546 | "outputs": [],
547 | "source": []
548 | },
549 | {
550 | "cell_type": "markdown",
551 | "metadata": {},
552 | "source": [
553 | "### C 4. Flat Crossover"
554 | ]
555 | },
556 | {
557 | "cell_type": "code",
558 | "execution_count": null,
559 | "metadata": {},
560 | "outputs": [],
561 | "source": []
562 | },
563 | {
564 | "cell_type": "markdown",
565 | "metadata": {},
566 | "source": [
567 | "### B 5. Order Crossover (OX)"
568 | ]
569 | },
570 | {
571 | "cell_type": "code",
572 | "execution_count": null,
573 | "metadata": {},
574 | "outputs": [],
575 | "source": []
576 | },
577 | {
578 | "cell_type": "markdown",
579 | "metadata": {},
580 | "source": [
581 | "### C 6. Partially Mapped Crossover(PMX)"
582 | ]
583 | },
584 | {
585 | "cell_type": "code",
586 | "execution_count": null,
587 | "metadata": {},
588 | "outputs": [],
589 | "source": []
590 | },
591 | {
592 | "cell_type": "markdown",
593 | "metadata": {},
594 | "source": [
595 | "### B 7. Edge Recombination Crossover (ERX)"
596 | ]
597 | },
598 | {
599 | "cell_type": "code",
600 | "execution_count": null,
601 | "metadata": {},
602 | "outputs": [],
603 | "source": []
604 | },
605 | {
606 | "cell_type": "markdown",
607 | "metadata": {},
608 | "source": [
609 | "### A 8. Cycle Crossover (CX)"
610 | ]
611 | },
612 | {
613 | "cell_type": "code",
614 | "execution_count": null,
615 | "metadata": {},
616 | "outputs": [],
617 | "source": []
618 | },
619 | {
620 | "cell_type": "markdown",
621 | "metadata": {},
622 | "source": [
623 | "### A 9. Alternating Edges Crossover (AEX)"
624 | ]
625 | },
626 | {
627 | "cell_type": "code",
628 | "execution_count": null,
629 | "metadata": {},
630 | "outputs": [],
631 | "source": []
632 | },
633 | {
634 | "cell_type": "markdown",
635 | "metadata": {},
636 | "source": [
637 | "## Mutation\n",
638 | "The main goal of mutation phase is to change the values of genes in a chromosome randomly and introduce new genetic material into\n",
639 | "the population,to increase genetic diversity.\n",
640 | "\n",
641 | "These are your tasks:\n",
642 | "\n",
643 | "1. **Uniform/Random Mutation**\n",
644 | "2. **Inorder Mutation**\n",
645 | "3. **Twors Mutation**\n",
646 | "4. **Centre Inverse Mutation (CIM)**\n",
647 | "5. **Reverse Sequence Mutation (RSM)**\n",
648 | "6. **Throas Mutation**\n",
649 | "7. **Thrors Mutation**\n",
650 | "8. **Partial Shuffle Mutation (PSM)**\n",
651 | "9. **Scramble Mutation**\n",
652 | "10. **Distance-Based Mutation Operator (DMO)**"
653 | ]
654 | },
655 | {
656 | "cell_type": "markdown",
657 | "metadata": {},
658 | "source": [
659 | "Here is a reference for 9/10 of these operators here .\n",
660 | "**you'll need more search of course.**"
661 | ]
662 | },
663 | {
664 | "cell_type": "markdown",
665 | "metadata": {},
666 | "source": [
667 | "### C 1. Uniform/Random Mutation"
668 | ]
669 | },
670 | {
671 | "cell_type": "code",
672 | "execution_count": null,
673 | "metadata": {},
674 | "outputs": [],
675 | "source": []
676 | },
677 | {
678 | "cell_type": "markdown",
679 | "metadata": {},
680 | "source": [
681 | "### C 2. Inorder Mutation"
682 | ]
683 | },
684 | {
685 | "cell_type": "code",
686 | "execution_count": null,
687 | "metadata": {},
688 | "outputs": [],
689 | "source": []
690 | },
691 | {
692 | "cell_type": "markdown",
693 | "metadata": {},
694 | "source": [
695 | "### C 3. Twors Mutation"
696 | ]
697 | },
698 | {
699 | "cell_type": "code",
700 | "execution_count": null,
701 | "metadata": {},
702 | "outputs": [],
703 | "source": []
704 | },
705 | {
706 | "cell_type": "markdown",
707 | "metadata": {},
708 | "source": [
709 | "### B 4. Centre Inverse Mutation (CIM)\n"
710 | ]
711 | },
712 | {
713 | "cell_type": "code",
714 | "execution_count": null,
715 | "metadata": {},
716 | "outputs": [],
717 | "source": []
718 | },
719 | {
720 | "cell_type": "markdown",
721 | "metadata": {},
722 | "source": [
723 | "### B 5. Reverse Sequence Mutation (RSM)"
724 | ]
725 | },
726 | {
727 | "cell_type": "code",
728 | "execution_count": null,
729 | "metadata": {},
730 | "outputs": [],
731 | "source": []
732 | },
733 | {
734 | "cell_type": "markdown",
735 | "metadata": {},
736 | "source": [
737 | "### A 6. Throas Mutation"
738 | ]
739 | },
740 | {
741 | "cell_type": "code",
742 | "execution_count": null,
743 | "metadata": {},
744 | "outputs": [],
745 | "source": []
746 | },
747 | {
748 | "cell_type": "markdown",
749 | "metadata": {},
750 | "source": [
751 | "### A 7. Thrors Mutation"
752 | ]
753 | },
754 | {
755 | "cell_type": "code",
756 | "execution_count": null,
757 | "metadata": {},
758 | "outputs": [],
759 | "source": []
760 | },
761 | {
762 | "cell_type": "markdown",
763 | "metadata": {},
764 | "source": [
765 | "### C 8. Partial Shuffle Mutation (PSM)"
766 | ]
767 | },
768 | {
769 | "cell_type": "code",
770 | "execution_count": null,
771 | "metadata": {},
772 | "outputs": [],
773 | "source": []
774 | },
775 | {
776 | "cell_type": "markdown",
777 | "metadata": {},
778 | "source": [
779 | "### C 9. Scramble Mutation\n",
780 | "Use this link."
781 | ]
782 | },
783 | {
784 | "cell_type": "code",
785 | "execution_count": null,
786 | "metadata": {},
787 | "outputs": [],
788 | "source": []
789 | },
790 | {
791 | "cell_type": "markdown",
792 | "metadata": {},
793 | "source": [
794 | "### A 10. Distance-Based Mutation Operator (DMO)\n",
795 | "You can get paper here."
796 | ]
797 | },
798 | {
799 | "cell_type": "code",
800 | "execution_count": null,
801 | "metadata": {},
802 | "outputs": [],
803 | "source": []
804 | },
805 | {
806 | "cell_type": "markdown",
807 | "metadata": {},
808 | "source": [
809 | "Get in touch with Authors: Mohammad Doosti Lakhani, Hamed Faraji"
810 | ]
811 | }
812 | ],
813 | "metadata": {
814 | "kernelspec": {
815 | "display_name": "Python 3",
816 | "language": "python",
817 | "name": "python3"
818 | },
819 | "language_info": {
820 | "codemirror_mode": {
821 | "name": "ipython",
822 | "version": 3
823 | },
824 | "file_extension": ".py",
825 | "mimetype": "text/x-python",
826 | "name": "python",
827 | "nbconvert_exporter": "python",
828 | "pygments_lexer": "ipython3",
829 | "version": "3.6.6"
830 | }
831 | },
832 | "nbformat": 4,
833 | "nbformat_minor": 2
834 | }
835 |
--------------------------------------------------------------------------------