forked from RichieTorres/DistributedEA-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.js
More file actions
172 lines (146 loc) · 4.75 KB
/
Copy pathClient.js
File metadata and controls
172 lines (146 loc) · 4.75 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var request = require("request");
var configuration = require("./configuration.js");
Client = function(){
var project = null;
var estimatedTime = null;
var population = null;
var realTime = null;
var objThis=this;
var fitness=[];
var oldIds=[];
var comunicationTimeDeliver=0;
var comunicationTimeRequest=0;
var workTime=0;
var sleepTime=0;
//The work time is taken in two different functions.
var workTimeIni=0;
var workTimeEnd=0;
function requestRandomInteger(max){
return Math.floor(Math.random()*max);
}
///returns aJob
this.requestJob = function(){
var requestOptions = {
url: configuration.urlServer,
form: {
action:'request',
assignedProject : project != null
}
};
var comunicationTimeIni=new Date();
request.post(requestOptions,function(error, response, body){
var comunicationTimeEnd=new Date();
comunicationTimeRequest+=comunicationTimeEnd-comunicationTimeIni;
workTimeIni=new Date();
if(error){
console.log("ERROR! "+error);
}else{
var objResponse = JSON.parse(body);
if(typeof objResponse.finalized === 'undefined'){
if(project === null){
project = objResponse.assignedProject;
eval('project.fitnessFunction = '+project.fitnessFunctionString);
eval('project.crossoverFunction = '+project.crossoverFunctionString);
eval('project.mutationFunction = '+project.mutationFunctionString);
}
if(typeof objResponse.sleep === 'undefined'){
estimatedTime = objResponse.estimatedTime;
population = objResponse.subPopulation;
generation = objResponse.generation + 1;
oldIds = objResponse.oldIds;
processJob();
deliverJob();
}
else{
sleepTime+=project.sleepTime;
setTimeout(requestJob,project.sleepTime);
}
}
else{
printReport();
}
}
});
};
function printReport(){
console.log("workTime: ",workTime/1000);
console.log("waiting for a job: ",comunicationTimeRequest/1000);
console.log("waiting for deliver a job: ",comunicationTimeDeliver/1000);
console.log("sleepTime: ",sleepTime/1000);
}
function processJob(){
realTime=new Date();
fitness=[]
var populationSize=population.length
var amountMutation=populationSize*project.mutationPercent;
//Selection
var selection = select(populationSize);
//Crossover
selection = crossMattingPool(selection,populationSize);
//The new chromosomes replaces all the old population
population=selection
//Mutation
mutatePopulation(amountMutation, populationSize);
realTime=new Date() - realTime;
};
function select(populationSize){
selection=new Array();
for(var i=0; i<populationSize; i+=2){
var idx1=i;
var idx2=i+1;
if (idx2 == populationSize){
idx2=0;
}
if(calculateFitness(idx1) > calculateFitness(idx2)){
selection.push(population[idx1]);
}
else{
selection.push(population[idx2])
}
}
return selection;
};
//A greater value is a greater fitness
function calculateFitness(idx){
fitness[idx]= project.fitnessFunction(population[idx]);
return fitness[idx];
};
function crossMattingPool(selection,populationSize){
for(var i = selection.length; i < populationSize; i++){
var idx1=requestRandomInteger(selection.length);
var idx2=requestRandomInteger(selection.length);
newChromosome = project.crossoverFunction(selection[idx1],selection[idx2]);
selection.push(newChromosome);
}
return selection;
};
function mutatePopulation(amountMutation, populationSize){
for(var i = 0; i < amountMutation; i++){
var idx=requestRandomInteger(populationSize);
population[idx]= project.mutationFunction(population[idx]);
}
};
function deliverJob(){
var requestOptions = {
url: configuration.urlServer,
form: {
action : 'deliver',
generation : generation,
newChromosomes : JSON.stringify(population),
estimatedTime : estimatedTime,
realTime : realTime,
fitness : JSON.stringify(fitness),
oldIds : JSON.stringify(oldIds)
}
};
workTimeEnd=new Date();
workTime+=workTimeEnd-workTimeIni;
var comunicationTimeIni=new Date();
request.post(requestOptions, function(error, response, body){
var comunicationTimeEnd=new Date();
comunicationTimeDeliver+=comunicationTimeEnd-comunicationTimeIni;
objThis.requestJob();
});
};
}
module.exports=Client;