Skip to content

bsneade/utility-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

8a6df9c · Mar 13, 2024

History

68 Commits
Nov 23, 2021
May 1, 2020
May 1, 2020
Apr 13, 2018
May 1, 2020
Apr 21, 2017
Feb 28, 2018
May 1, 2020
Mar 13, 2024
Apr 22, 2023
Mar 3, 2018
May 1, 2020

Repository files navigation

utility-agent

A JavaScript implementation of a Utility Agent

What is a Utility Agent

A Utility Agent is a simple form of AI that make decisions based on the utility (score) of an available set of actions.

For some more information check out [Wikipedia - Utility Based Agents][https://en.wikipedia.org/wiki/Intelligent_agent#Utility-based_agents].

Installation

npm install --save utility-agent

Usage

For Javascript, see below. For Typescript there is a barrel provided. The same classes need to be extended or implemented as in Javascript. See the integration tests.

Create some Actions

Actions are what your agent is capabile of doing. The Action should return the modified context.

var UtilityAgent = require("utility-agent");

//Create a constructor
function ExampleAction() {
    //call the super constructor with a name for this action
	UtilityAgent.Action.call(this,"ExampleAction");
}
ExampleAction.prototype = new UtilityAgent.Action(); //copy the prototype

//Override the execute method
ExampleAction.prototype.execute = function(context) {
	//Perform your action here.  It is recommended that your action return a Promise.
}

module.exports = ExampleAction;

Create some scorers

Scorers calculate a utility score based on the provided context. The base Scorer provides some utility functions for common formulas.

var UtilityAgent = require("utility-agent");

//Create a constructor
function ExampleScorer() {
    //call the super on the constructor
	UtilityAgent.Scorer.apply(this);
}
ExampleScorer.prototype = Object.create(UtilityAgent.Scorer.prototype); // copy the prototype

//Override the score method
ExampleScorer.prototype.score = function(context) {
    //Your scoring logic goes here.
    //There are a bunch of helper methods for performing some calcuations as well.
	return Promise.resolve(this.linearFunction(10, 1, 0));
}

module.exports = ExampleScorer;

Create the Agent

The Agent is the entry point and configuration for the system. This is where you instantiate your Actions and link them to to your Scorers. These will go into an array of Qualifiers. Then we pick which Selector we are going to use and instantiate our UtilityAgent.

var UtilityAgent = require("utility-agent");

//This is the set of actions that your AI will perform.  A Qualifier links a set of 
//   reusable scorers together to determine the total score for the Action.  There
//   are multiple types of Qualifiers.
var qualifiers = [
    new UtilityAgent.SumOfChildrenQualifier([ new ExampleScorer() ], new ExampleAction())
];

//Create the Selector, which dictates the method by which scored Qualifiers will be chosen.
var selector = new UtilityAgent.HighestScoringSelector();

//Finally instantiate your Agent
agent = new UtilityAgent.UtilityAgent(selector, qualifiers);

Tick the agent logic

When you call the tick() method on your agent it will calculate the utilities and pick a winning Action. It will be up to the client to invoke the action with its managed Context.

//Tick one cycle of the logic
agent.tick(context, null) //our Selector doesn't require a default value here
    .then(winningAction => { 
        //go ahead and execute the action that was returned
    	return Promise.resolve(winningAction.execute(context)); 
    })
    .catch(error => { 
        return Promise.reject("Could not run tick: " + error);
    });