private Candidate InferTypeArguments(Candidate candidate, IDictionary<Type, Type> typeArgumentMap, SymbolTable symbols) { return this.DispatchType != DispatchTypes.Method ? candidate : new Dictionary<Type, Type>(typeArgumentMap).Let(map => { if (map.Count == candidate.Method.GetGenericArguments().Length) { return candidate.Clone( member: candidate.Method != null && candidate.Method.IsGenericMethodDefinition ? candidate.Method.MakeGenericMethod(typeArgumentMap.ToArgumentArray()) : candidate.Member, arguments: candidate.ParameterMap .Select(_ => _.Item2 is AmbiguousLambdaExpression && _.Item1.GetDelegateSignature() != null ? ((AmbiguousLambdaExpression) _.Item2) .ApplyTypeArguments(_.Item1) .ApplyTypeArguments(map) .Reduce(symbols, _.Item1.ReplaceGenericArguments(map)) : _.Item2 ) .ToArray() ); } else { candidate.ParameterMap .Where(_ => !(_.Item1.IsGenericParameter ? EnumerableEx.Return(_.Item1) : _.Item1.GetGenericArguments() ).All(map.ContainsKey)) .ForEach(_ => { if (_.Item1.GetDelegateSignature() != null) { if (_.Item2 is AmbiguousLambdaExpression) { var method = _.Item1.GetDelegateSignature(); if (!method.ReturnType.Let(r => (r.IsGenericParameter ? new [] { r, } : r.GetGenericArguments()).Let(ts => ts.All(map.ContainsKey)) )) { method.GetParameters() .Select(p => p.ParameterType.Let(t => t.IsGenericParameter ? map.GetValue(t) : t )) .If(ts => ts.All(t => t != null), ts => map = new TypeNode(method.ReturnType).Match(map, ((AmbiguousLambdaExpression) _.Item2) .ApplyTypeArguments(ts) .Type(symbols) .GetDelegateSignature() .ReturnType ) ); } } else if (_.Item2 is LambdaExpression) { map = new TypeNode(_.Item1).Match(map, _.Item2.Type.GetDelegateSignature().GetDelegateType()); } } else if (_.Item1.ContainsGenericParameters) { (_.Item1.IsGenericParameter ? EnumerableEx.Return(Tuple.Create(_.Item1, _.Item2.Type)) : _.Item1.GetAppearingTypes() .Zip(_.Item2.Type.GetCorrespondingType(_.Item1).GetAppearingTypes(), Tuple.Create) .Where(t => t.Item1.IsGenericParameter) ).ForEach(t => map = new TypeNode(t.Item1).Match(map, t.Item2)); } }); return map.Keys.All(typeArgumentMap.ContainsKey) ? null : this.InferTypeArguments(candidate, map, symbols); } }); }
protected void calculateNextGeneration(List <Candidate> population, CostFunction costFunction) { List <Candidate> mirrorPopulation = null; List <Candidate> oldPopulation = (List <Candidate>)population.Clone(); switch (configuration().strategy) { case Strategy.Rand1Standard: { population.Shuffle(); List <Candidate> shuffledPop1 = (List <Candidate>)population.Clone(); population.Shuffle(); List <Candidate> shuffledPop2 = (List <Candidate>)population.Clone(); population.Shuffle(); mirrorPopulation = (List <Candidate>)shuffledPop1.Clone(); for (int popIter = 0; popIter < population.Count; popIter++) { population[popIter].values = population[popIter].values + configuration().stepsizeWeight *(shuffledPop1[popIter].values - shuffledPop2[popIter].values); } } break; case Strategy.BestMemberWithJitter: { population.Shuffle(); List <Candidate> shuffledPop1 = (List <Candidate>)population.Clone(); population.Shuffle(); Vector jitter = new Vector(population[0].values.size(), 0.0); for (int popIter = 0; popIter < population.Count; popIter++) { for (int jitterIter = 0; jitterIter < jitter.Count; jitterIter++) { jitter[jitterIter] = rng_.nextReal(); } population[popIter].values = bestMemberEver_.values + Vector.DirectMultiply( shuffledPop1[popIter].values - population[popIter].values , 0.0001 * jitter + configuration().stepsizeWeight); } mirrorPopulation = new InitializedList <Candidate>(population.Count); mirrorPopulation.ForEach((ii, vv) => mirrorPopulation[ii] = (Candidate)bestMemberEver_.Clone()); } break; case Strategy.CurrentToBest2Diffs: { population.Shuffle(); List <Candidate> shuffledPop1 = (List <Candidate>)population.Clone(); population.Shuffle(); for (int popIter = 0; popIter < population.Count; popIter++) { population[popIter].values = oldPopulation[popIter].values + configuration().stepsizeWeight *(bestMemberEver_.values - oldPopulation[popIter].values) + configuration().stepsizeWeight *(population[popIter].values - shuffledPop1[popIter].values); } mirrorPopulation = (List <Candidate>)shuffledPop1.Clone(); } break; case Strategy.Rand1DiffWithPerVectorDither: { population.Shuffle(); List <Candidate> shuffledPop1 = (List <Candidate>)population.Clone(); population.Shuffle(); List <Candidate> shuffledPop2 = (List <Candidate>)population.Clone(); population.Shuffle(); mirrorPopulation = (List <Candidate>)shuffledPop1.Clone(); Vector FWeight = new Vector(population.First().values.size(), 0.0); for (int fwIter = 0; fwIter < FWeight.Count; fwIter++) { FWeight[fwIter] = (1.0 - configuration().stepsizeWeight) * rng_.nextReal() + configuration().stepsizeWeight; } for (int popIter = 0; popIter < population.Count; popIter++) { population[popIter].values = population[popIter].values + Vector.DirectMultiply(FWeight, shuffledPop1[popIter].values - shuffledPop2[popIter].values); } } break; case Strategy.Rand1DiffWithDither: { population.Shuffle(); List <Candidate> shuffledPop1 = (List <Candidate>)population.Clone(); population.Shuffle(); List <Candidate> shuffledPop2 = (List <Candidate>)population.Clone(); population.Shuffle(); mirrorPopulation = (List <Candidate>)shuffledPop1.Clone(); double FWeight = (1.0 - configuration().stepsizeWeight) * rng_.nextReal() + configuration().stepsizeWeight; for (int popIter = 0; popIter < population.Count; popIter++) { population[popIter].values = population[popIter].values + FWeight * (shuffledPop1[popIter].values - shuffledPop2[popIter].values); } } break; case Strategy.EitherOrWithOptimalRecombination: { population.Shuffle(); List <Candidate> shuffledPop1 = (List <Candidate>)population.Clone(); population.Shuffle(); List <Candidate> shuffledPop2 = (List <Candidate>)population.Clone(); population.Shuffle(); mirrorPopulation = (List <Candidate>)shuffledPop1.Clone(); double probFWeight = 0.5; if (rng_.nextReal() < probFWeight) { for (int popIter = 0; popIter < population.Count; popIter++) { population[popIter].values = oldPopulation[popIter].values + configuration().stepsizeWeight *(shuffledPop1[popIter].values - shuffledPop2[popIter].values); } } else { double K = 0.5 * (configuration().stepsizeWeight + 1); // invariant with respect to probFWeight used for (int popIter = 0; popIter < population.Count; popIter++) { population[popIter].values = oldPopulation[popIter].values + K * (shuffledPop1[popIter].values - shuffledPop2[popIter].values - 2.0 * population[popIter].values); } } } break; case Strategy.Rand1SelfadaptiveWithRotation: { population.Shuffle(); List <Candidate> shuffledPop1 = (List <Candidate>)population.Clone(); population.Shuffle(); List <Candidate> shuffledPop2 = (List <Candidate>)population.Clone(); population.Shuffle(); mirrorPopulation = (List <Candidate>)shuffledPop1.Clone(); adaptSizeWeights(); for (int popIter = 0; popIter < population.Count; popIter++) { if (rng_.nextReal() < 0.1) { population[popIter].values = rotateArray(bestMemberEver_.values); } else { population[popIter].values = bestMemberEver_.values + currGenSizeWeights_[popIter] * (shuffledPop1[popIter].values - shuffledPop2[popIter].values); } } } break; default: Utils.QL_FAIL("Unknown strategy (" + Convert.ToInt32(configuration().strategy) + ")"); break; } // in order to avoid unnecessary copying we use the same population object for mutants crossover(oldPopulation, population, population, mirrorPopulation, costFunction); }
private Candidate InferTypeArguments(Candidate candidate, IDictionary <Type, Type> typeArgumentMap, SymbolTable symbols) { return(this.DispatchType != DispatchTypes.Method ? candidate : new Dictionary <Type, Type>(typeArgumentMap).Let(map => { if (map.Count == candidate.Method.GetGenericArguments().Length) { return candidate.Clone( member: candidate.Method != null && candidate.Method.IsGenericMethodDefinition ? candidate.Method.MakeGenericMethod(typeArgumentMap.ToArgumentArray()) : candidate.Member, arguments: candidate.ParameterMap .Select(_ => _.Item2 is AmbiguousLambdaExpression && _.Item1.GetDelegateSignature() != null ? ((AmbiguousLambdaExpression)_.Item2) .ApplyTypeArguments(_.Item1) .ApplyTypeArguments(map) .Reduce(symbols, _.Item1.ReplaceGenericArguments(map)) : _.Item2 ) .ToArray() ); } else { candidate.ParameterMap .Where(_ => !(_.Item1.IsGenericParameter ? EnumerableEx.Return(_.Item1) : _.Item1.GetGenericArguments() ).All(map.ContainsKey)) .ForEach(_ => { if (_.Item1.GetDelegateSignature() != null) { if (_.Item2 is AmbiguousLambdaExpression) { var method = _.Item1.GetDelegateSignature(); if (!method.ReturnType.Let(r => (r.IsGenericParameter ? new [] { r, } : r.GetGenericArguments()).Let(ts => ts.All(map.ContainsKey)) )) { method.GetParameters() .Select(p => p.ParameterType.Let(t => t.IsGenericParameter ? map.GetValue(t) : t )) .If(ts => ts.All(t => t != null), ts => map = new TypeNode(method.ReturnType).Match(map, ((AmbiguousLambdaExpression)_.Item2) .ApplyTypeArguments(ts) .Type(symbols) .GetDelegateSignature() .ReturnType ) ); } } else if (_.Item2 is LambdaExpression) { map = new TypeNode(_.Item1).Match(map, _.Item2.Type.GetDelegateSignature().GetDelegateType()); } } else if (_.Item1.ContainsGenericParameters) { (_.Item1.IsGenericParameter ? EnumerableEx.Return(Tuple.Create(_.Item1, _.Item2.Type)) : _.Item1.GetAppearingTypes() .Zip(_.Item2.Type.GetCorrespondingType(_.Item1).GetAppearingTypes(), Tuple.Create) .Where(t => t.Item1.IsGenericParameter) ).ForEach(t => map = new TypeNode(t.Item1).Match(map, t.Item2)); } }); return map.Keys.All(typeArgumentMap.ContainsKey) ? null : this.InferTypeArguments(candidate, map, symbols); } })); }