示例#1
0
    // Use this for initialization
    void Start()
    {
        timer = 1 / step_per_second;

        maze_generator mg = this.GetComponentInParent <maze_generator>();

        dungeon = mg.Get_Dungeon;

        int mazeLength = dungeon.getMaze_length();

        tableLength = mazeLength * mazeLength;

        rewardTable = new int[tableLength, tableLength];
        qTable      = new float[tableLength, tableLength];
        usedTable   = new int[tableLength, tableLength];
        //creates reward table
        for (int i = 0; i < tableLength; i++)
        {
            for (int j = 0; j < tableLength; j++)
            {
                //first put every state to state as -1 so it is not reachable
                rewardTable[i, j] = -1;
                int curVerIndex_j    = i % mazeLength;
                int curVerIndex_i    = (i - curVerIndex_j) / mazeLength;
                int targetVerIndex_j = j % mazeLength;
                int targetVerIndex_i = (j - targetVerIndex_j) / mazeLength;
                //foreach state to state
                cell current = dungeon.getCells()[curVerIndex_i][curVerIndex_j];
                cell target  = dungeon.getCells()[targetVerIndex_i][targetVerIndex_j];
                //look for current states neighbours
                foreach (cell neighbour in current.getNeighbours())
                {
                    //if its reachable
                    if (neighbour == target)
                    {
                        //then mark it as 0 so it is reachable
                        rewardTable[i, j] = 0;
                        //if it is the end point then mark it with reward
                        if (dungeon.end_i == targetVerIndex_i && dungeon.end_j == targetVerIndex_j)
                        {
                            rewardTable[i, j] = reward;
                        }
                        break;
                    }
                }
            }
        }

        character  = Instantiate(character, new Vector3(dungeon.start_x, dungeon.start_y, 0), Quaternion.identity);
        curVertice = dungeon.start_i * dungeon.getMaze_length() + dungeon.start_j;
    }
示例#2
0
    // Use this for initialization
    void Start()
    {
        this.mazeLength = parameters.mazeLength;
        this.selectRandomStartAndEnd = parameters.selectRandomStartAndEnd;
        this.stepPerSecond           = parameters.stepPerSecond;
        this.waitingTimeAtTheEnd     = parameters.waitingTimeAtTheEnd;
        this.reward         = parameters.reward;
        this.learningRate   = parameters.learningRate;
        this.discountFactor = parameters.discountFactor;
        this.epoch          = parameters.epoch;
        this.stopLearningAt = parameters.stopLearningAt;
        this.smartChoosing  = parameters.smartChoosing;
        //set the random seed
        Random.InitState((int)System.DateTime.Now.Ticks);
        this.gameObject.SetActive(false);
        //initializing of the maze
        GameObject     generator = Instantiate(mazeGenerator, new Vector3(0, 0, 0), Quaternion.identity);
        maze_generator mgs       = generator.GetComponent <maze_generator>();

        //assigning some settings
        mgs.mazeLength = mazeLength;
        mgs.select_random_start_and_end = selectRandomStartAndEnd;
        this.gameObject.SetActive(true);
        //creates maze and stop
        this.gameObject.SetActive(false);
        //initializing of camera
        GameObject.FindGameObjectWithTag("Canvas").GetComponent <Canvas>().renderMode    = RenderMode.ScreenSpaceCamera;
        GameObject.FindGameObjectWithTag("Canvas").GetComponent <Canvas>().worldCamera   = Instantiate(cameraObject, new Vector3(0, 0, -10), Quaternion.identity);
        GameObject.FindGameObjectWithTag("Canvas").GetComponent <Canvas>().planeDistance = 5;
        //initializing of Q Learner instance
        q_learner ql = generator.AddComponent <q_learner>();

        //assigning of some settings
        ql.character           = character;
        ql.reward              = reward;
        ql.learning_rate       = learningRate;
        ql.discount_factor     = discountFactor;
        ql.step_per_second     = stepPerSecond;
        ql.smart               = smartChoosing;
        ql.maxEpoch            = epoch;
        ql.stopLearningAt      = stopLearningAt;
        ql.waitingTimeAtTheEnd = waitingTimeAtTheEnd;
        this.gameObject.SetActive(true);
        //and start the programme
    }
示例#3
0
    //Update visual represantation of Q Table in scene
    void updateVisualQTable(int cur, int to)
    {
        if (usedTable[cur, to] < dungeon.getMaze_length())
        {
            usedTable[cur, to]++;
        }
        int            mazeLength = dungeon.getMaze_length();
        maze_generator mg         = this.gameObject.GetComponent <maze_generator>();

        GameObject[][] dungeonObjects = mg.Get_DungeonObjects;
        int            cur_j          = cur % mazeLength;
        int            cur_i          = (cur - cur_j) / mazeLength;

        if (cur - mazeLength == to) //north
        {
            Transform cell = dungeonObjects[cur_i][cur_j].transform;
            for (int i = 0; i < cell.childCount; i++)
            {
                if (cell.GetChild(i).tag == "NM")
                {
                    cell.GetChild(i).GetComponent <SpriteRenderer>().color = colorFunction(usedTable[cur, to]);
                    break;
                }
            }
        }
        else if (cur + 1 == to) //east
        {
            Transform cell = dungeonObjects[cur_i][cur_j].transform;
            for (int i = 0; i < cell.childCount; i++)
            {
                if (cell.GetChild(i).tag == "EM")
                {
                    cell.GetChild(i).GetComponent <SpriteRenderer>().color = colorFunction(usedTable[cur, to]);
                    break;
                }
            }
        }
        else if (cur + mazeLength == to) //south
        {
            Transform cell = dungeonObjects[cur_i][cur_j].transform;
            for (int i = 0; i < cell.childCount; i++)
            {
                if (cell.GetChild(i).tag == "SM")
                {
                    cell.GetChild(i).GetComponent <SpriteRenderer>().color = colorFunction(usedTable[cur, to]);
                    break;
                }
            }
        }
        else if (cur - 1 == to) //west
        {
            Transform cell = dungeonObjects[cur_i][cur_j].transform;
            for (int i = 0; i < cell.childCount; i++)
            {
                if (cell.GetChild(i).tag == "WM")
                {
                    cell.GetChild(i).GetComponent <SpriteRenderer>().color = colorFunction(usedTable[cur, to]);;
                    break;
                }
            }
        }
    }