2017-02-21 21:16:18 +00:00
|
|
|
/// <reference path="AbstractAI.ts"/>
|
|
|
|
/// <reference path="Maneuver.ts"/>
|
|
|
|
module TS.SpaceTac {
|
|
|
|
|
2017-02-27 00:42:12 +00:00
|
|
|
export type TacticalProducer = Iterator<Maneuver>;
|
2017-03-08 23:18:40 +00:00
|
|
|
export type TacticalEvaluator = (maneuver: Maneuver) => number;
|
2017-02-21 21:16:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AI that applies a set of tactical rules
|
|
|
|
*
|
|
|
|
* It uses a set of producers (to propose new maneuvers), and evaluators (to choose the best maneuver).
|
2017-02-22 01:14:14 +00:00
|
|
|
*
|
|
|
|
* As much work as possible is done using iterators, without materializing every possibilities.
|
2017-02-21 21:16:18 +00:00
|
|
|
*/
|
|
|
|
export class TacticalAI extends AbstractAI {
|
|
|
|
producers: TacticalProducer[] = []
|
|
|
|
evaluators: TacticalEvaluator[] = []
|
|
|
|
|
2017-03-08 23:18:40 +00:00
|
|
|
best: Maneuver | null
|
|
|
|
best_score: number
|
2017-02-21 21:16:18 +00:00
|
|
|
|
|
|
|
protected initWork(): void {
|
2017-03-08 23:18:40 +00:00
|
|
|
this.best = null;
|
|
|
|
this.best_score = -Infinity;
|
|
|
|
|
2017-02-22 01:14:14 +00:00
|
|
|
if (this.producers.length == 0) {
|
|
|
|
this.setupDefaultProducers();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.evaluators.length == 0) {
|
|
|
|
this.setupDefaultEvaluators();
|
|
|
|
}
|
|
|
|
|
2017-02-21 21:16:18 +00:00
|
|
|
this.addWorkItem(() => this.unitWork());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluate a single maneuver
|
|
|
|
*/
|
|
|
|
evaluate(maneuver: Maneuver) {
|
|
|
|
return sum(this.evaluators.map(evaluator => evaluator(maneuver)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-27 00:42:12 +00:00
|
|
|
* Single unit of work => produce a batch of maneuvers and evaluate them
|
|
|
|
*
|
|
|
|
* The best produced maneuver (highest evaluation score) is kept to be played.
|
|
|
|
* If two maneuvers have nearly the same score, the best one is randomly chosen.
|
2017-02-21 21:16:18 +00:00
|
|
|
*/
|
|
|
|
private unitWork() {
|
2017-02-27 00:42:12 +00:00
|
|
|
let done = 0;
|
2017-02-21 21:16:18 +00:00
|
|
|
|
2017-02-27 00:42:12 +00:00
|
|
|
while (done < 1000 && this.producers.length > 0) {
|
|
|
|
// Produce a maneuver
|
2017-03-09 17:11:00 +00:00
|
|
|
let maneuver: Maneuver | null = null;
|
2017-02-27 00:42:12 +00:00
|
|
|
let producer = this.producers.shift();
|
2017-03-09 17:11:00 +00:00
|
|
|
if (producer) {
|
|
|
|
[maneuver, producer] = producer();
|
|
|
|
}
|
2017-02-21 21:16:18 +00:00
|
|
|
|
2017-02-27 00:42:12 +00:00
|
|
|
if (maneuver) {
|
2017-03-09 17:11:00 +00:00
|
|
|
if (producer) {
|
|
|
|
this.producers.push(producer);
|
|
|
|
}
|
2017-02-21 21:16:18 +00:00
|
|
|
|
2017-02-27 00:42:12 +00:00
|
|
|
// Evaluate the maneuver
|
|
|
|
let score = this.evaluate(maneuver);
|
|
|
|
//console.log(maneuver, score);
|
|
|
|
if ((Math.abs(score - this.best_score) < 0.0001 && this.random.bool()) || score > this.best_score) {
|
|
|
|
this.best = maneuver;
|
|
|
|
this.best_score = score;
|
|
|
|
}
|
2017-02-21 21:16:18 +00:00
|
|
|
}
|
2017-02-27 00:42:12 +00:00
|
|
|
|
|
|
|
done += 1;
|
2017-02-21 21:16:18 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 00:42:12 +00:00
|
|
|
// Continue or stop
|
|
|
|
if (this.producers.length > 0 && this.getDuration() < 3000) {
|
2017-02-21 21:16:18 +00:00
|
|
|
this.addWorkItem(() => this.unitWork());
|
|
|
|
} else if (this.best) {
|
|
|
|
this.best.apply();
|
|
|
|
}
|
|
|
|
}
|
2017-02-22 01:14:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the default set of maneuver producers
|
|
|
|
*/
|
|
|
|
private setupDefaultProducers() {
|
2017-02-27 00:42:12 +00:00
|
|
|
let producers = [
|
|
|
|
TacticalAIHelpers.produceDirectShots,
|
|
|
|
TacticalAIHelpers.produceBlastShots,
|
|
|
|
TacticalAIHelpers.produceRandomMoves,
|
|
|
|
]
|
2017-03-09 17:11:00 +00:00
|
|
|
producers.forEach(producer => this.producers.push(producer(this.ship, this.ship.getBattle() || new Battle())));
|
2017-02-22 01:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the default set of maneuver evaluators
|
|
|
|
*/
|
|
|
|
private setupDefaultEvaluators() {
|
2017-02-27 00:42:12 +00:00
|
|
|
let scaled = (evaluator: (...args) => number, factor: number) => (...args) => factor * evaluator(...args);
|
|
|
|
let evaluators = [
|
|
|
|
scaled(TacticalAIHelpers.evaluateTurnCost, 1),
|
|
|
|
scaled(TacticalAIHelpers.evaluateDamageToEnemy, 30),
|
|
|
|
scaled(TacticalAIHelpers.evaluateClustering, 3),
|
|
|
|
]
|
2017-03-09 17:11:00 +00:00
|
|
|
// TODO evaluator typing is lost
|
2017-02-27 00:42:12 +00:00
|
|
|
evaluators.forEach(evaluator => this.evaluators.push((maneuver: Maneuver) => evaluator(this.ship, this.ship.getBattle(), maneuver)));
|
2017-02-22 01:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-21 21:16:18 +00:00
|
|
|
}
|