-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.js
88 lines (59 loc) · 1.76 KB
/
agent.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function Agent(agentName, matrix){
// General Variables Declaration
this.agentName = agentName;
this.originalMatrix = matrix;
this.tempMatrix = matrix;
this.actualPosition;
this.compensation = 1;
// Agent's Logic
this.findBestPosition = function(){ // Ojo Validar
var maxNum = 0;
var x, y;
for(var i = 0; i < this.tempMatrix.length; i++){
for(var j = 0; j < this.tempMatrix.length; j++){
if (this.tempMatrix[i][j] > maxNum){
maxNum = this.tempMatrix[i][j];
x = i;
y = j;
}
}
}
this.actualPosition = {"x":x, "y":y}
return {"maxNum":maxNum, "x":x, "y":y};
}
this.makeOffer = function(){
var bestPosition = this.findBestPosition();
this.tempMatrix[bestPosition.x][bestPosition.y] -= this.compensation;
console.log(this.agentName);
//console.log(bestPosition);
console.log(this.tempMatrix);
return {"position":{"x":bestPosition.x, "y":bestPosition.y}, "compensation":this.compensation};
}
this.evaluateOffer = function(offer){
//console.log(this.agentName + " " + position);
this.tempMatrix[offer.position.x][offer.position.y] += offer.compensation;
this.findBestPosition();
if (offer.position.x == this.actualPosition.x && offer.position.y == this.actualPosition.y){
// Responder OFERTA ACEPTADA {"acepted":true}
console.info(agentName + " acepta!");
return {"acepted": true};
} else {
// Responder OFERTA RECHAZADA {"acepted":false, "x":x, "y":y}
return {"acepted": false};
}
}
this.getOffer = function(offer){
if (offer.acepted){
// Actualizar Matriz Original
} else {
// Hacer otra oferta
}
}
this.sumar = function(funcion, num){
var suma = num + 1;
if (suma < 21){
console.log("Suma desde " + this.id + ": " + suma);
funcion.sumar(this, suma);
}
}
}