public FitnessPoint GetOptimalPoint(AlgorithmType type, string functionExpression, List <string> argumentsSymbols, List <Compartment> ranges) { if (argumentsSymbols.Count != ranges.Count) { throw new AlgorithmException(AlgorithmExceptionType.DifferenceArguments); } for (int i = 0; i < argumentsSymbols.Count; i++) { if (!functionExpression.Contains(argumentsSymbols[i])) { throw new AlgorithmException(AlgorithmExceptionType.WrongParametersArguments); } } Function function = new Function(functionExpression, argumentsSymbols); int agentsCount = 0; for (int i = 0; i < ranges.Count; i++) { agentsCount += (int)Math.Abs(ranges[i].Min - ranges[i].Max); } for (int i = 0; i < this.algorithms.Count; i++) { if (this.algorithms[i].AlgorithmType == type) { this.algorithms[i].SetNewAndReset(function, agentsCount, ranges); } } List <FitnessPoint> bestPoints = new List <FitnessPoint>(); for (int i = 0; i < this.Iterations; i++) { for (int j = 0; j < this.algorithms.Count; j++) { if (this.algorithms[j].AlgorithmType == type) { FitnessPoint best_point = this.algorithms[j].GenerateBestValue(); bestPoints.Add(best_point); } } } FitnessPoint result = null; if (type == AlgorithmType.Maximum) { result = bestPoints.Max(); } else if (type == AlgorithmType.Minimum) { result = bestPoints.Min(); } return(result); }
private FitnessPoint FindBestPointInRange(AlgorithmType algorithmType, List <FitnessPoint> options, params Compartment[] compartments) { FitnessPoint result = null; FitnessPoint tempResult = null; int numberOfRanges = compartments.Length; List <FitnessPoint> cloneOptions = new List <FitnessPoint>(options); switch (algorithmType) { case AlgorithmType.Maximum: { FitnessPoint tempBest = cloneOptions.Max(); for (int i = 0; i < numberOfRanges; i++) { if (tempBest.Axis.Values[i] >= compartments[i].Min && tempBest.Axis.Values[i] <= compartments[i].Max) { tempResult = new FitnessPoint(tempBest); } else { i = -1; cloneOptions.Remove(tempBest); tempBest = cloneOptions.Max(); tempResult = null; } } result = tempResult; break; } case AlgorithmType.Minimum: { FitnessPoint tempBest = cloneOptions.Min(); for (int i = 0; i < numberOfRanges; i++) { if (tempBest.Axis.Values[i] >= compartments[i].Min && tempBest.Axis.Values[i] <= compartments[i].Max) { tempResult = new FitnessPoint(tempBest); } else { i = -1; cloneOptions.Remove(tempBest); tempBest = cloneOptions.Min(); tempResult = null; } } result = tempResult; break; } } return(result); }
public override FitnessPoint GenerateBestValue() { if (this.function == null) { throw new AlgorithmException(AlgorithmExceptionType.ParametersNotSeted); } FitnessPoint result = null; while (true) { if (!this.NextIteration()) { result = this.particles.Max().Position; break; } } this.Reset(); return(result); }
public override async Task <FitnessPoint> GenerateBestValueAsync() { if (this.function == null) { throw new AlgorithmException(AlgorithmExceptionType.ParametersNotSeted); } FitnessPoint result = null; while (true) { bool isNotEnd = await Task.Run(() => this.NextIteration()); if (!isNotEnd) { result = this.particles.Max().Position; break; } } this.Reset(); return(result); }
private async void button_search_Click(object sender, EventArgs e) { this.button_search.Enabled = false; // CODE FOR SEARCHING MIN/MAX ALGHORITM // clear listbox results listView_results.Items.Clear(); HeuristicAlgorithms myHeuristicAlgorithms = new HeuristicAlgorithms(); // list of points List <FitnessPoint> myListOfFitnessPoints = new List <FitnessPoint>(); // best fitness point _bestValue = new FitnessPoint(); // assign function expresion to variable _myFunctionExpression = textBox_function.Text; // create list of argument symbols AssignArgumentSymbols(); // create list of ranges for each variable _myRanges = new List <Compartment>(); // validation range of each variable switch (_myArgumentsSymbol.Count) { case 1: { if (!ValidateRangeValues(this.numericUpDown_X_range_from.Value, this.numericUpDown_X_range_to.Value)) { this.button_search.Enabled = true; return; } break; } case 2: { if (!ValidateRangeValues(this.numericUpDown_X_range_from.Value, this.numericUpDown_X_range_to.Value)) { this.button_search.Enabled = true; return; } if (!ValidateRangeValues(this.numericUpDown_Y_range_from.Value, this.numericUpDown_Y_range_to.Value)) { this.button_search.Enabled = true; return; } break; } case 3: { if (!ValidateRangeValues(this.numericUpDown_X_range_from.Value, this.numericUpDown_X_range_to.Value)) { this.button_search.Enabled = true; return; } if (!ValidateRangeValues(this.numericUpDown_Y_range_from.Value, this.numericUpDown_Y_range_to.Value)) { this.button_search.Enabled = true; return; } if (!ValidateRangeValues(this.numericUpDown_Z_range_from.Value, this.numericUpDown_Z_range_to.Value)) { this.button_search.Enabled = true; return; } break; } default: break; } if (!IsDigitsOnly(_myFunctionExpression)) { try { // find list of fitness points myListOfFitnessPoints = await myHeuristicAlgorithms.GetAllOptimalPointsAsync(_myAlgorithmType, _myFunctionExpression, _myArgumentsSymbol, _myRanges); // find best fitness point (minimum or maximum) switch (_myArgumentsSymbol.Count) { case 1: { _bestValue = this.FindBestPointInRange(_myAlgorithmType, myListOfFitnessPoints, new Compartment((double)this.numericUpDown_X_range_from.Value, (double)this.numericUpDown_X_range_to.Value)); break; } case 2: { _bestValue = this.FindBestPointInRange(_myAlgorithmType, myListOfFitnessPoints, new Compartment((double)this.numericUpDown_X_range_from.Value, (double)this.numericUpDown_X_range_to.Value), new Compartment((double)this.numericUpDown_Y_range_from.Value, (double)this.numericUpDown_Y_range_to.Value)); break; } case 3: { _bestValue = this.FindBestPointInRange(_myAlgorithmType, myListOfFitnessPoints, new Compartment((double)this.numericUpDown_X_range_from.Value, (double)this.numericUpDown_X_range_to.Value), new Compartment((double)this.numericUpDown_Y_range_from.Value, (double)this.numericUpDown_Y_range_to.Value), new Compartment((double)this.numericUpDown_Z_range_from.Value, (double)this.numericUpDown_Z_range_to.Value)); break; } } } catch (AlgorithmException ex) { switch (ex.Fail) { case AlgorithmExceptionType.ParametersNotSeted: AddResult("Parameters no set"); break; case AlgorithmExceptionType.FunctionNotExecuted: AddResult("Function not executed"); break; case AlgorithmExceptionType.DifferenceArguments: AddResult("Difference arguments"); break; case AlgorithmExceptionType.FunctionNotSeted: AddResult("Function no set"); break; case AlgorithmExceptionType.WrongParametersArguments: AddResult("Wrong parameters arguments"); break; case AlgorithmExceptionType.BadRanges: AddResult("Bad ranges"); break; } this.button_search.Enabled = true; return; } double twinRangeFitness = (this.numericUpDown_precison.Value == 0) ? 0 : 1; for (int i = 0; i < (int)this.numericUpDown_precison.Value; i++) { twinRangeFitness /= 10; } twinRangeFitness = _bestValue.Fitness - twinRangeFitness; foreach (FitnessPoint item in myListOfFitnessPoints) { if (item.Fitness == _bestValue.Fitness) { AddResult(item.Fitness, item.Axis.Values, Color.Green); continue; } if (Math.Round(item.Fitness, (int)this.numericUpDown_precison.Value, MidpointRounding.AwayFromZero) <= _bestValue.Fitness && Math.Round(item.Fitness, (int)this.numericUpDown_precison.Value, MidpointRounding.AwayFromZero) >= twinRangeFitness) { AddResult(item.Fitness, item.Axis.Values, Color.Orange); } else { AddResult(item.Fitness, item.Axis.Values, Color.White); } } } // display graph for function with 1 variable of for constant functions if (_myArgumentsSymbol.Count == 1) { this.myDisplayFunction2D.ClearAll(); this.myDisplayFunction2D.Load(_myFunctionExpression); // set axis X range _axisXRange = new Compartment(_myRanges[0].Min, _myRanges[0].Max); // show function graph this.myDisplayFunction2D.Graph(_axisXRange); } // display graph for function with 2 variable else if (_myArgumentsSymbol.Count == 2) { this.myDisplayFunction3D.ClearPoints(); this.myDisplayFunction3D.Load(_myFunctionExpression); // set axis X range _axisXRange = new Compartment(_myRanges[0].Min, _myRanges[0].Max); // set axis Y range _axisYRange = new Compartment(_myRanges[1].Min, _myRanges[1].Max); // show function graph await this.myDisplayFunction3D.GraphAync(_axisXRange, _axisYRange); } this.button_search.Enabled = true; }