예제 #1
0
    //-------------------------------------------------------------------------
    void Update()
    {
        if (_start)
        {
            try
            {
                //_auto_save_timer += RealTime.deltaTime;
                //if(_auto_save_timer >= auto_save_time)
                //{
                //    _auto_save_timer = 0.0;
                //    SaveToFile();
                //}

                //_learn_timer += RealTime.deltaTime;

                // 下棋,计算适应性分数
                {
                    // 重置适应性分数
                    ResetFitnessScores();

                    //// 下完所有的棋子
                    //for (int i = 0; i < BoardList.Count; i++)
                    //{
                    //    while (!BoardList[i].game_over)
                    //        BoardList[i].Step();
                    //}

                    // 下完一步(每边一步)
                    for (int step = 0; step < step_count_per_gen; step++)
                    {
                        for (int i = 0; i < BoardList.Count; i++)
                        {
                            BoardList[i].Step();
                            BoardList[i].Step();

                            if (BoardList[i].game_over)
                            {
                                BoardList[i].Restart();
                            }
                        }
                    }
                }

                // 迭代一次
                Gen.Epoch();

                // 将基因更新给神经网络
                PutWeightsToNet(Gen.GetWeights());

                // 刷新界面
                //LabelTun.text =string.Format("产生{0}代",Gen.Generation.ToString());
                //LabelTime.text = string.Format("时间{0}:{1}:{2}", (int)(_learn_timer/3600), ((int)_learn_timer%3600)/60, (int)_learn_timer%60);
            }
            catch (Exception E)
            {
                Debug.LogError(E.Message);
                Debug.LogError(E.StackTrace);
                OnClickStop(null);
            }
        }
    }
예제 #2
0
    // Update is called once per frame
    void Update()
    {
        if (!_started)
        {
            return;
        }

        if (!_learn)
        {
            _gameMap.Update(Time.deltaTime);
            return;
        }


        try
        {
            //_auto_save_timer += Time.deltaTime;
            //if (_auto_save_timer >= auto_save_time)
            //{
            //    _auto_save_timer = 0.0;
            //    SaveToFile();
            //}

            _learn_timer += Time.deltaTime;

            //下棋,计算适应性分数
            {
                //重置适应性分数
                ResetFitnessScores();

                //下完一步(每边一步)
                if (step_count_per_gen > 0)
                {
                    //for (int step = 0; step < step_count_per_gen; step++)
                    {
                        for (int i = 0; i < MapList.Count; i++)
                        {
                            MapList[i].Update(Time.deltaTime);
                            if (MapList[i].gameOver)
                            {
                                MapList[i].Restart();
                            }
                        }
                    }
                    _stepCount++;

                    if (_stepCount >= step_count_per_gen)
                    {
                        _stepCount = 0;
                        //迭代一次
                        Gen.Epoch();

                        //将基因更新给神经网络
                        PutWeightsToNet(Gen.GetWeights());
                    }
                }
                else
                {
                    bool gameOver  = true;
                    int  overCount = 0;
                    //下完所有的棋子
                    for (int i = 0; i < MapList.Count; i++)
                    {
                        if (!MapList[i].gameOver)
                        {
                            MapList[i].Update(Time.deltaTime);
                        }
                        else
                        {
                            overCount++;
                        }

                        if (!MapList[i].gameOver)
                        {
                            gameOver = false;
                        }
                    }

                    if (gameOver || (float)overCount / MapList.Count > 2.0f / 3.0f)
                    {
                        //迭代一次
                        Gen.Epoch();

                        //将基因更新给神经网络
                        PutWeightsToNet(Gen.GetWeights());

                        for (int i = 0; i < MapList.Count; i++)
                        {
                            if (MapList[i].gameOver)
                            {
                                MapList[i].Restart();
                            }
                        }
                    }
                }
            }


            //刷新界面
            textTuns.text = string.Format("{0}", Gen.Generation.ToString());
            textTime.text = string.Format("{0}:{1}:{2}", (int)(_learn_timer / 3600), ((int)_learn_timer % 3600) / 60, (int)_learn_timer % 60);
        }
        catch (Exception E)
        {
            Debug.LogError(E.Message);
            Debug.LogError(E.StackTrace);
            //OnClickStop(null);
        }
    }