Skip to content

Commit 4fa1df2

Browse files
MohsenMohsen
authored andcommitted
AI formula is extended; new DNA (i) is implemented in Chromosome in order to check the occupation percentage of side or center of board
1 parent 7bb7d24 commit 4fa1df2

File tree

2 files changed

+56
-92
lines changed

2 files changed

+56
-92
lines changed

resources/scripts/genetics.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,23 @@ let HALT_LEARNING = false;
8484
let LEARN_IN_PROGRESS = false;
8585

8686
const GENETICS = {
87-
// aX + bY + cZ + dW + eQ + f + gT + hS = 0
87+
// Formula: aX + bY + cZ + dW + eQ + f + gT + hS + iP = 0
8888
// X: cols integrity (%)
8989
// Y: rows integrity (%)
90-
// X: 9-blocks integrity (%)
90+
// Z: 9-blocks integrity (%)
9191
// W: total occupation (%)
9292
// Q: selected element occupation (%)
9393
// T: row/col/blockSet completeness (%)
9494
// S: board total integrity (%)
95+
// P: priority of sides and corner occupation (%)
9596

9697
population: [], // Array of Chromosomes of { a, b, c, d, e, f, g, h }
9798
populationGameScore: [],
9899
populationAvgSuccessRates: [], // [ { s: successRate, c: ageCount }, ... ]
99100
avgTotalSuccessRate: {}, // { best, worst, count }
100101
currentPopulation: -1,
101102
POPULATION_COUNT: 100,
102-
CHROMOSOME_DNA_COUNT: 8,
103+
CHROMOSOME_DNA_COUNT: 9,
103104
CHROMOSOME_DNA_RANGE: 2100, // -10.00 .. 0.00 .. +10.00
104105

105106
MUTATION_RATE: 0.4,
@@ -131,6 +132,7 @@ const GENETICS = {
131132
f: GENETICS.utils.getRandDNA(),
132133
g: GENETICS.utils.getRandDNA(),
133134
h: GENETICS.utils.getRandDNA(),
135+
i: GENETICS.utils.getRandDNA(),
134136
});
135137

136138
GENETICS.populationGameScore.push(0);
@@ -289,7 +291,8 @@ const GENETICS = {
289291
e: randPosition < 5 ? chromosome1.e : chromosome2.e,
290292
f: randPosition < 6 ? chromosome1.f : chromosome2.f,
291293
g: randPosition < 7 ? chromosome1.g : chromosome2.g,
292-
h: chromosome2.h,
294+
h: randPosition < 8 ? chromosome1.h : chromosome2.h,
295+
i: chromosome2.i,
293296
};
294297
},
295298
getNewChild: function (
@@ -355,6 +358,12 @@ const GENETICS = {
355358
: Math.random() < 0.5
356359
? chromosome1.h
357360
: chromosome2.h,
361+
i:
362+
mutationDnaIndex === 7
363+
? GENETICS.utils.getRandDNA()
364+
: Math.random() < 0.5
365+
? chromosome1.i
366+
: chromosome2.i,
358367
};
359368
},
360369
},
@@ -673,8 +682,10 @@ function getCalculatedValue(element, position, chromosome, boardState) {
673682
chromosome.e * getOccupationOfElement(element) +
674683
chromosome.f +
675684
chromosome.g * PLAY_INFO.statistics.getT(boardStateSimulation) +
676-
chromosome.h * PLAY_INFO.statistics.getS(boardStateSimulation).divValue
685+
chromosome.h * PLAY_INFO.statistics.getS(boardStateSimulation).divValue +
686+
chromosome.i * PLAY_INFO.statistics.getP(boardStateSimulation)
677687
);
688+
678689
}
679690

680691
function simulateBoardState(element, position, boardState) {

resources/scripts/index.js

Lines changed: 40 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -21,98 +21,26 @@ const PLAY_INFO = {
2121
avgScore: 0,
2222
avgScoreList: [],
2323
currentElements: [],
24-
boardState: [
25-
// Block Set No
26-
0,
27-
0,
28-
0,
29-
0,
30-
0,
31-
0,
32-
0,
33-
0,
34-
0, // 0 1 2
35-
0,
36-
0,
37-
0,
38-
0,
39-
0,
40-
0,
41-
0,
42-
0,
43-
0,
44-
0,
45-
0,
46-
0,
47-
0,
48-
0,
49-
0,
50-
0,
51-
0,
52-
0,
53-
54-
0,
55-
0,
56-
0,
57-
0,
58-
0,
59-
0,
60-
0,
61-
0,
62-
0, // 3 4 5
63-
0,
64-
0,
65-
0,
66-
0,
67-
0,
68-
0,
69-
0,
70-
0,
71-
0,
72-
0,
73-
0,
74-
0,
75-
0,
76-
0,
77-
0,
78-
0,
79-
0,
80-
0,
81-
82-
0,
83-
0,
84-
0,
85-
0,
86-
0,
87-
0,
88-
0,
89-
0,
90-
0, // 6 7 8
91-
0,
92-
0,
93-
0,
94-
0,
95-
0,
96-
0,
97-
0,
98-
0,
99-
0,
100-
0,
101-
0,
102-
0,
103-
0,
104-
0,
105-
0,
106-
0,
107-
0,
108-
0,
24+
boardState: [ // Block Set No
25+
0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 1 2
26+
0, 0, 0, 0, 0, 0, 0, 0, 0,
27+
0, 0, 0, 0, 0, 0, 0, 0, 0,
28+
29+
0, 0, 0, 0, 0, 0, 0, 0, 0, // 3 4 5
30+
0, 0, 0, 0, 0, 0, 0, 0, 0,
31+
0, 0, 0, 0, 0, 0, 0, 0, 0,
32+
33+
0, 0, 0, 0, 0, 0, 0, 0, 0, // 6 7 8
34+
0, 0, 0, 0, 0, 0, 0, 0, 0,
35+
0, 0, 0, 0, 0, 0, 0, 0, 0,
10936
],
11037
// X: avg sparsity of cols
11138
// Y: avg sparsity of rows
112-
// X: avg sparsity of 9-blocks
39+
// Z: avg sparsity of 9-blocks
11340
// W: total occupation
11441
// T: avg complete sets ( row / col / 9-block )
11542
// S: avg total board integrity
43+
// P: side of the board occupation
11644
statistics: {
11745
// column integrity
11846
getX: function (boardState) {
@@ -377,7 +305,7 @@ const PLAY_INFO = {
377305
(GAME_INFO.BOARD_SIZE_BLOCK * GAME_INFO.BOARD_SIZE_BLOCK +
378306
1 -
379307
Number(key)) *
380-
resultSet.divCount[key];
308+
resultSet.divCount[key];
381309
}
382310

383311
resultSet.divValue /=
@@ -386,6 +314,31 @@ const PLAY_INFO = {
386314

387315
return resultSet;
388316
},
317+
// total board integrity
318+
getP: function (boardState) {
319+
if (boardState === undefined) {
320+
boardState = PLAY_INFO.boardState;
321+
}
322+
323+
let result = .0;
324+
325+
for (let i = 0; i < GAME_INFO.BOARD_SIZE_BLOCK; i++) {
326+
for (let j = 0; j < GAME_INFO.BOARD_SIZE_BLOCK; j++) {
327+
result +=
328+
(
329+
Math.abs( i - Math.floor( GAME_INFO.BOARD_SIZE_BLOCK / 2 ) ) +
330+
Math.abs( j - Math.floor( GAME_INFO.BOARD_SIZE_BLOCK / 2 ) )
331+
) * boardState[ i * GAME_INFO.BOARD_SIZE_BLOCK + j ]
332+
}
333+
}
334+
335+
336+
// normalise the number
337+
result /= ( GAME_INFO.BOARD_SIZE_BLOCK - 1 ) * boardState.length;
338+
result = Math.round(result * 100) / 100; // 2 decimal precision
339+
340+
return result;
341+
}
389342
},
390343
utils: {
391344
getBlockNo: function (row, col) {

0 commit comments

Comments
 (0)