new HillClimbing(targets, options)
Parameters:
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
targets |
Array.<Object> | a list of targets |
|||||||||||||||
options |
Object | options for the algorithm Properties
|
Example
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 = {startScore: -100, numberOfMutations:2};
const myHillClimbing = new HillClimbing(targets); // Create a new instance and pass the initial data (targets)
Methods
-
static getVersion() → {String}
-
Returns the current version of the library
Returns:
StringExample
console.log(HillClimbing.getVersion()); // "0.0.2"
-
addTarget(target) → {void}
-
Add a new target to the list of targets
Parameters:
Name Type Description target
Object A new target to calculate in the new solution
Returns:
voidExample
myHillClimbing.addTarget({ name: "myNewValue", value: 50, min: 0, max: 100 });
-
exportData(json) → {Array.<Object>|string}
-
Returns the data of all iterations
Parameters:
Name Type Default Description json
boolean false true if the exported data should be in JSON format
Returns:
Array.<Object> | string -The data of all iterations
Example
console.log(myHillClimbing.exportData());
-
getBestScore() → {Number}
-
Get the best score that has been found so far
Returns:
Number -The best score that has been found so far
Example
console.log(myHillClimbing.getBestScore());
-
getBestSolution() → {Array.<Object>}
-
Returns the current best solution. The best solution is the solution that has the highest score
Returns:
Array.<Object> -The best solution
Example
console.log(myHillClimbing.getBestSolution());
-
getBestSolutionValues() → {Array.<number>}
-
Returns a array with the values of the targets with the best solution
Returns:
Array.<number> -The values of the targets with the best solution
Example
console.log(myHillClimbing.getBestSolutionValues());
-
getBestTargetValueSolutionByName(name) → {Number}
-
Returns the best solution value of the given target name
Parameters:
Name Type Description name
String The name of the target
Returns:
Number -The best solution value of the given target name
Example
console.log(myHillClimbing.getBestSolutionValue("myValue1"));
-
getCurrentSolution() → {Array.<Object>}
-
Returns the current solution
Returns:
Array.<Object> -The current solution
Example
console.log(myHillClimbing.getCurrentSolution());
-
getCurrentSolutionValues() → {Array.<number>}
-
Returns a array with the values of the targets with the current solution
Returns:
Array.<number> -The values of the targets with the current solution
Example
console.log(myHillClimbing.getCurrentSolutionValues());
-
getCurrentTargetValueSolutionByName(name) → {Number}
-
Returns the current solution value of the given target name
Parameters:
Name Type Description name
String The name of the target
Returns:
Number -The current solution value of the given target name
Example
console.log(myHillClimbing.getCurrentSolutionValue("myValue1"));
-
getLastTargetsChanged() → {Array.<Object>}
-
Returns the last target that has been changed
Returns:
Array.<Object> -The last target that has been changed
Example
console.log(myHillClimbing.getLastTargetsChanged()); // [Target]
-
getNumberOfIterations() → {Number}
-
Returns the number of iterations that have been run
Returns:
Number -The number of iterations that have been run
Example
console.log(myHillClimbing.getNumberOfIterations());
-
getTargets() → {Array.<Object>}
-
Returns all targets from the list
Returns:
Array.<Object> -The list of targets
Example
console.log(myHillClimbing.getTargets());
-
randomNumber(min, max) → {Number}
-
Get a random number between two numbers
Parameters:
Name Type Default Description min
Number 0 The minimum number
max
Number 1 The maximum number
Returns:
Number -The random number
Example
myHillClimbing.randomNumber(0, 100);
-
removeTarget(name) → {void}
-
Remove a target from the list of targets
Parameters:
Name Type Description name
String The name of the target to remove
Returns:
voidExample
myHillClimbing.removeTarget("myValue1");
-
reset() → {void}
-
Resets the algorithm
Returns:
voidExample
myHillClimbing.reset();
-
run(score) → {Array.<Object>}
-
This function its the main function of the algorithm. This function will calculate a new solution based on the best solution and will return the new solution.
Based in the given score, the algorithm will change randomly the value of a random target.
Parameters:
Name Type Description score
Number The score that will be used to calculate the new solution
Returns:
Array.<Object> -The new current solution
Example
const myNewScore = 10; myHillClimbing.run(myNewScore);
-
setAllTargets(targets) → {void}
-
Change all targets to the new targets
Parameters:
Name Type Description targets
Array.<Object> A list of new targets
Returns:
voidExample
myHillClimbing.setAllTargets([ { name: "myNewValue1", value: 0, min: 0, max: 5 }, { name: "myNewValue2", value: -100, min: -500, max: -100 }, { name: "myNewValue3", value: 0, min: 0, max: 1 }, ]);
-
setTargetMax(targetName, max) → {void}
-
Change a target maximum value
Parameters:
Name Type Description targetName
string The name of the target to change
max
number The new maximum value
Returns:
voidExample
myHillClimbing.setTargetMax("myTargetName", 100);
-
setTargetMin(targetName, min) → {void}
-
Change a target minimum value
Parameters:
Name Type Description targetName
string The name of the target to change
min
number The new minimum value
Returns:
voidExample
myHillClimbing.setTargetMin("myTargetName", -100);
-
setTargetName(oldName, newName) → {void}
-
Change a target name
Parameters:
Name Type Description oldName
string The name of the target to change
newName
string The new name of the target
Returns:
voidExample
myHillClimbing.setTargetName("myTargetName", "myNewName");
-
setTargetPrecision(targetName, precision) → {void}
-
Change a target precision
Parameters:
Name Type Description targetName
string The name of the target to change
precision
number The new precision value
Returns:
voidExample
myHillClimbing.setTargetPrecision("myTargetName", 5);
-
setTargetProperty(targetName, property, value) → {void}
-
Change a target property (name, min, max, precision)
Parameters:
Name Type Description targetName
string The name of the target to change
property
string The property to change
value
string | number The new value
Returns:
voidExamples
myHillClimbing.setTargetProperty("myTargetName", "name", "myNewName");
myHillClimbing.setTargetProperty("myNewName", "min", -100);