예제 #1
0
        public static String HarmonySearchAlgorithm(Function f, int NI, int HMS, double HMCR, double PAR, double BW, double[] PVBmin, double[] PVBmax, ref double[] bestSolution)
        {
            String output            = "";
            int    numberofVariables = f.getArgumentsNumber();

            double[,] HMtab = new double[HMS, numberofVariables + 1];
            int iterations = 0;

            HarmonyTool.InitializeHM(HMtab, PVBmin, PVBmax);
            HarmonyTool.PerformHM(f, HMtab);
            HarmonyTool.SortHM(HMtab);
            double[] newVec = new double[HMtab.GetLength(1)];
            for (int i = 0; i < NI; i++)
            {
                newVec = HarmonyTool.NewHarmonyVector(HMtab, HMCR, PAR, BW, PVBmin, PVBmax);
                HarmonyTool.UpdateHM(HMtab, f, newVec);
                if (NI - iterations <= 20)
                {
                    output += "Iteration number: " + iterations.ToString() + "\n" + HarmonyTool.DisplayBestSolution(HMtab) + "\n";
                }
                iterations++;
            }
            if (HMtab.GetLength(1) - 1 == 2)
            {
                bestSolution[0] = HMtab[0, 0];
                bestSolution[1] = HMtab[0, 1];
            }
            else
            {
                bestSolution[0] = Double.NaN;
                bestSolution[1] = Double.NaN;
            }

            return(output);
        }
예제 #2
0
 //calculating f(x) value for new vector
 public static void PperformVec(Function f, double[] vec)
 {
     double[] vecOfValues = new double[f.getArgumentsNumber()];
     for (int i = 0; i < f.getArgumentsNumber(); i++)
     {
         vecOfValues[i] = vec[i]; //assigning value to temporary vector from new found vector
     }
     vec[vec.GetLength(0) - 1] = HarmonyTool.EvaluateFun(f, vecOfValues);
 }
예제 #3
0
 //calculating f(x) for every vector in HM
 public static void PerformHM(Function f, double[,] HMtab)
 {
     double[] valuesOfVariables = new double[HMtab.GetLength(1) - 1];
     for (int i = 0; i < HMtab.GetLength(0); i++)
     {
         for (int j = 0; j < HMtab.GetLength(1) - 1; j++) //going through parameters excluding column with f(x)
         {
             valuesOfVariables[j] = HMtab[i, j];
         }
         HMtab[i, (HMtab.GetLength(1) - 1)] = HarmonyTool.EvaluateFun(f, valuesOfVariables);
     }
 }
예제 #4
0
 public static void UpdateHM(double[,] HMtab, Function f, double[] vec)
 {
     HarmonyTool.PperformVec(f, vec);
     if (vec[vec.GetLength(0) - 1] < HMtab[HMtab.GetLength(0) - 1, HMtab.GetLength(1) - 1])
     {
         for (int i = 0; i < HMtab.GetLength(1); i++)
         {
             HMtab[HMtab.GetLength(0) - 1, i] = vec[i];
         }
         HarmonyTool.SortHM(HMtab);
     }
 }
예제 #5
0
        public static void SortHM(double[,] HMtab)
        {
            int valuePosition = HMtab.GetLength(1) - 1;

            for (int i = 0; i < HMtab.GetLength(0) - 1; i++)
            {
                for (int j = 0; j < HMtab.GetLength(0) - 1; j++)
                {
                    if (HMtab[j, valuePosition] > HMtab[j + 1, valuePosition])
                    {
                        for (int x = 0; x < HMtab.GetLength(1); x++)
                        {
                            HarmonyTool.Swap(HMtab, j, x, j + 1, x);
                        }
                    }
                }
            }
        }
예제 #6
0
 private void Calculate_Btn_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         double   HMCR    = Double.Parse(HMCR_textbox.Text);
         double   PAR     = Double.Parse(PAR_textbox.Text);
         int      NI      = int.Parse(NI_TextBox.Text);
         int      HMS     = int.Parse(HMS_TextBox.Text);
         double   BW      = Double.Parse(BW_TextBox.Text);
         String   fun_str = ObjFun_ComboBox.Text.ToString();
         double[] PVBmax  = ParsePVB(PVBmax_TextBox.Text, ';');
         double[] PVBmin  = ParsePVB(PVBmin_TextBox.Text, ';');
         bool     errFlag = false;
         for (int i = 0; i < PVBmax.Length; i++)
         {
             if (PVBmax[i] <= PVBmin[i])
             {
                 errFlag = true;
             }
         }
         if (errFlag)
         {
             MessageBox.Show("PVB not given correctly! Check bounds", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
         }
         else
         {
             Function fn     = new Function(fun_str);
             String   result = HarmonyTool.HarmonySearchAlgorithm(fn, NI, HMS, HMCR, PAR, BW, PVBmin, PVBmax, ref bestSolution);
             Result_TextBox.Text = result;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("A handled exception just occurred: " + ex.Message + "\n Check if forms are filled properly!", "Exception", MessageBoxButton.OK, MessageBoxImage.Information);
     }
 }
예제 #7
0
        private void Plot_btn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var tmp = new PlotModel {
                    Title = "Contour Plot", Subtitle = "f(x1,x2)"
                };

                tmp.Axes.Add(new LinearColorAxis
                {
                    Position = OxyPlot.Axes.AxisPosition.Right,
                    //Palette = OxyPalettes.Jet(500)
                    Palette = OxyPalettes.Rainbow(100)
                });
                double[] PVBmax  = ParsePVB(PVBmax_TextBox.Text, ';');
                double[] PVBmin  = ParsePVB(PVBmin_TextBox.Text, ';');
                bool     errFlag = false;
                for (int i = 0; i < PVBmax.Length; i++)
                {
                    if (PVBmax[i] <= PVBmin[i])
                    {
                        errFlag = true;
                    }
                }
                String   fun_str = ObjFun_ComboBox.Text.ToString();
                Function fn      = new Function(fun_str);
                double   x1_min;
                double   x1_max;
                double   x2_min;
                double   x2_max;
                double   tmpMaxValue = Double.MinValue;

                if (PVBmin.GetLength(0) == 2 && !errFlag)
                {
                    x1_min = PVBmin[0];
                    x1_max = PVBmax[0];
                    x2_min = PVBmin[1];
                    x2_max = PVBmax[1];
                    var x1x1 = ArrayBuilder.CreateVector(x1_min, x1_max, 100);
                    var x2x2 = ArrayBuilder.CreateVector(x2_min, x2_max, 100);
                    double[,] peaksData = new double[x1x1.GetLength(0), x2x2.GetLength(0)];
                    double[] xy_tab = new double[2];

                    for (int i = 0; i < x1x1.GetLength(0); i++)
                    {
                        for (int j = 0; j < x2x2.GetLength(0); j++)
                        {
                            xy_tab[0]       = x1x1[i];
                            xy_tab[1]       = x2x2[j];
                            peaksData[i, j] = HarmonyTool.EvaluateFun(fn, xy_tab);
                            if (peaksData[i, j] > tmpMaxValue)
                            {
                                tmpMaxValue = peaksData[i, j];
                            }
                        }
                    }


                    var heatMapSeries = new HeatMapSeries
                    {
                        X0           = x1_min,
                        X1           = x1_max,
                        Y0           = x2_min,
                        Y1           = x2_max,
                        Interpolate  = true,
                        RenderMethod = HeatMapRenderMethod.Bitmap,
                        Data         = peaksData
                    };
                    tmp.Series.Add(heatMapSeries);


                    var cs = new ContourSeries
                    {
                        Color             = OxyColors.Black,
                        LabelBackground   = OxyColors.Transparent,
                        ColumnCoordinates = x1x1,
                        RowCoordinates    = x2x2,
                        Data = peaksData
                    };
                    tmp.Series.Add(cs);

                    if (solutionCheckBox.IsChecked == true)
                    {
                        var sc = new ScatterSeries
                        {
                            MarkerType = MarkerType.Circle
                        };
                        sc.BinSize               = 10;
                        sc.MarkerType            = MarkerType.Cross;
                        sc.MarkerStrokeThickness = 3;
                        sc.MarkerStroke          = OxyColors.Black;
                        double x1 = bestSolution[0];
                        double x2 = bestSolution[1];
                        sc.Points.Add(new OxyPlot.Series.ScatterPoint(x1, x2, 5, tmpMaxValue));
                        tmp.Series.Add(sc);
                    }
                    GetMainViewModel().MyModel = tmp;
                }
                else
                {
                    MessageBox.Show("Number of variables does not equal n = 2 or PVB not given correctly! Check forms", "Exception", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("A handled exception just occurred: " + ex.Message + "\n Check if forms are filled properly! Domain of function has to be set in PVB forms", "Exception", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }