<commit_before>package io.github.nejc92.mcts;

import com.rits.cloning.Cloner;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

public class MctsTreeNodeTest {

    private static final double EXPLORATION_PARAMETER = 0.6;

    private List<String> allPossibleActions;
    private List<String> availableActions;
    private MctsTreeNode<TestState, String> rootNode;
    private TestState state;
    private Cloner cloner = new Cloner();

    @Before
    public void setUp() {
        allPossibleActions = new ArrayList<>(Arrays.asList("0", "1", "2"));
        availableActions = allPossibleActions.subList(0, 2);
        state = new TestState(availableActions);
        rootNode = new MctsTreeNode<>(state, EXPLORATION_PARAMETER, cloner);
    }

    @Test
    public void testDeepCloneRepresentedState() {
        TestState clone = rootNode.deepCloneRepresentedState();
        assertNotEquals(state, clone);
    }

    @Test
    public void isRootNode() {
        MctsTreeNode<TestState, String> child = rootNode.addNewChildFromAction(allPossibleActions.get(0));
        assertTrue(rootNode.isRootNode());
        assertFalse(child.isRootNode());
    }

    @Test
    public void testGetUntriedActionsForCurrentPlayer() {
        assertEquals(availableActions, rootNode.getUntriedActionsForCurrentAgent());
        rootNode.addNewChildFromAction(availableActions.get(0));
        availableActions.remove(0);
        assertEquals(availableActions, rootNode.getUntriedActionsForCurrentAgent());
        rootNode.addNewChildFromAction(availableActions.get(0));
        assertEquals(new ArrayList<String>(), rootNode.getUntriedActionsForCurrentAgent());
    }

    @Test
    public void testAddNewChildFromValidAction() {
        MctsTreeNode<TestState, String> child = rootNode.addNewChildFromAction(allPossibleActions.get(0));
        assertEquals(rootNode, child.getParentNode());
    }

    @Test(expected= IllegalArgumentException.class)
    public void testAddNewChildFromInvalidAction() {
        rootNode.addNewChildFromAction(allPossibleActions.get(2));
    }

    @Test(expected= IllegalArgumentException.class)
    public void testAddNewChildFromTriedAction() {
        rootNode.addNewChildFromAction(availableActions.get(0));
        rootNode.addNewChildFromAction(availableActions.get(0));
    }

    @Test
    public void testIsFullyExpanded() {
        assertFalse(rootNode.isFullyExpanded());
        rootNode.addNewChildFromAction(availableActions.get(0));
        assertFalse(rootNode.isFullyExpanded());
        rootNode.addNewChildFromAction(availableActions.get(1));
        assertTrue(rootNode.isFullyExpanded());
    }

    @Test
    public void testGetBestChildWithLotsOfVisits() {
        MctsTreeNode<TestState, String> child0 = rootNode.addNewChildFromAction(availableActions.get(0));
        MctsTreeNode<TestState, String> child1 = rootNode.addNewChildFromAction(availableActions.get(1));
        for (int i = 0; i < 21; i++) {
            rootNode.updateDomainTheoreticValue(0);
            if (i < 20) child0.updateDomainTheoreticValue(0.25); // totalReward: 5 visitCount: 20
            else child1.updateDomainTheoreticValue(0.5);         // totalReward: 0.5 visitCount: 1
        }
        assertEquals(child1, rootNode.getBestChild());
    }

    @Test
    public void testGetBestChildWithSameVisits() {
        MctsTreeNode<TestState, String> child0 = rootNode.addNewChildFromAction(availableActions.get(0));
        MctsTreeNode<TestState, String> child1 = rootNode.addNewChildFromAction(availableActions.get(1));
        for (int i = 0; i < 21; i++) {
            rootNode.updateDomainTheoreticValue(0);
            child0.updateDomainTheoreticValue(0.01); // totalReward: 0.2 visitCount: 20
            child1.updateDomainTheoreticValue(0.02); // totalReward: 0.4 visitCount: 20
        }
        assertEquals(child1, rootNode.getBestChild());
    }

    @Test(expected= UnsupportedOperationException.class)
    public void testGetBestChildWithUnvisitedChildNodes() {
        MctsTreeNode<TestState, String> child0 = rootNode.addNewChildFromAction(availableActions.get(0));
        assertEquals(child0, rootNode.getBestChild());
    }

    @Test(expected= UnsupportedOperationException.class)
    public void testGetBestChildWithoutChildNodes() {
        rootNode.getBestChild();
    }
}<commit_msg>mcts - tree node add getBestChild tests<commit_after>package io.github.nejc92.mcts;

import com.rits.cloning.Cloner;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

public class MctsTreeNodeTest {

    private static final double EXPLORATION_PARAMETER = 0.6;

    private List<String> allPossibleActions;
    private List<String> availableActions;
    private MctsTreeNode<TestState, String> rootNode;
    private TestState state;
    private Cloner cloner = new Cloner();

    @Before
    public void setUp() {
        allPossibleActions = new ArrayList<>(Arrays.asList("0", "1", "2"));
        availableActions = allPossibleActions.subList(0, 2);
        state = new TestState(availableActions);
        rootNode = new MctsTreeNode<>(state, EXPLORATION_PARAMETER, cloner);
    }

    @Test
    public void testDeepCloneRepresentedState() {
        TestState clone = rootNode.deepCloneRepresentedState();
        assertNotEquals(state, clone);
    }

    @Test
    public void isRootNode() {
        MctsTreeNode<TestState, String> child = rootNode.addNewChildFromAction(allPossibleActions.get(0));
        assertTrue(rootNode.isRootNode());
        assertFalse(child.isRootNode());
    }

    @Test
    public void testGetUntriedActionsForCurrentPlayer() {
        assertEquals(availableActions, rootNode.getUntriedActionsForCurrentAgent());
        rootNode.addNewChildFromAction(availableActions.get(0));
        availableActions.remove(0);
        assertEquals(availableActions, rootNode.getUntriedActionsForCurrentAgent());
        rootNode.addNewChildFromAction(availableActions.get(0));
        assertEquals(new ArrayList<String>(), rootNode.getUntriedActionsForCurrentAgent());
    }

    @Test
    public void testAddNewChildFromValidAction() {
        MctsTreeNode<TestState, String> child = rootNode.addNewChildFromAction(allPossibleActions.get(0));
        assertEquals(rootNode, child.getParentNode());
    }

    @Test(expected= IllegalArgumentException.class)
    public void testAddNewChildFromInvalidAction() {
        rootNode.addNewChildFromAction(allPossibleActions.get(2));
    }

    @Test(expected= IllegalArgumentException.class)
    public void testAddNewChildFromTriedAction() {
        rootNode.addNewChildFromAction(availableActions.get(0));
        rootNode.addNewChildFromAction(availableActions.get(0));
    }

    @Test
    public void testIsFullyExpanded() {
        assertFalse(rootNode.isFullyExpanded());
        rootNode.addNewChildFromAction(availableActions.get(0));
        assertFalse(rootNode.isFullyExpanded());
        rootNode.addNewChildFromAction(availableActions.get(1));
        assertTrue(rootNode.isFullyExpanded());
    }

    @Test
    public void testGetBestChildWithLessVisitsLessReward() {
        MctsTreeNode<TestState, String> child0 = rootNode.addNewChildFromAction(availableActions.get(0));
        MctsTreeNode<TestState, String> child1 = rootNode.addNewChildFromAction(availableActions.get(1));
        for (int i = 0; i < 20; i++) {
            rootNode.updateDomainTheoreticValue(0);
            if (i < 5) child0.updateDomainTheoreticValue(0.1); // visitCount:  5, totalReward: 0.5
            else child1.updateDomainTheoreticValue(0.2);       // visitCount: 15, totalReward: 3
        }
        assertEquals(child0, rootNode.getBestChild());
    }

    @Test
    public void testGetBestChildWithLessVisitsMoreReward() {
        MctsTreeNode<TestState, String> child0 = rootNode.addNewChildFromAction(availableActions.get(0));
        MctsTreeNode<TestState, String> child1 = rootNode.addNewChildFromAction(availableActions.get(1));
        for (int i = 0; i < 20; i++) {
            rootNode.updateDomainTheoreticValue(0);
            if (i < 5) child0.updateDomainTheoreticValue(0.5); // visitCount:  5, totalReward: 2.5
            else child1.updateDomainTheoreticValue(0.1);       // visitCount: 15, totalReward: 1.5
        }
        assertEquals(child0, rootNode.getBestChild());
    }

    @Test
    public void testGetBestChildWithSameVisitsLessReward() {
        MctsTreeNode<TestState, String> child0 = rootNode.addNewChildFromAction(availableActions.get(0));
        MctsTreeNode<TestState, String> child1 = rootNode.addNewChildFromAction(availableActions.get(1));
        for (int i = 0; i < 21; i++) {
            rootNode.updateDomainTheoreticValue(0);
            child0.updateDomainTheoreticValue(0.01); // visitCount: 20, totalReward: 0.2
            child1.updateDomainTheoreticValue(0.02); // visitCount: 20, totalReward: 0.4
        }
        assertEquals(child1, rootNode.getBestChild());
    }


    @Test(expected= UnsupportedOperationException.class)
    public void testGetBestChildWithUnvisitedChildNodes() {
        MctsTreeNode<TestState, String> child0 = rootNode.addNewChildFromAction(availableActions.get(0));
        assertEquals(child0, rootNode.getBestChild());
    }

    @Test(expected= UnsupportedOperationException.class)
    public void testGetBestChildWithoutChildNodes() {
        rootNode.getBestChild();
    }
}
{'fraction_non_alphanumeric': 0.10373617198914632, 'fraction_numerical': 0.017011062408682946, 'mean_word_length': 3.7464091134224864, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '934086', 'n_tokens_mistral': 2899, 'n_tokens_neox': 2795, 'n_words': 469}