Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added some nice toStrings() for debugging, and more logging in MCTS
  • Loading branch information
AlexanderNorup committed Oct 31, 2023
commit e699aad8e00865589ac545ae147ad79abebed513
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ public boolean equals(Object obj) {
}
return false;
}

@Override
public String toString() {
return String.format("Participant with %s (UUID: %s...)", this.activeMonster.toString(), this.uuid.toString().substring(0,5));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ public IBattleState clone() {
clone.setActiveParticipant(activeParticipant);
return clone;
}

@Override
public String toString() {
return String.format("%s vs. %s (it is %s's turn)", this.player.getActiveMonster(), this.enemy.getActiveMonster(), this.activeParticipant.getActiveMonster().getName());
}
}
24 changes: 21 additions & 3 deletions src/main/java/dk/sdu/mmmi/modulemon/MCTSBattleAI/MCTSBattleAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private long getTimeLimitms() {

@Override
public void doAction() {
System.out.println("Starting action finding (time limit: " + this.timeLimit + ")");
System.out.printf("Starting action finding (time limit: %dms)%n", this.timeLimit);
numSimulatedActions = 0;

// Update state, should the enemy have changed their monster
Expand All @@ -78,7 +78,9 @@ public void doAction() {

var bestChild = bestChild(rootNode, 0);

System.out.println(explainBestChild(bestChild));
System.out.println(String.format("Simulated %d actions in %dms", this.numSimulatedActions, ((System.nanoTime() - startTime) / 1000000)));
System.out.println(explainNodeOptions(rootNode));
// System.out.println(explainBestChild(bestChild));

if (bestChild.getParentMove() != null) {
battleSimulation.doMove(participantToControl, bestChild.getParentMove());
Expand All @@ -89,12 +91,28 @@ public void doAction() {
}
}

private String explainNodeOptions(Node rootNode){
StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("MCTS has ").append(rootNode.getChildren().size()).append(" options. In decreasing order of reward they are:").append('\n');
for(var node : rootNode.getChildren().stream().sorted((a,b) -> Float.compare(a.getReward(), b.getReward())*-1).toArray()){
stringBuilder.append("- ").append(node.toString()).append('\n');
}

return stringBuilder.toString();
}

/**
* Will iterate the best rewards under a given node.
* A nice way to get an overview over what the best scenario (according to MCTS) below a certain Node is.
* It's not suuper usefull, because it has to assume the player is playing in their favour, which they often arent.
*/
private String explainBestChild(Node bestChild) {
StringBuilder stringBuilder = new StringBuilder();
String action = bestChild.getParentMove() != null ?
"using " + bestChild.getParentMove().getName()
: "switching to " + bestChild.getParentSwitch();
stringBuilder.append("MCTS is ").append(action).append(" because it sees the following best scenario: (Simulated ").append(this.numSimulatedActions).append(" actions)").append('\n');
stringBuilder.append("Looking forward from ").append(action).append(", it sees the following best scenario:").append('\n');

var currentlyExpanding = bestChild;
var isMCTS = false;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/dk/sdu/mmmi/modulemon/MCTSBattleAI/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,19 @@ public void setParticipant(IBattleParticipant participant) {
private IBattleParticipant getOpposingParticipant(IBattleParticipant current, IBattleState state) {
return state.getPlayer().equals(current) ? state.getEnemy() : state.getPlayer();
}

@Override
public String toString() {
if(this.parent != null){
String action = "doing nothing";
if(this.parentMove != null){
action = String.format("%s using %s", this.parent.getParticipant().getActiveMonster().getName(), this.parentMove);
}else if(this.parentSwitch != null){
action = "Switching to " + this.parentSwitch;
}
return String.format("%s (Reward: %.8f)", action, this.reward);
}else{
return "Root node";
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/dk/sdu/mmmi/modulemon/Monster/Monster.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,6 @@ public int hashCode() {

@Override
public String toString() {
return this.getName();
return String.format("%s (HP: %d/%d)", this.getName(), this.getHitPoints(), this.getMaxHitPoints());
}
}