Esempio n. 1
0
        private void sAToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form_SA form_SA = new Form_SA(geometryRoot, 100);

            form_SA.ShowDialog();
            if (form_SA.Finished)
            {
                TWO_MESHS_sourceObj = form_SA.TWO_MESHS_sourceObj;
                TWO_MESHS_targetObj = form_SA.TWO_MESHS_targetObj;

                displayMode = DisplayMode.TWO_MESHS;
            }
        }
Esempio n. 2
0
        //private double[] GetRandomData_DimSeven(int dimValue)
        //{
        //    double[] newData = new double[DataLength];
        //    for (int i = 0; i < DataLength; i++)
        //    {
        //        newData[i] = currentData[i];
        //    }
        //    newData[7] = dimValue;
        //    return newData;
        //}

        //public void DoSA(Form_SA form)
        //{

        //    double Temperature = InitTemperature;

        //    int outerIterCount = 0;
        //    int totalIterCount = 0;
        //    int totalAccept = 0;

        //    int no_Accept_Count = 0;

        //    for (outerIterCount = 0; outerIterCount < outerIterCount_Limit; outerIterCount++)
        //    {
        //        //Temperature = (outerIterCount_Limit - outerIterCount) * InitTemperature / outerIterCount_Limit;
        //        Temperature = Temperature * alphaT;

        //        int InnerAcceptCount = 0;

        //        int innerIterCount = 0;

        //        bool has_Accept = false;


        //        //int dim_i = random.Next(DataLength);
        //        bool[] dim_visited = { false, false, false, false, false, false, false, false};

        //        for (int j = 0; j < 8; j++)
        //        {
        //            int dim_i = 0;

        //            while (true)
        //            {
        //                dim_i = random.Next(8);
        //                if(!dim_visited[dim_i])
        //                {
        //                    dim_visited[dim_i] = true;
        //                    break;
        //                }
        //            }

        //            if (dim_i == 7)
        //            {

        //                bool[] mirror_visited = { false, false, false, false, false, false, false, false };
        //                for (int i = 0; i < 8; i++)
        //                {

        //                    int mirror_i = 0;

        //                    while (true)
        //                    {
        //                        mirror_i = random.Next(8);
        //                        if (!mirror_visited[mirror_i])
        //                        {
        //                            mirror_visited[mirror_i] = true;
        //                            break;
        //                        }
        //                    }

        //                    innerIterCount++;
        //                    totalIterCount++;
        //                    Record.Add(currentValue);

        //                    double[] nowData = GetRandomData_DimSeven(mirror_i);
        //                    double nowValue = TheValueFunction(nowData);

        //                    form.SetVal(Temperature, outerIterCount, innerIterCount, totalIterCount, totalAccept, nowValue, currentMinValue);

        //                    double deltaValue = nowValue - currentValue;
        //                    if (deltaValue < 0)
        //                    {
        //                        currentValue = nowValue;
        //                        currentData = nowData;
        //                        if (nowValue < currentMinValue)
        //                        {
        //                            currentMinValue = nowValue;
        //                            currentMinData = nowData;
        //                        }
        //                        InnerAcceptCount++;
        //                        totalAccept++;
        //                        has_Accept = true;
        //                        if (InnerAcceptCount > innerIterCount_Accept_Limit)
        //                            break;
        //                    }
        //                    else
        //                    {
        //                        double probability = Math.Exp(-(deltaValue / Temperature));
        //                        double lambda = random.NextDouble();
        //                        if (probability > lambda)
        //                        {
        //                            currentValue = nowValue;
        //                            currentData = nowData;
        //                            InnerAcceptCount++;
        //                            totalAccept++;
        //                            break;
        //                        }
        //                    }
        //                }
        //            }
        //            else
        //            {

        //                for (int i = 0; i < innerIterCount_Limit; i++)
        //                {
        //                    innerIterCount++;
        //                    totalIterCount++;
        //                    Record.Add(currentValue);


        //                    if (i > innerIterCount_Limit)
        //                        break;
        //                    if (!form.IsOpening)
        //                        break;

        //                    double[] nowData = GetRandomData_OneDim(dim_i);
        //                    double nowValue = TheValueFunction(nowData);

        //                    form.SetVal(Temperature, outerIterCount, innerIterCount, totalIterCount, totalAccept, nowValue, currentMinValue);

        //                    double deltaValue = nowValue - currentValue;

        //                    if (deltaValue < 0)
        //                    {
        //                        currentValue = nowValue;
        //                        currentData = nowData;
        //                        if (nowValue < currentMinValue)
        //                        {
        //                            currentMinValue = nowValue;
        //                            currentMinData = nowData;
        //                        }
        //                        InnerAcceptCount++;
        //                        totalAccept++;
        //                        has_Accept = true;
        //                        if (InnerAcceptCount > innerIterCount_Accept_Limit)
        //                            break;
        //                    }
        //                    else
        //                    {
        //                        double probability = Math.Exp(-(deltaValue / Temperature));
        //                        double lambda = random.NextDouble();
        //                        if (probability > lambda)
        //                        {
        //                            currentValue = nowValue;
        //                            currentData = nowData;
        //                            InnerAcceptCount++;
        //                            totalAccept++;
        //                            if (InnerAcceptCount > innerIterCount_Accept_Limit)
        //                                break;
        //                        }
        //                    }
        //                }//inner
        //            }
        //        }//middle

        //        if (!has_Accept)
        //            no_Accept_Count++;
        //        if (no_Accept_Count > no_Accept_Limit)
        //            break;
        //        if (!form.IsOpening)
        //            break;
        //    }//outer

        //}

        public void DoSA(Form_SA form)
        {
            double Temperature = InitTemperature;

            int outerIterCount = 0;
            int totalIterCount = 0;
            int totalAccept    = 0;

            int currentMirror = 0;

            int[] MirrorsAcceptRate = { MirrorsAcceptRate_Init, MirrorsAcceptRate_Init, MirrorsAcceptRate_Init, MirrorsAcceptRate_Init, MirrorsAcceptRate_Init, MirrorsAcceptRate_Init, MirrorsAcceptRate_Init, MirrorsAcceptRate_Init };
            int   no_Accept_Count   = 0;

            for (outerIterCount = 0; outerIterCount < outerIterCount_Limit; outerIterCount++)
            {
                //Temperature = (outerIterCount_Limit - outerIterCount) * InitTemperature / outerIterCount_Limit;
                Temperature = Temperature * alphaT;

                int InnerAcceptCount = 0;

                int innerIterCount = 0;

                bool has_Accept = false;


                //int dim_i = random.Next(DataLength);
                //bool[] dim_visited = { false, false, false, false, false, false, false, false };

                for (int middleIterCount = 0; middleIterCount < middleIterCount_Limit; middleIterCount++)
                {
                    //int dim_i = 0;

                    //while (true)
                    //{
                    //    dim_i = random.Next(7);
                    //    if (!dim_visited[dim_i])
                    //    {
                    //        dim_visited[dim_i] = true;
                    //        break;
                    //    }
                    //}


                    //if (random.Next(MirrorsChangeRate) == 0)
                    //{
                    //    ChangeCurrentMirror(ref currentMirror, MirrorsAcceptRate);//change mirror
                    //}
                    ChangeCurrentMirror(ref currentMirror, MirrorsAcceptRate);//change mirror

                    for (innerIterCount = 0; innerIterCount < innerIterCount_Limit; innerIterCount++)
                    {
                        if (innerIterCount > innerIterCount_Limit)
                        {
                            break;
                        }
                        if (!form.IsOpening)
                        {
                            break;
                        }

                        totalIterCount++;
                        Record.Add(currentValue);

                        int      dim_i   = random.Next(7);
                        double[] nowData = GetRandomData_OneDim(dim_i, currentMirror);

                        double nowValue = TheValueFunction(nowData);

                        form.SetVal(Temperature, outerIterCount, innerIterCount, totalIterCount, totalAccept, nowValue, currentMinValue);

                        double deltaValue = nowValue - currentValue;

                        if (deltaValue < 0)
                        {
                            currentValue = nowValue;
                            //currentData_byMirrors[currentMirror] = nowData;
                            currentData = nowData;

                            MirrorsAcceptRate[currentMirror]++;

                            if (nowValue < currentMinValue)
                            {
                                currentMinValue = nowValue;
                                currentMinData  = nowData;
                                currentMinData_byMirrors[currentMirror] = nowData;
                            }

                            //if (nowValue < currentMinValue_byMirrors[currentMirror])
                            //{
                            //    currentMinValue_byMirrors[currentMirror] = nowValue;
                            //}


                            InnerAcceptCount++;
                            totalAccept++;
                            has_Accept = true;
                            if (InnerAcceptCount > innerIterCount_Accept_Limit)
                            {
                                break;
                            }
                        }
                        else
                        {
                            double probability = Math.Exp(-(deltaValue / Temperature));
                            double lambda      = random.NextDouble();
                            if (probability > lambda)
                            {
                                currentValue = nowValue;
                                //currentData_byMirrors[currentMirror] = nowData;
                                currentData = nowData;
                                InnerAcceptCount++;
                                totalAccept++;
                                if (InnerAcceptCount > innerIterCount_Accept_Limit)
                                {
                                    break;
                                }
                            }
                        }
                    } //inner
                }     //middle

                if (!has_Accept)
                {
                    no_Accept_Count++;
                }
                if (no_Accept_Count > no_Accept_Limit)
                {
                    break;
                }
                if (!form.IsOpening)
                {
                    break;
                }
            }//outer
        }