Git Product home page Git Product logo

utility-agent's Introduction

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);
    });

utility-agent's People

Contributors

bsneade avatar dependabot[bot] avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

deebloo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.