예제 #1
0
        private void OnMyTurnEnd()
        {
            _isTurnBegin = true;

            var timeNeeded = TyUtility.GetSecondsSinceStart() - _turnTimeStart;

            if (AdjustEpisodeMultiplier && UsedAlgorithm == Algorithm.SearchTree)
            {
                const double MAX_DIFF = 4.0;
                double       diff     = Math.Min(TyConst.DECREASE_SIMULATION_TIME - timeNeeded, MAX_DIFF);
                double       factor   = 0.05;

                //reduce more if above the time limit:
                if (diff <= 0.0f)
                {
                    factor = 0.2;
                }

                //simulate at max this value * _defaultEpisodeMultiplier:
                const int MAX_EPISODE_MULTIPLIER = 4;
                _curEpisodeMultiplier = Math.Clamp(_curEpisodeMultiplier + (int)(factor * diff * _defaultEpisodeMultiplier),
                                                   _defaultEpisodeMultiplier,
                                                   _defaultEpisodeMultiplier * MAX_EPISODE_MULTIPLIER);
            }

            if (PrintTurnTime)
            {
                TyDebug.LogInfo("Turn took " + timeNeeded.ToString("0.000") + "s");
            }

            if (timeNeeded >= TyConst.MAX_TURN_TIME)
            {
                TyDebug.LogWarning("Turn took " + timeNeeded.ToString("0.000") + "s");
            }
        }
예제 #2
0
        public static TyStateWeights UniformLerp(TyStateWeights lhs, TyStateWeights rhs, float t)
        {
            TyStateWeights p = new TyStateWeights();

            for (int i = 0; i < p._weights.Length; i++)
            {
                p._weights[i] = TyUtility.Lerp(lhs._weights[i], rhs._weights[i], t);
            }

            return(p);
        }
예제 #3
0
        /// <summary> False if there is not enough time left to do simulations. </summary>
        private bool IsAllowedToSimulate(double startTime, int curEpisode, int maxEpisode, int options)
        {
            double time = TyUtility.GetSecondsSinceStart() - startTime;

            if (time >= MAX_TIME)
            {
                TyDebug.LogWarning("Stopped simulations after " + time.ToString("0.000") + "s and " + curEpisode + " of " + maxEpisode + " episodes. Having " + options + " options.");
                return(false);
            }

            return(true);
        }
예제 #4
0
        public static TyStateWeights UniformRandLerp(TyStateWeights lhs, TyStateWeights rhs, System.Random random, float tMin, float tMax)
        {
            TyStateWeights p = new TyStateWeights();

            for (int i = 0; i < p._weights.Length; i++)
            {
                float t = random.RandFloat(tMin, tMax);
                p._weights[i] = TyUtility.Lerp(lhs._weights[i], rhs._weights[i], t);
            }

            return(p);
        }
예제 #5
0
        public static TyStateWeights NonUniformLerp(TyStateWeights lhs, TyStateWeights rhs, float[] tValues)
        {
            System.Diagnostics.Debug.Assert(tValues.Length >= (int)WeightType.Count);

            TyStateWeights p = new TyStateWeights();

            for (int i = 0; i < p._weights.Length; i++)
            {
                p._weights[i] = TyUtility.Lerp(lhs._weights[i], rhs._weights[i], tValues[i]);
            }

            return(p);
        }
예제 #6
0
        //When your hero takes fatal damage, prevent it and become Immune this turn.
        private static void IceBlock(TyState playerState, TyState opponentState, Controller player, Controller opponent, Spell secret)
        {
            //give punishment when at full hp, give reward if hp lessens

            const int MAX_HEALTH    = 30;
            const int MIN_HEALTH    = 1;
            float     healthPercent = 1.0f - TyUtility.InverseLerp(playerState.HeroHealth, MIN_HEALTH, MAX_HEALTH);

            //punishment when at full hp:
            const float MIN_VALUE = -30.0f;
            //reward when at 1 hp:
            const float MAX_VALUE = 45.0f;

            float value = TyUtility.Lerp(MIN_VALUE, MAX_VALUE, healthPercent);

            playerState.BiasValue += value;
        }
예제 #7
0
        private PlayerTask GetSimulationTreeTask(POGame.POGame poGame, List <PlayerTask> options)
        {
            double time = TyUtility.GetSecondsSinceStart() - _turnTimeStart;

            if (time >= TyConst.MAX_TURN_TIME)
            {
                TyDebug.LogError("Turn takes too long, fall back to greedy.");
                return(GetGreedyBestTask(poGame, options));
            }

            _simTree.InitTree(_analyzer, poGame, options);

            //-1 because TurnEnd won't be looked at:
            int optionCount = options.Count - 1;
            int numEpisodes = (int)((optionCount) * _curEpisodeMultiplier);

            double simStart = TyUtility.GetSecondsSinceStart();

            /*
             * for (int i = 0; i < numEpisodes; i++)
             * {
             *      if (!IsAllowedToSimulate(simStart, i, numEpisodes, optionCount))
             *              break;
             *
             *      bool shouldExploit = ((double)i / (double)numEpisodes) > EXPLORE_TRESHOLD;
             *      _simTree.SimulateEpisode(_random, i, shouldExploit);
             * }
             *
             */
            int i = 0;

            while (IsAllowedToSimulate(simStart, i, numEpisodes, optionCount))
            {
                bool shouldExploit = ((double)i / (double)numEpisodes) > EXPLORE_TRESHOLD;
                _simTree.SimulateEpisode(_random, i, shouldExploit);
                i++;
            }

            var bestNode = _simTree.GetBestNode();

            return(bestNode.Task);
        }
예제 #8
0
 private void OnMyTurnBegin(POGame.POGame state)
 {
     _isTurnBegin   = false;
     _turnTimeStart = TyUtility.GetSecondsSinceStart();
 }