Menu

[r61]: / GraphScript / trunk / scripts / samples / consensus.js  Maximize  Restore  History

Download this file

149 lines (129 with data), 2.6 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
var OP_ONE = 'ONE';
var OP_TWO = 'TWO';
var OPKEY = 'opinion';
var changeMindThreshold = 0.8;
function setOpinion(v,op)
{
setVertexData(v,OPKEY,op);
}
function getOpinion(v)
{
if (v)
return v.getUserDatum(OPKEY);
}
function calcOpinion(v)
{
var neighbors = v.getNeighbors().toArray();
var op1Count = 0, op2Count = 0;
for (var i = 0; i < neighbors.length; i++)
{
var neighbor = neighbors[i];
var nOp = getOpinion(neighbor);
// msg('neighbor - ' + neighbor + ': ' + nOp);
if (nOp == OP_ONE)
{
op1Count++;
}
if (nOp == OP_TWO)
{
op2Count++;
}
}
var total = neighbors.length;
var op1Percent = op1Count / total;
var op2Percent = op2Count / total;
msg('op1% = ' + op1Percent);
msg('op2% = ' + op2Percent);
if (op1Percent > changeMindThreshold )
{
setOpinion(v,OP_ONE);
}
else if (op2Percent > changeMindThreshold )
{
setOpinion(v,OP_TWO);
}
}
var changedOp = false;
var prevOps = {};
function randOpinion()
{
var r = Math.random();
msg('random opinion: ' + (r > 0.5 ? OP_ONE : OP_TWO));
return r > 0.5 ? OP_ONE : OP_TWO;
}
function init()
{
applyToVertices
(
function(v)
{
setOpinion(v,randOpinion());
}
);
changedOp = false;
prevOps = {};
applyToVertices(showVertexOpinion);
GraphUI.refreshGraph();
}
function showVertexOpinion(v)
{
if (v)
{
var op = getOpinion(v);
// msg(v + ': ' + op);
var color = 'red';
if (op == OP_ONE)
color = 'blue';
else if (op == OP_TWO)
color = 'white';
setVertexColor(v,color);
setVertexLabel(v,op || 'NONE');
}
}
function keepOpinions()
{
applyToVertices(
function(v)
{
prevOps[v] = getOpinion(v);
}
);
}
function compareOpinions()
{
applyToVertices(
function(v)
{
var currOp = getOpinion(v);
// msg(v + ' currently: ' + currOp);
var prevOp = prevOps[v];
// msg(v + ' previously: ' + prevOp);
changedOp = changedOp || (currOp != prevOp);
}
);
}
var alg = createAlgorithm();
addAlgorithmStep
(
alg,
function()
{
changedOp = false;
keepOpinions(); //remember current opinions
applyToVertices(calcOpinion); //calc all opinions
applyToVertices(showVertexOpinion);
compareOpinions(); //see if any opinion changed
msg("changed op : " + changedOp);
return !changedOp; //stop if no opinion was changed
},
function() { return 0; } //return to the same step
);
setResetFunc(init);
init();
/*
applyToVertices(calcOpinion); //calc all opinions
applyToVertices(showVertexOpinion);
applyToVertices(calcOpinion); //calc all opinions
applyToVertices(showVertexOpinion);
*/
runAlgorithm(alg);
MongoDB Logo MongoDB