Skip to content

Commit

Permalink
Major changes and bug fixes:
Browse files Browse the repository at this point in the history
- rewritten the mechanism to update agents positions
- solved a bug that made unreachable promising positions to be returned
- solved a bug that made the agent move between two promising positions
- solved a bug that made the visits of a node become negative when removing parcels
  • Loading branch information
frangente committed Feb 2, 2024
1 parent 6847fef commit 4704909
Show file tree
Hide file tree
Showing 10 changed files with 651 additions and 452 deletions.
591 changes: 337 additions & 254 deletions src/domain/beliefs.ts

Large diffs are not rendered by default.

61 changes: 36 additions & 25 deletions src/domain/planner/mcts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class MonteCarloTreeSearch {
public constructor(enviroment: BeliefSet) {
this._beliefs = enviroment;

this._beliefs.onMyPositionChange(this._updatePosition.bind(this));
this._beliefs.onParcelsChange(this._onParcelsChange.bind(this));
this._beliefs.onExpiredParcels(this._onExpiredParcels.bind(this));
}
Expand All @@ -44,19 +45,17 @@ export class MonteCarloTreeSearch {
* Starts the MCTS algorithm.
* To stop the algorithm, call stop().
*
* @param position The current position of the agent.
*
* @throws {Error} If the MCTS algorithm is already started.
*/
public async start(position: Position) {
public async start() {
if (this._root !== null) {
throw new Error("MCTS already started. If you want to restart it, call stop() first.");
}

// we create a fake state to start the search
const state: State = {
executedIntenion: Intention.putdown(position),
position,
executedIntenion: Intention.putdown(this._beliefs.myPosition),
position: this._beliefs.myPosition,
arrivalInstant: Instant.now(),
pickedParcels: [],
};
Expand Down Expand Up @@ -93,20 +92,6 @@ export class MonteCarloTreeSearch {
this._root = null;
}

/**
* Updates the current position of the agent.
*
* @param position The new position of the agent.
*/
public updatePosition(position: Position) {
if (this._root === null) {
throw new MCTSNotStartedError();
}

this._root.state.arrivalInstant = Instant.now();
this._root.state.position = position;
}

/**
* Executes an intention.
* This method should be called when the agent executes an intention to update the tree
Expand All @@ -124,13 +109,30 @@ export class MonteCarloTreeSearch {
const child = this._root.children.find((child) =>
child.state.executedIntenion.equals(intention),
);
if (child === undefined) {
throw new Error("Intention not found.");
}
if (child !== undefined) {
this._root = child;
this._root.parent = null;
this._root.state.arrivalInstant = Instant.now();
} else {
// It may happen that while the player is executing an intention, the environment
// changes and the intention is removed from the tree. In this case, we start a new
// search from the state of the executed intention.
let pickedParcels: [ParcelID, DecayingValue][];
if (this._root.state.executedIntenion.type === IntentionType.PUTDOWN) {
pickedParcels = [];
} else {
pickedParcels = [...this._root.state.pickedParcels];
}

this._root = child;
this._root.parent = null;
this._root.state.arrivalInstant = Instant.now();
const state: State = {
executedIntenion: intention,
position: intention.position,
pickedParcels,
arrivalInstant: Instant.now(),
};

this._root = new Node(state, this._beliefs.getParcelPositions(), this._beliefs);
}
}

/**
Expand Down Expand Up @@ -211,6 +213,15 @@ export class MonteCarloTreeSearch {
// Private methods
// ------------------------------------------------------------------------

public _updatePosition(position: Position) {
if (this._root === null) {
return;
}

this._root.state.arrivalInstant = Instant.now();
this._root.state.position = position;
}

private _onParcelsChange(
newFreeParcels: ParcelID[],
changedPositionParcels: [ParcelID, Position, Position][],
Expand Down
10 changes: 3 additions & 7 deletions src/domain/planner/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export class Node {
// append the grandchildren to the current node. For the moment, we simply remove the
// intention and its subtree.
if (this.children.length > i) {
totalVisitDiff -= this.children[i].visits;
totalVisitDiff -= this.children[i].visits - 1;
this.children.splice(i, 1);
}

Expand All @@ -323,13 +323,9 @@ export class Node {
}

this._visits += totalVisitDiff;
if (this._visits < 0) {
if (this._visits <= 0) {
// just to be sure
throw new Error("The visits of a node cannot be negative.");
}

if (this._visits === 0) {
this._visits = 1;
throw new Error("The visits of a node cannot be non positive.");
}

return totalVisitDiff;
Expand Down
Loading

0 comments on commit 4704909

Please sign in to comment.