This commit is contained in:
2018-05-21 08:12:29 +03:00
commit 0d4e25dbf8
14 changed files with 12923 additions and 0 deletions

61
source/Daemon.js Normal file
View File

@ -0,0 +1,61 @@
const DEFAULT_EPSILON = 0.01;
function create_key(steps) {
return steps.join('');
}
export default class Daemon {
humanCount;
robotCount;
epsilon;
weights = {};
constructor(humanCount, robotCount, epsilon = DEFAULT_EPSILON) {
this.humanCount = humanCount;
this.robotCount = robotCount;
this.epsilon = epsilon;
}
predict(movements) {
const steps = this._getStepSlice(movements);
const w0 = this._getWeight([...steps, 0]);
const w1 = this._getWeight([...steps, 1]);
if (w1 > w0) {
return 1;
} else {
return 0;
}
}
adjust(movements, humanValue, stepNumber) {
const steps = this._getStepSlice(movements);
const weightAdjustment = this._getWeightAdjustment(stepNumber);
this._adjustWeight([...steps, humanValue], weightAdjustment);
}
_getStepSlice(movements) {
return movements.getLastMovements(this.humanCount, this.robotCount);
}
_getWeightAdjustment(stepNumber) {
return Math.pow(1 + this.epsilon, stepNumber);
}
_getWeight(steps) {
const key = create_key(steps);
const weight = this.weights[key];
return weight === undefined ? 0 : weight;
}
_setWeight(steps, value) {
const key = create_key(steps);
this.weights[key] = value;
}
_adjustWeight(steps, weight) {
const currentWeight = this._getWeight(steps);
const newWeight = currentWeight + weight;
this._setWeight(steps, newWeight);
}
}

23
source/Movements.js Normal file
View File

@ -0,0 +1,23 @@
export default class Movements {
humanMovements = [];
robotMovements = [];
constructor(human = [], robot = []) {
this.humanMovements = human;
this.robotMovements = robot;
}
makeHumanMove(value) {
this.humanMovements.push(value === 1 ? value : 0);
}
makeRobotMove(value) {
this.robotMovements.push(value === 1 ? value : 0);
}
getLastMovements(humanCount, robotCount) {
const humanSlice = this.humanMovements.slice(-humanCount);
const robotSlice = this.robotMovements.slice(-robotCount);
return [].concat(robotSlice, humanSlice);
}
}

13
source/Predictor.js Normal file
View File

@ -0,0 +1,13 @@
import Movements from './Movements';
import Supervisor from './Supervisor';
export default class Predictor {
movements;
supervisor;
score = 0;
constructor() {
this.movements = new Movements();
this.supervisor = new Supervisor();
}
}

12
source/Supervisor.js Normal file
View File

@ -0,0 +1,12 @@
export default class Supervisor {
daemons = [];
constructor(daemons) {
this.daemons = daemons;
}
predict(movements) {
}
}

15
source/index.js Normal file
View File

@ -0,0 +1,15 @@
import Vue from 'vue';
import './style.css';
import Predictor from './Predictor';
new Vue({
el: '#app',
data: {
predictor: new Predictor(),
},
methods: {
pass(value) {
console.log('PASS', value);
},
},
});

20
source/style.css Normal file
View File

@ -0,0 +1,20 @@
html, body {
width: 100vw;
}
.app {
display: block;
margin: 0 auto;
width: 200px;
text-align: center;
}
.score {
font-size: 300%;
margin: 2em auto 0.8em;
display: inline-block;
}
button {
display: inline-block;
}