Beispiel #1
0
        public void createStates()
        {
            //all_states = new List<State>();
            all_states = new Dictionary <int, State>();
            State state;
            int   index = 0;

            for (int i = 0; i <= max_hp; i++)                 // HP [0,1,2]
            {
                for (int j = 0; j <= max_energy; j++)         // ENERGY [0,1,2]
                {
                    for (int k = 0; k < n_positions; k++)     // POSITION X [10]
                    {
                        for (int l = 0; l < n_positions; l++) // POSITION Z [10]
                        {
                            state = new State();
                            state.setHp(i);
                            state.setEnergy(j);
                            state.setCharacterPosition(positions[k, l]);
                            state.setIndex(index);

                            //Debug.Log("State: " + state.getObjective() + " " + state.getCharacterPosition().z_index + " " + state.getCharacterPosition().x_index + " " + state.getEnergy() + " " + state.getHp());

                            int hash_index = toKeyState(i, j, state.getCharacterPosition().z_index,
                                                        state.getCharacterPosition().x_index);

                            all_states.Add(hash_index, state);
                            index++;
                        }
                    }
                }
            }

            n_states = all_states.Count;
        }
Beispiel #2
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;
                }
            }
        }