Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

201flaviosilva-labs/HillClimbing.js

Repository files navigation

Hill Climbing JavaScript

NPM

Table of Contents

Introduction

A hill climbing algorithm implemented in javascript. The algorithm is used to find the best solution to a problem. Most commonly used in the context of a Artificial Intelligence in genetic algorithm.

Simple Get Started

This is a simple example of how to use the library. For a more complete example, check the Wiki.

Import the library from npm

npm install hillclimbing

Load the library in your project

import HillClimbing from "hillclimbing";

Create a new instance of the library

const targets = [
	{
		name: "myValue1", // The name of the value
		value: 50, // The initial value
		min: 0, // The minimum value that the value can be
		max: 100, // The maximum value that the value can be
		precision: 0, // The number of decimal places to round to
	},
	{ name: "myValue2", value: -2, min: -100, max: 10 },
	{ name: "myValue3", value: 764, min: 100, max: 1250, precision: 10 },
];

const options = { // if you want, you can specify some default options
	startScore: -100, // The score that the algorithm starts with, (on reset it will be set to this value)
	numberOfMutations: 2, // The number of mutations that the algorithm will run every iteration
	}

const myHillClimbing = new HillClimbing(targets, options); // Create a new instance and pass the initial data (targets)

Update iterations

const myScore = 10;
const myNewSolution = myHillClimbing.run(myScore); // Run the algorithm and get the new solution based on the score

Full Code

import HillClimbing from "hillclimbing";

const targets = [
	{ name: "myValue1", value: 50, min: 0, max: 100, precision: 0 },
	{ name: "myValue2", value: -2, min: -100, max: 10 },
	{ name: "myValue3", value: 764, min: 100, max: 1250, precision: 10 },
];

const options = {startScore: -100, numberOfMutations: 2};

const myHillClimbing = new HillClimbing(targets, options);

const myScore = 10;
const myNewSolution = myHillClimbing.run(myScore);

For more information, examples and a better understanding, check the Wiki.

Community