-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.js
65 lines (56 loc) · 2.67 KB
/
algorithm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import fs from 'fs';
import config from './config.js';
import generate_initialize_population from './generate_initial_population.js';
import { fitness_func_generation } from './fitness_func.js';
// import crossoverGeneration from './crossover.js';
// import mutate_Population from './mutation.js';
const check_acceptability = (population) => {
let flag = true;
for (let i = 0; i < population.length; i++) {
flag = true;
if (population[i].fitness >= config.acceptable_Global_Fitness_Score) {
console.log(population[i].data[j].local_fitness);
for (let j = 0; j < population[i].data.length; j++) {
if (population[i].data[j].local_fitness < config.acceptable_Local_Fitness_Score) {
flag = false;
}
}
} else flag = false;
if (flag == true) {
fs.writeFileSync('accepetedsol.json', JSON.stringify([population[i]], null, 4), 'utf8');
return true;
}
}
return false;
}
const algorithm = (room) => {
// step 1: generate initial population
// let population = generate_initialize_population();
// step 1: read population from file (if exists) (alternate)
let population = JSON.parse(fs.readFileSync('population_selected.json', 'utf8'));
// Checkpoint: write population to file
fs.writeFileSync('population_selected.json', JSON.stringify(population, null, 4), 'utf8');
// The loop is Technically infinite, and it will break when acceptable solution is found
// but we have set a max_generation limit to avoid infinite computation of poor offsprings
for (let i = 0; i < config.max_generation; i++) {
// step 2: crossover population
// population = crossoverGeneration(population, room);
// step 3: mutate population
// population = mutate_Population(population, room);
// checkpoint: write population to file per 10 iterations
if (i % 10 == 0) fs.writeFileSync('population_selected.json', JSON.stringify(population, null, 4), 'utf8');
// step 4: calculate fitness score
population = fitness_func_generation(population, room);
// step 5: Check for acceptability or Stop criteria
if (check_acceptability(population)) {
console.log('Acceptable solution found');
return population;
}
// Now Repeat step 2 to 5 until stop criteria
}
return null;
}
let room = JSON.parse(fs.readFileSync('room.json', 'utf8'));
let population = algorithm(room);
if (population == null) console.log('No acceptable solution found');
else fs.writeFileSync('population_selected.json', JSON.stringify(population, null, 4), 'utf8');