示例#1
0
    //-------------------------------------------------------------------------
    //public static byte[] Compress(byte[] inputBytes)
    //{
    //    MemoryStream ms = new MemoryStream();
    //    GZipOutputStream gzip = new GZipOutputStream(ms);
    //    gzip.Write(inputBytes, 0, inputBytes.Length);
    //    gzip.Close();
    //    return ms.ToArray();
    //}

    //-------------------------------------------------------------------------
    ///// <summary>
    ///// 解压缩字节数组
    ///// </summary>
    ///// <param name="str"></param>
    //public static byte[] Decompress(byte[] inputBytes)
    //{
    //    GZipInputStream gzi = new GZipInputStream(new MemoryStream(inputBytes));
    //    MemoryStream re = new MemoryStream();
    //    int count = 0;
    //    byte[] data = new byte[4096];
    //    while ((count = gzi.Read(data, 0, data.Length)) != 0)
    //    {
    //        re.Write(data, 0, count);
    //    }
    //    return re.ToArray();
    //}

    //-------------------------------------------------------------------------
    void Start()
    {
        //UI
        //UIEventListener.Get(BtnStart.gameObject).onClick = OnClickStart;
        //UIEventListener.Get(BtnStop.gameObject).onClick = OnClickStop;

        if (LoadWeights)
        {
            try
            {
                LoadGenFromFile();
            }
            catch (System.Exception E)
            {
                Debug.Log("Load Gen Failed " + E.Message);
                LoadWeights = false;
            }
        }

        if (!LoadWeights)
        {
            UGenAlg.ConfigData Config = new UGenAlg.ConfigData();
            Config.PopSize    = 3000;
            Config.NumWeights = new UControllerBotAI().Net.GetWeightCount();
            Gen = new UGenAlg();
            Gen.Init(Config);
        }

        // 更新适应性分数代理
        Gen.onUpdateFitnessScores = UpdateFitnessScores;

        BoardList = new List <UChessBoard>();
        ChessBoardPrefab.learn = true;
        ChessBoardPrefab.gameObject.SetActive(showFirstChessboard);
        ChessBoardPrefab.InitStart();

        BoardList.Add(ChessBoardPrefab);

        // 创建其他棋盘
        for (int i = 1; i < Gen.Config.PopSize / 2; i++)
        {
            UChessBoard board = UnityEngine.Object.Instantiate <GameObject>(ChessBoardPrefab.gameObject).GetComponent <UChessBoard>();
            board.learn = true;
            //board.show = false;
            board.gameObject.SetActive(false);
            board.InitStart();
            BoardList.Add(board);
        }

        //将加载的适应性分数保存
        if (LoadWeights)
        {
            PutFitnessScores();
        }
    }
示例#2
0
    void StartLearn()
    {
        if (LoadWeights)
        {
            try
            {
                LoadGenFromFile();
            }
            catch (Exception E)
            {
                Debug.Log("Load Gen Failed " + E.Message);
                LoadWeights = false;
            }
        }

        if (!LoadWeights)
        {
            UGenAlg.ConfigData Config = new UGenAlg.ConfigData();
            Config.PopSize    = NetSize;
            Config.NumWeights = new UBotAIController().Net.GetWeightCount();
            Gen = new UGenAlg();
            Gen.Init(Config);
        }

        //更新适应性分数代理
        Gen.onUpdateFitnessScores = UpdateFitnessScores;


        MapList = new List <Map>();

        //创建其他棋盘
        for (int i = 0; i < Gen.Config.PopSize / 2; i++)
        {
            Map map = new Map();

            map.whitePlayer            = new Gamer();
            map.whitePlayer.map        = map;
            map.whitePlayer.camp       = Camp.White;
            map.blackPlayer            = new Gamer();
            map.blackPlayer.map        = map;
            map.blackPlayer.camp       = Camp.Black;
            map.whitePlayer.controller = new UBotAIController();
            map.blackPlayer.controller = new UBotAIController();

            MapList.Add(map);
            if (i == 0 && showFisrtGame)
            {
                board.InitMap(map);
            }
        }

        //将加载的适应性分数保存
        if (LoadWeights)
        {
            PutFitnessScores();
        }

        _learn_timer = 0;
        _stepCount   = 0;

        _started = true;
        _inited  = true;
        _learn   = true;
    }