protected override IScope[] Select(List <IScope> scopes) { int count = NumberOfSelectedSubScopesParameter.ActualValue.Value; bool copy = CopySelectedParameter.Value.Value; IRandom random = RandomParameter.ActualValue; bool maximization = MaximizationParameter.ActualValue.Value; ItemArray <DoubleValue> qualities = QualityParameter.ActualValue; IScope[] selected = new IScope[count]; // create a list for each scope that contains the scope's index in the original scope list and its lots var temp = qualities.Where(x => IsValidQuality(x.Value)).Select((x, index) => new { index, x.Value }); if (maximization) { temp = temp.OrderBy(x => x.Value); } else { temp = temp.OrderByDescending(x => x.Value); } var list = temp.Select((x, index) => new { x.index, lots = index + 1 }).ToList(); //check if list with indexes is as long as the original scope list //otherwise invalid quality values were filtered if (list.Count != scopes.Count) { throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate."); } int lotSum = list.Count * (list.Count + 1) / 2; for (int i = 0; i < count; i++) { int selectedLot = random.Next(lotSum) + 1; int j = 0; int currentLot = list[j].lots; while (currentLot < selectedLot) { j++; currentLot += list[j].lots; } if (copy) { selected[i] = (IScope)scopes[list[j].index].Clone(); } else { selected[i] = scopes[list[j].index]; scopes[list[j].index] = null; lotSum -= list[j].lots; list.RemoveAt(j); } } if (!copy) { scopes.RemoveAll(x => x == null); } return(selected); }
public override IOperation Apply() { ItemArray <BoolValue> tabu = IsTabuParameter.ActualValue; if (tabu.Length > 0) { PercentValue value = PercentTabuParameter.ActualValue; if (value == null) { value = new PercentValue(); PercentTabuParameter.ActualValue = value; } value.Value = tabu.Where(x => x.Value).Count() / (double)tabu.Length; ResultCollection results = ResultsParameter.ActualValue; if (results != null) { IResult result = null; results.TryGetValue(PercentTabuParameter.ActualName, out result); if (result != null) { result.Value = value; } else { results.Add(new Result(PercentTabuParameter.ActualName, "Indicates how much of the neighborhood is tabu.", (IItem)value.Clone())); } } } return(base.Apply()); }
protected override IScope[] Select(List <IScope> scopes) { int count = NumberOfSelectedSubScopesParameter.ActualValue.Value; bool copy = CopySelectedParameter.Value.Value; IRandom random = RandomParameter.ActualValue; bool maximization = MaximizationParameter.ActualValue.Value; ItemArray <DoubleValue> qualities = QualityParameter.ActualValue; IScope[] selected = new IScope[count]; double pressure = PressureParameter.ActualValue.Value; var ordered = qualities.Where(x => IsValidQuality(x.Value)).Select((x, index) => new KeyValuePair <int, double>(index, x.Value)).OrderBy(x => x.Value).ToList(); if (maximization) { ordered.Reverse(); } //check if list with indexes is as long as the original scope list //otherwise invalid quality values were filtered if (ordered.Count != scopes.Count) { throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate."); } int m = scopes.Count; for (int i = 0; i < count; i++) { double rand = 1 + random.NextDouble() * (Math.Pow(m, 1.0 / pressure) - 1); int selIdx = (int)Math.Floor(Math.Pow(rand, pressure) - 1); if (copy) { selected[i] = (IScope)scopes[ordered[selIdx].Key].Clone(); } else { int idx = ordered[selIdx].Key; selected[i] = scopes[idx]; scopes.RemoveAt(idx); ordered.RemoveAt(selIdx); for (int j = 0; j < ordered.Count; j++) { var o = ordered[j]; if (o.Key > idx) { ordered[j] = new KeyValuePair <int, double>(o.Key - 1, o.Value); } } m--; } } return(selected); }
protected override IScope[] Select(List <IScope> scopes) { int count = NumberOfSelectedSubScopesParameter.ActualValue.Value; bool copy = CopySelectedParameter.Value.Value; bool maximization = MaximizationParameter.ActualValue.Value; ItemArray <DoubleValue> qualities = QualityParameter.ActualValue; IScope[] selected = new IScope[count]; // create a list for each scope that contains the scope's index in the original scope list var temp = qualities.Where(x => IsValidQuality(x.Value)).Select((x, index) => new { index, x.Value }); if (maximization) { temp = temp.OrderBy(x => x.Value); } else { temp = temp.OrderByDescending(x => x.Value); } var list = temp.ToList(); //check if list with indexes is as long as the original scope list //otherwise invalid quality values were filtered if (list.Count != scopes.Count) { throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate."); } if (copy) { int j = 0; for (int i = 0; i < count; i++) { selected[i] = (IScope)scopes[list[j].index].Clone(); j++; if (j >= list.Count) { j = 0; } } } else { for (int i = 0; i < count; i++) { selected[i] = scopes[list[0].index]; scopes[list[0].index] = null; list.RemoveAt(0); } scopes.RemoveAll(x => x == null); } return(selected); }