| const CleanerProblem = require('./CleanerProblem'); | ||
| const CleanerAgent = require('./CleanerAgent'); | ||
| let myProblem = new CleanerProblem({ maxIterations: 12 }); | ||
| myProblem.addAgent("Smith", CleanerAgent, { x: 0, y: 2 }); | ||
| let iterator = myProblem.interactiveSolve([[0, 0, 0, 0], [0, 1, 1, -1], [0, 1, 0, 0], [0, 0, 0, 1]], { | ||
| onFinish: result => { | ||
| let agentID = result.actions[result.actions.length - 1].agentID; | ||
| console.log("Winner " + agentID); | ||
| console.log(result.actions); | ||
| let world = JSON.parse(JSON.stringify(result.data.world)); | ||
| let agentState = result.data.states[agentID]; | ||
| world[agentState.y][agentState.x] = "X"; | ||
| console.log(world); | ||
| }, | ||
| onTurn: result => { | ||
| console.log("Turn: " + JSON.stringify(result.actions[result.actions.length - 1])); | ||
| } | ||
| }); | ||
| iterator.next(); | ||
| iterator.next(); |
@@ -18,6 +18,2 @@ | ||
| this.data.world = JSON.parse(JSON.stringify(parameter.world)); | ||
| //this.solution = parameter.solution; | ||
| //this.update = parameter.update; | ||
| //this.callbacks = parameter.callbacks; | ||
| //this.perceptionForAgent = parameter.perceptionForAgent; | ||
| } | ||
@@ -56,14 +52,47 @@ /** | ||
| } | ||
| /** | ||
| * This function start the virtual life. It will continously execute the actions | ||
| * given by the agents in response to the perceptions. It stop when the solution function | ||
| * is satified or when the max number of iterations is reached. | ||
| * @param {Array} callbacks | ||
| */ | ||
| start(callbacks = {}) { | ||
| * This function start the virtual life. It will continously execute the actions | ||
| * given by the agents in response to the perceptions. It stop when the solution function | ||
| * is satisfied or when the max number of iterations is reached. | ||
| * If it must to run in interactive mode, the start mode return this object, which is actually | ||
| * the controller | ||
| * @param {Array} callbacks | ||
| */ | ||
| start(callbacks, interactive = false) { | ||
| this.callbacks = callbacks; | ||
| this.loop(); | ||
| this.currentAgentIndex = 0; | ||
| if (interactive === false) { | ||
| this.loop(); | ||
| return null; | ||
| } else { | ||
| return this; | ||
| } | ||
| } | ||
| /** | ||
| * Executes the next iteration in the virtual life simulation | ||
| */ | ||
| next() { | ||
| if (!this.problem.goalTest(this.data)) { | ||
| let keys = Object.keys(this.agents); | ||
| let agent = this.agents[keys[this.currentAgentIndex]]; | ||
| agent.receive(this.problem.perceptionForAgent(this.getData(), agent.getID())); | ||
| let action = agent.send(); | ||
| this.actions.push({ agentID: agent.getID(), action }); | ||
| this.problem.update(this.data, action, agent.getID()); | ||
| if (this.problem.goalTest(this.data)) { | ||
| this.finishAll(); | ||
| return false; | ||
| } else { | ||
| if (this.callbacks.onTurn) { | ||
| this.callbacks.onTurn({ actions: this.getActions(), data: this.data }); | ||
| } | ||
| if (this.currentAgentIndex >= keys.length - 1) this.currentAgentIndex = 0;else this.currentAgentIndex++; | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * Virtual life loop. At the end of every step it executed the onTurn call back. It could b used for animations of login | ||
@@ -70,0 +99,0 @@ */ |
+14
-4
@@ -51,11 +51,21 @@ const AgentController = require('../core/AgentController'); | ||
| * Solve the given problem | ||
| * @param {*} problem | ||
| * @param {*} world | ||
| * @param {*} callbacks | ||
| */ | ||
| solve(problem, callbacks) { | ||
| this.controller.setup({ world: problem, problem: this }); | ||
| this.controller.start(callbacks); | ||
| solve(world, callbacks) { | ||
| this.controller.setup({ world: world, problem: this }); | ||
| this.controller.start(callbacks, false); | ||
| } | ||
| /** | ||
| * Returns an interable function that allow to execute the simulation step by step | ||
| * @param {*} world | ||
| * @param {*} callbacks | ||
| */ | ||
| interactiveSolve(world, callbacks) { | ||
| this.controller.setup({ world: world, problem: this }); | ||
| return this.controller.start(callbacks, true); | ||
| } | ||
| } | ||
| module.exports = Problem; |
@@ -85,6 +85,6 @@ const Problem = require('../core/Problem'); | ||
| */ | ||
| solve(problem, callbacks) { | ||
| /*solve(problem, callbacks) { | ||
| this.controller.setup({ world: problem, problem: this }); | ||
| this.controller.start(callbacks); | ||
| } | ||
| }*/ | ||
| } | ||
@@ -91,0 +91,0 @@ |
@@ -17,5 +17,5 @@ const CleanerProblem = require('./CleanerProblem'); | ||
| }, | ||
| onTurn0: result => { | ||
| console.log("Turn: " + result); | ||
| onTurn: result => { | ||
| console.log("Turn: " + JSON.stringify(result.actions[result.actions.length - 1])); | ||
| } | ||
| }); |
+1
-1
| { | ||
| "name": "ai-agents", | ||
| "version": "0.1.3", | ||
| "version": "0.2.0", | ||
| "description": "Framework to create virtual agents", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
16455
16.43%10
11.11%418
15.15%