Skip to content

Commit 654567f

Browse files
committed
update comments and readme
1 parent d0244ee commit 654567f

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,41 @@
1-
# N-Queen-GA
2-
Solve N-Queen problem by GA in JavaScript
1+
# N-Queen by Genetic Algorithm
2+
Solve N-Queen problem by Genetic Algorithm (GA) in JavaScript.
3+
4+
## How to execute
5+
1. First install [node.js](https://nodejs.org/en/)
6+
7+
2. Set your GA parameters at [index.js](https://github.com/bezzad/N-Queen-GA/blob/master/index.js):
8+
```
9+
// N , Pop, SR, MR, ReGen, CR
10+
var ga = new GA(200, 500, 10, 50, 10000, 75);
11+
12+
// N: Number of Queens in the N×N Chess Board
13+
// Pop: Population size
14+
// SR: Selection probability %
15+
// MR: Mutation probability %
16+
// ReGen: Regeneration limitation
17+
// CR: Max Chromosomes Convergence Rate %
18+
```
19+
20+
3. Go to root folder and run below code in `Bash` or `CMD` and press Enter: <br>
21+
```
22+
$ node index.js
23+
```
24+
25+
>**Note**
26+
This project independent from any libraries. So don't need to run `npm install`.
27+
28+
<br/><br/>
29+
The GA executed and printed an answer like this:
30+
```
31+
Starting GA ...
32+
generation 100, elite fitness is 2
33+
GA ended due to the best chromosome found :)
34+
35+
Result: {"len":200,"genome":[112,192,199,167,105,183,38,148,85,107,126,78,116,72,160,142,155,162,74,41,49,10,177,137,4,128,80,140,123,59,181,24,120,169,110,13,197,98,81,62,125,96,91,163,90,84,79,75,132,196,9,19,102,67,164,63,48,69,45,193,36,70,46,134,95,130,106,44,157,150,170,119,117,184,1,154,86,17,180,113,30,43,188,143,7,28,173,20,26,194,56,32,175,76,190,153,178,159,2,191,115,187,83,97,64,58,35,89,39,100,5,168,165,131,152,23,189,0,3,182,186,37,141,195,18,172,6,29,21,136,77,8,149,12,60,47,14,73,138,27,114,93,33,179,99,82,103,65,55,176,146,156,34,122,144,118,61,71,133,57,127,147,51,88,108,22,42,16,171,68,11,166,185,129,94,124,174,109,151,101,50,135,121,111,52,54,92,145,158,104,161,139,31,53,66,87,198,25,40,15],"fitness":0}
36+
37+
Generation: 123
38+
duration: 3073 ms
39+
```
40+
41+

ga.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use strict";
2-
var { Random, StopWatch } = require('./util');
2+
const { Random } = require('./util');
33

44
class Chromosome {
55
constructor(n) {
@@ -159,7 +159,7 @@ class GA {
159159
regeneration() {
160160
this.RegenerationCounter++;
161161
if (this.RegenerationCounter % 100 === 0)
162-
console.log("generation", this.RegenerationCounter, "elite fitness", this.Popunation[0].fitness);
162+
console.log("generation", this.RegenerationCounter, ", elite fitness is", this.Popunation[0].fitness);
163163
let newPopulation = [];
164164

165165
// create new chromosomes
@@ -195,16 +195,5 @@ class GA {
195195
return this.Popunation[0]; // Elitest chromosome
196196
}
197197
}
198-
// N , Pop, SR, MR, ReGen, CR
199-
// best practice: GA(200, 500, 30, 60, 10000, 75); 4775ms
200-
// fast practice: GA(200, 500, 10, 50, 10000, 75); 2659ms
201-
// N-Queen O(n^n) | O(n!) == NP-Complex
202-
StopWatch.start();
203-
// ------------------------------------------------------
204-
// N , Pop, SR, MR, ReGen, CR
205-
var ga = new GA(200, 500, 10, 50, 10000, 75);
206-
var result = ga.Start();
207-
console.log("Result:", result);
208-
console.log("Generation:", ga.RegenerationCounter)
209-
// ------------------------------------------------------
210-
StopWatch.stop();
198+
199+
module.exports = GA;

index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { StopWatch } = require('./util');
2+
const GA = require("./ga");
3+
4+
// N , Pop, SR, MR, ReGen, CR
5+
// best practice: GA(200, 500, 30, 60, 10000, 75); 4775ms
6+
// fast practice: GA(200, 500, 10, 50, 10000, 75); 2659ms
7+
// N-Queen O(n^n) | O(n!) == NP-Complex
8+
StopWatch.start();
9+
// ------------------------------------------------------
10+
// N , Pop, SR, MR, ReGen, CR
11+
var ga = new GA(200, 500, 10, 50, 10000, 75);
12+
13+
// N: Number of Queens in the N×N Chess Board
14+
// Pop: Population size
15+
// SR: Selection probability %
16+
// MR: Mutation probability %
17+
// ReGen: Regeneration limitation
18+
// CR: Max Chromosomes Convergence Rate %
19+
20+
var result = ga.Start();
21+
console.log("\nResult:", JSON.stringify(result));
22+
console.log("\nGeneration:", ga.RegenerationCounter)
23+
// ------------------------------------------------------
24+
StopWatch.stop();

0 commit comments

Comments
 (0)