Exemplo n.º 1
0
        public StoreData(StoreData st, COMMON_MAP map)
        {
            this.ID = st.ID;
            stoneIdList = new List<int>();
            stonePlaceList = new List<int>();
            stoneKindsList = new List<int>();
            evalueList = new List<float>();

            for(int i=0; i < st.stoneKindsList.Count; i++)
            {
                stoneIdList.Add(st.stoneIdList[i]);
                stonePlaceList.Add(st.stonePlaceList[i]);
                stoneKindsList.Add(st.stoneKindsList[i]);
            }
            for(int i=0; i < evalueList.Count; i++)
            {
                evalueList.Add(st.evalueList[i]);
            }
            evalue = st.evalue;
            score = st.score;
            closed = st.closed;
            agicF = new AGI_CalcFields(st.agicF, map);
            lastAttackStone = st.lastAttackStone;
        }
Exemplo n.º 2
0
        private void selectionQueue()
        {
            Dictionary<int, float> tmpMap = new Dictionary<int, float>();
            List<int> tmpList = new List<int>();
            List<StoreData> retryList = new List<StoreData>();

            /* マップ作成 */
            int countId = nextQueId.Count;

            for (int i = 0; i < countId; i++)
            {
                int id;
                if (nextQueId.TryDequeue(out id) == false)
                {
                    continue;
                }
                if (storeDataDic.ContainsKey(id) == true)
                {
                    float eval = storeDataDic[id].GetEvalueListSum();
                    tmpList.Add(id);
                    if (tmpMap.ContainsKey(id) != true)
                    {
                        tmpMap.Add(id, eval);
                    }
                }
                else if (saveStoreDataDic.ContainsKey(id) == true)
                {
                    float eval = storeDataDic[id].GetEvalueListSum();
                    tmpList.Add(id);
                    if (tmpMap.ContainsKey(id) != true)
                    {
                        tmpMap.Add(id, eval);
                    }
                }
                else
                {
                    ;
                }

            }

            /* ソート */
            /* mapソート */
            var map = tmpMap.OrderByDescending((x) => x.Value); /* 降順ソート */

            /* 評価値が優秀な種 */
            int count = 0;
            int retryCnt = 0;
            float preEvalue = 0.0F; /* 前回の評価値 */

            Dictionary<int, StoreData> copyStoreDic = new Dictionary<int, StoreData>();

            /* CHOISE_FOR_EVAL_NUM分Queueに格納 */
            foreach (var data in map)
            {
                int id = data.Key;
                if (searchQueId.Contains(id) != true)
                {
                    if (count < CHOISE_FOR_EVAL_NUM)
                    {
                        searchQueId.Enqueue(id);
                        /* copy */
                        StoreData st = new StoreData(storeDataDic[id], commonMap);
                        if (preEvalue != st.evalue)
                        {
                            copyStoreDic.Add(id, st);
                            /* copyEnd */
                            count++;
                            preEvalue = st.evalue;
                        }
                    }
                    else
                    {
                        ;
                    }

                    /* リトライサーチ用 */
                    if (retryCnt < RETRY_COUNT_EVAL)
                    {
                        StoreData retrySt = new StoreData(storeDataDic[id], commonMap);
                        retryList.Add(retrySt);
                        retryCnt++;
                    }
                }
                if(count >= CHOISE_FOR_EVAL_NUM && retryCnt >= RETRY_COUNT_EVAL)
                {
                    break;
                }
            }

            /* 焼きなまし?分 */
            int maxScoreId;
            int Temperature;
            int maxScore;

            if(searchQueId.TryPeek(out maxScoreId)== true)
            {
                if (copyStoreDic.ContainsKey(maxScoreId) == true) {
                    maxScore = copyStoreDic[maxScoreId].score;
                }
                else
                {
                    maxScore = 0;
                }

                Temperature = (int)(((float) maxScore / (float)agi.GetAgiFields().GetEmptySize()) * 10);
            }
            else
            {
                Temperature = 0;
            }

            Array tmpAry = tmpMap.ToArray();
            uint max = (uint)tmpList.Count;

            if (Temperature < AnnealingRate.Length && max != 0) {
                for (int i = 0; i < AnnealingRate[Temperature]; i++)
                {
                    int idx = (int)rand.NextMax(max - 1);
                    int id = tmpList[idx];

                    if (searchQueId.Contains(id) != true)
                    {

                         searchQueId.Enqueue(id);
                         /* copy */
                         StoreData st = new StoreData(storeDataDic[id], commonMap);
                        if (preEvalue != st.evalue)
                        {
                            copyStoreDic.Add(id, st);
                            /* copyEnd */

                            preEvalue = st.evalue;
                        }

                    }
                }
            }

            /* リトライサーチ用 */
            for (retryCnt = 0; retryCnt < RETRY_COUNT_RAND; retryCnt++)
            {
                if (max != 0)
                {
                    int idx = (int)rand.NextMax(max - 1);
                    int id = tmpList[idx];
                    if (searchQueId.Contains(id) != true)
                    {
                        if (retryCnt < RETRY_COUNT_EVAL)
                        {
                            StoreData retrySt = new StoreData(storeDataDic[id], commonMap);
                            retryList.Add(retrySt);
                            retryCnt++;
                        }
                    }
                }
            }

            /* 元に戻す */
            storeDataDic.Clear();   /* クリア */
            tmpList.Clear();
            foreach (KeyValuePair<int, StoreData> pair in copyStoreDic)
            {
                storeDataDic.Add(pair.Key, pair.Value);
            }
            RetrySearch.Enqueue(retryList);
        }
Exemplo n.º 3
0
 private void retryDataSet()
 {
     List<StoreData> retryData = new List<StoreData>();
     retryData = RetrySearch.Dequeue();
     for (int i=0; i < retryData.Count; i++)
     {
         searchQueId.Enqueue(retryData[i].ID);
         StoreData st = new StoreData(retryData[i], commonMap);
         if (storeDataDic.ContainsKey(st.ID) != true)
         {
             storeDataDic.Add(st.ID, st);
         }
     }
 }
Exemplo n.º 4
0
        /* 探索(並列) */
        private void queueSearchForParallel()
        {
            int searchSize = searchQueId.Count;
            int[] queIdArray = searchQueId.ToArray();
            int[] searchField = new int[9] { MY_FIELD, NEIGHT1, NEIGHT2, NEIGHT3, NEIGHT4, NEIGHT5, NEIGHT6, NEIGHT7, NEIGHT8 };
            int answerSize = 9999999;
            Object thisLock = new Object();

            Parallel.For(0, searchSize, i =>
            {
                /// Console.WriteLine(searchQueId.Count);
                int targetId = queIdArray[i];                       /* キューからID取得 */
                                                                    // string key;

                // key = dic.FirstOrDefault(x => x.Value.Equals(targetId)).Key;          /* dicのValue(ID)よりFields(Key)を取得 */
                // if (key != null)
                //{
                //  string strFields = key.ToString();                          /* キーをStirngへ変換 */

                if (storeDataDic.ContainsKey(targetId) == true)
                {
                    StoreData tgtSD = storeDataDic[targetId];
                    AGI_CalcFields tgtAgiCF = new AGI_CalcFields(tgtSD.GetAgiCalcFields(), commonMap);    /* stringからAGI_CalcFieldsを復元 */
                    for (int idKind = tgtSD.lastAttackStone + 1; idKind <= tgtSD.lastAttackStone + SEARCH_KIND_MAX; idKind++)  /* SEARCH_KIND_MAX先分走査 */
                    {
                        /* 石の決定 */
                        if (idKind < agi.GetAgiStoneList().GetListSize()) /* 石最大オーバー */
                        {
                            bool setFlg = false;
                            for (int kind = 0; kind < (int)Kinds.REV_R270; kind++)
                            {
                                if (agi.GetAgiStoneList().GetAgiStoneKindsList(idKind).GetAGIStones(kind) == null)    /* 重複により削除された石 */
                                {
                                    continue;
                                }

                                int maxX = agi.GetAgiStoneList().GetAgiStoneKindsList(idKind).GetAGIStones(kind).GetXSize() - 2;
                                int maxY = agi.GetAgiStoneList().GetAgiStoneKindsList(idKind).GetAGIStones(kind).GetYSize() - 2;
                                int targetSize = Math.Max(maxX, maxY);
                                if (targetSize >= 8)
                                {
                                    targetSize = 8;
                                }
                                else if (targetSize <= 0)
                                {
                                    targetSize = 0;
                                }

                                for (int idx = 0; idx < tgtAgiCF.GetAgiCalcFieldsSize(); idx++)      /* 接地セル走査 */
                                {
                                    if ((tgtAgiCF.GetAgiCalcFieldNum(idx) == MY_FIELD) ||   /* 自フィールドまたは */

                                    ((tgtAgiCF.GetAgiCalcFieldNum(idx) < 0) && (tgtAgiCF.GetAgiCalcFieldNum(idx) >= searchField[targetSize])) || /* 近傍以内または */

                                    (((tgtAgiCF.GetAgiCalcFieldNum(idx) == WALL) || (tgtAgiCF.GetAgiCalcFieldNum(idx) == OBJECT)) &&  /* 壁または障害物であり、かつ */
                                    (
                                      ((tgtAgiCF.GetAgiCalcFieldNum(idx + 1) >= searchField[targetSize]) && (tgtAgiCF.GetAgiCalcFieldNum(idx + 1) < 0)) || /* 右隣りが近傍値 */
                                      ((tgtAgiCF.GetAgiCalcFieldNum(idx + F_MAX_X) >= searchField[targetSize]) && (tgtAgiCF.GetAgiCalcFieldNum(idx + F_MAX_X) < 0)) ||/* 下隣が近傍値 */
                                      ((tgtAgiCF.GetAgiCalcFieldNum(idx + (F_MAX_X + 1)) >= searchField[targetSize]) && (tgtAgiCF.GetAgiCalcFieldNum(idx + (F_MAX_X + 1)) < 0)) /* 右下が近傍値 */
                                    )
                                    )
                                    )

                                    {
                                        AGI_CalcFields resultF = placement(tgtAgiCF, idKind, kind, idx, true);   /* 配置確認 */
                                        if (resultF == null)  /* 配置失敗 */
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            int retId = storeFieldsForParallel(resultF, tgtSD.ID, idKind, kind, idx, tgtAgiCF, EVAL_C);
                                            if (retId == -1)  /* 書き込み失敗 */
                                            {
                                                continue;
                                            }
                                            else
                                            {
                                                setFlg = true;
                                                if (resultF.GetAgiCalcEmptySize() != 0)
                                                {
                                                    nextQueId.Enqueue(retId);
                                                }
                                                else
                                                {
                                                    lock (thisLock)
                                                    {
                                                        StoreData tmp;
                                                        if (saveStoreDataDic.ContainsKey(retId) == true)
                                                        {
                                                            tmp = new StoreData(saveStoreDataDic[retId], commonMap);

                                                            if (answerId.ContainsKey(retId) == true)
                                                            {

                                                                answerId[retId] = tmp;

                                                            }
                                                            else
                                                            {
                                                                answerId.Add(retId, tmp);
                                                            }

                                                        }
                                                        else
                                                        {
                                                            nextQueId.Enqueue(retId);
                                                        }
                                                    }
                                                }

                                            }
                                        }

                                    }

                                }
                            }

                            if (setFlg == false)
                            {
                                int retId = storeFieldsForParallelLast(tgtSD, idKind);
                                nextQueId.Enqueue(retId);
                            }
                        }
                        else
                        {
                            lock (thisLock)
                            {
                                if (answerSize < tgtSD.score)
                                {
                                    if (answerId.ContainsKey(targetId) == false)
                                    {
                                        answerId.Add(targetId, tgtSD);
                                    }
                                    answerSize = tgtSD.score;
                                }
                            }
                        }
                    }
                }
                else
                {
                    ;
                }

            });

            foreach (var key in saveStoreDataDic.Keys)
            {
                if (storeDataDic.ContainsKey(key) == true)
                {
                    if (saveStoreDataDic.ContainsKey(key) == true)
                    {
                        storeDataDic[key] = saveStoreDataDic[key];
                    }
                }
                else
                {
                    if (saveStoreDataDic.ContainsKey(key) == true)
                    {
                        storeDataDic.Add(key, saveStoreDataDic[key]);
                    }
                }
            }

            saveStoreDataDic.Clear();
            searchQueId = new ConcurrentQueue<int>();
        }
Exemplo n.º 5
0
        /* 問題文をディクショナリに格納 */
        private bool storeRootFields(AGI_CalcFields agicF)
        {
            bool result = true;

            /* 各種登録 */
            /* ID取得 */
            StoreData regist = new StoreData(false, agicF, commonMap);
            regist.ID = 0;  /* ROOTのID */
            /* store登録 */
            regist.evalue = 0.0F;
            regist.score = calcScore(agicF);
            storeDataDic.Add(regist.ID, regist);

            return result;
        }
Exemplo n.º 6
0
        /* StoreDataへ格納(並列) */
        private int storeFieldsForParallelLast(StoreData sdt, int lastAttack)
        {
            int retId = -1;

            /* 各種登録 */
            /* ID取得 */
            StoreData regist = new StoreData(true, sdt.GetAgiCalcFields(), commonMap);
            /* store登録 */

            /* stoneの登録(親のstoneを受け継ぐ) */
            for (int i = 0; i < sdt.GetStoneInfoSize(); i++)
            {
                regist.SetStoneInfo(sdt.GetStoneInfoId(i),
                    sdt.GetStoneInfoPlace(i), sdt.GetStoneInfoKind(i));
            }

            regist.score = sdt.score;

            for (int i = 0; i < sdt.GetEvalueListSize(); i++)
            {
                regist.SetEvalueList(sdt.GetEvalueInfo(i));
            }

            regist.lastAttackStone = lastAttack;

            Object thisLock = new Object();

            lock (thisLock)
            {
                if (saveStoreDataDic.ContainsKey(regist.ID) != true)
                {
                    saveStoreDataDic[regist.ID] = regist;
                }
            }
            retId = regist.ID;
            return retId;
        }
Exemplo n.º 7
0
        /* StoreDataへ格納(並列) */
        private int storeFieldsForParallel(AGI_CalcFields agicF, int parentId, int stoneId, int kinds, int placeIdx, AGI_CalcFields beforeAgiF, int evalKind)
        {
            int retId = -1;

            /* 各種登録 */
            /* ID取得 */
            StoreData regist = new StoreData(true, agicF, commonMap);
            /* store登録 */
            /* stoneの登録(親のstoneを受け継ぐ) */
            if (storeDataDic.ContainsKey(parentId) == true)
            {
                for (int i = 0; i < storeDataDic[parentId].GetStoneInfoSize(); i++)
                {
                    regist.SetStoneInfo(storeDataDic[parentId].GetStoneInfoId(i),
                        storeDataDic[parentId].GetStoneInfoPlace(i), storeDataDic[parentId].GetStoneInfoKind(i));
                }
                int passStoneSize = stoneId - regist.GetFinallyStoneId();
                regist.SetStoneInfo(stoneId, placeIdx, kinds);
                regist.score = calcScore(agicF);
                switch (evalKind)
                {
                    case EVAL_A:
                        /* サイズが大きいほど有利 */
                        regist.evalue = calcEvalInitial(agicF, beforeAgiF, smallOrderList[stoneId]);
                        break;
                    case EVAL_B:
                        regist.evalue = calcEvalDepth(agicF, beforeAgiF, smallOrderList[stoneId]);
                        break;
                    case EVAL_C:
                        regist.evalue = calcEvalSizeKai(agicF, beforeAgiF, regist.score, passStoneSize, smallOrderList[stoneId]);
                        break;
                    default:
                        regist.evalue = calcEvalSizeKai(agicF, beforeAgiF, regist.score, passStoneSize, smallOrderList[stoneId]);
                        break;
                }

                for (int i = 0; i < storeDataDic[parentId].GetEvalueListSize(); i++)
                {
                    regist.SetEvalueList(storeDataDic[parentId].GetEvalueInfo(i));
                }

                regist.SetEvalueList(regist.evalue);

                regist.lastAttackStone = stoneId;

                Object thisLock = new Object();

                lock (thisLock)
                {
                    if (saveStoreDataDic.ContainsKey(regist.ID) != true)
                    {
                        try
                        {
                            saveStoreDataDic.Add(regist.ID, regist);
                        }
                        catch (System.ArgumentException)
                        {
                            saveStoreDataDic[regist.ID] = regist;
                        }
                    }
                    else
                    {
                        saveStoreDataDic[regist.ID] = regist;
                    }
                }
                retId = regist.ID;
            }
            return retId;
        }
Exemplo n.º 8
0
        /* StoreDataへ格納 */
        private int storeFields(AGI_CalcFields agicF, int parentId, int stoneId, int kinds, int placeIdx, AGI_CalcFields beforeAgiF, int evalKind)
        {
            int retId = -1;

            /* 各種登録 */

            /* ID取得 */
            StoreData regist = new StoreData(true, agicF, commonMap);
            /* stoneの登録(親のstoneを受け継ぐ) */
            for (int i = 0; i < storeDataDic[parentId].GetStoneInfoSize(); i++)
            {
                regist.SetStoneInfo(storeDataDic[parentId].GetStoneInfoId(i),
                    storeDataDic[parentId].GetStoneInfoPlace(i), storeDataDic[parentId].GetStoneInfoKind(i));
            }
            int passSize = regist.GetFinallyStoneId() - stoneId;
            regist.SetStoneInfo(stoneId, placeIdx, kinds);
            regist.score = calcScore(agicF);
            if (evalKind == EVAL_A)
            {  /* サイズが大きいほど有利 */
                regist.evalue = calcEvalInitial(agicF, beforeAgiF, smallOrderList[stoneId]);
            }
            else if (evalKind == EVAL_B)
            {
                regist.evalue = calcEvalDepth(agicF, beforeAgiF, smallOrderList[stoneId]);
            }
            else
            {
                regist.evalue = calcEvalSizeKai(agicF, beforeAgiF, regist.score, passSize, smallOrderList[stoneId]);
            }
            regist.closed = agicF.closed;
            storeDataDic.Add(regist.ID, regist);
            regist.lastAttackStone = stoneId;
            retId = regist.ID;
            return retId;
        }
Exemplo n.º 9
0
        private void step2()
        {
            Dictionary<int, float> tmpMap = new Dictionary<int, float>();
            Dictionary<int, StoreData> copyStoreDic = new Dictionary<int, StoreData>();
            List<StoreData> retryList = new List<StoreData>();
            int retryCnt = 0;

            /* map作成(ID, evalue) */
            for (int i = 1; i < storeDataDic.Count; i++)
            {
                if (storeDataDic.ContainsKey(i) == true)
                {
                    tmpMap.Add(storeDataDic[i].ID, storeDataDic[i].evalue);
                }
            }

            /* mapソート */
            var map = tmpMap.OrderByDescending((x) => x.Value); /* 降順ソート */
            int[] saveSeeds = new int[INIT_SEARCH_IDX];
            for (int i = 0; i < INIT_SEARCH_IDX; i++)
            {
                saveSeeds[i] = 0;
            }

            /* 評価値が優秀な種/評価値が優秀であり石ファイルが異なる種 */
            int count = 0;
            /* INIT_CHOISE_FOR_EVAL_NUM分Queueに格納 */
            foreach (var data in map)
            {
                int id = data.Key;

                if (count < INIT_CHOISE_FOR_EVAL_NUM)
                {
                    if (searchQueId.Contains(id) != true)
                    {
                        searchQueId.Enqueue(id);
                        if (copyStoreDic.ContainsKey(id) != true)
                        {
                            copyStoreDic.Add(id, storeDataDic[id]);
                        }
                        count++;
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    /* INIT_CHOISE_FOR_EVAL_NUM分Queueに格納 */
                    if (saveSeeds[storeDataDic[id].GetFinallyStoneId()] < INIT_SEEDS_SAVE)
                    {
                        if (searchQueId.Contains(id) != true)
                        {
                            searchQueId.Enqueue(id);
                            if (copyStoreDic.ContainsKey(id) != true)
                            {
                                copyStoreDic.Add(id, storeDataDic[id]);
                            }
                            saveSeeds[storeDataDic[id].GetFinallyStoneId()]++;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        ;
                    }
                }

                /* リトライサーチ用 */
                if (retryCnt < RETRY_COUNT_EVAL)
                {
                    StoreData retrySt = new StoreData(storeDataDic[id], commonMap);
                    retryList.Add(retrySt);
                    retryCnt++;
                }

            }

            uint max = (uint)tmpMap.Count;

            /* リトライサーチ用 */
            for (retryCnt = 0; retryCnt < RETRY_COUNT_RAND; retryCnt++)
            {
                if (max != 0)
                {
                    int idx = (int)rand.NextMax(max - 1);
                    if (tmpMap.ContainsKey(idx) == true)
                    {
                        if (retryCnt < RETRY_COUNT_EVAL)
                        {
                            if (storeDataDic.ContainsKey(idx) == true)
                            {

                                StoreData retrySt = new StoreData(storeDataDic[idx], commonMap);
                                retryList.Add(retrySt);
                                retryCnt++;
                            }
                        }
                    }
                }
            }

            RetrySearch.Enqueue(retryList);

            /* 元に戻す */
            storeDataDic.Clear();   /* クリア */
            foreach (KeyValuePair<int, StoreData> pair in copyStoreDic)
            {
                storeDataDic.Add(pair.Key, pair.Value);
            }
        }