Ejemplo n.º 1
0
        private void Play()
        {
            if (current_iteraction < iteractions)
            {
                if (current_iteraction == 0)
                {
                    current_state = problem.getRandomState();

                    int key = LearningCharacter.toKeyState(
                        max_hp,
                        max_energy,
                        current_state.getCharacterPosition().z_index,
                        current_state.getCharacterPosition().x_index);

                    current_state = all_states[key];

                    this.gameObject.transform.position = current_state.getCharacterPosition().position_vector();

                    hp_text_object.text     = "" + max_hp;
                    energy_text_object.text = "" + max_energy;
                }

                if (!is_moving)
                {
                    Action bestAction = store.getBestAction(current_state);

                    Pair new_move = bestAction.execute(current_state);

                    float distance = Vector3.Distance(current_state.getCharacterPosition().position_vector(), new_move.new_state.getCharacterPosition().position_vector());

                    inc = distance / seconds_per_action;

                    dir = (new_move.new_state.getCharacterPosition().position_vector() -
                           current_state.getCharacterPosition().position_vector());

                    current_state = new_move.new_state;

                    is_moving = true;

                    current_iteraction++;

                    hp_text_object.text     = "" + current_state.getHp();
                    energy_text_object.text = "" + current_state.getEnergy();

                    actions_text_object.text += bestAction.ToString() + ", ";

                    Debug.Log("AÇÃO --- " + bestAction.ToString());
                }
                else
                {
                    andar(dir / inc);
                }

                if (current_state.getHp() == 0)
                {
                    Debug.Log("MORREU HP = 0");
                    current_iteraction = iteractions;
                }
            }
        }
Ejemplo n.º 2
0
        private void Learn()
        {
            if (current_try < tries)
            {
                if (new_try)
                {
                    current_try++;
                    current_state = problem.getRandomState();
                    new_try       = false;
                    //Debug.Log("Try number " + current_try);
                }
                if (current_iteraction < iteractions)
                {
                    //                 alpha,gamma, rho,  nu
                    QLearning(problem, 0.7f, 0.75f, 0.2f, 0.1f);
                    current_iteraction++;
                    // if is dead restarts
                    if (current_state.getHp() == 0)
                    {
                        Debug.Log("restarts.... (dies)");
                        current_iteraction = 0;
                        new_try            = true;
                    }
                }
                else
                {
                    Debug.Log("Success!!!!");
                    current_iteraction = 0;
                    new_try            = true;
                }
            }
            else
            {
                current_try = 0;

                //Debug.Log("End and save");
                //save = true;
                store.writeMatrix();
                Debug.Log("Saved");
            }
        }