本文整理汇总了C#中IItem.Clone方法的典型用法代码示例。如果您正苦于以下问题:C# IItem.Clone方法的具体用法?C# IItem.Clone怎么用?C# IItem.Clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IItem
的用法示例。
在下文中一共展示了IItem.Clone方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Apply
public static ItemArray<IItem> Apply(IItem initiator, IItem guide, IntValue k, PercentValue n) {
if (!(initiator is RealVector) || !(guide is RealVector))
throw new ArgumentException("Cannot relink path because one of the provided solutions or both have the wrong type.");
if (n.Value <= 0.0)
throw new ArgumentException("RelinkingAccuracy must be greater than 0.");
RealVector v1 = initiator.Clone() as RealVector;
RealVector v2 = guide as RealVector;
if (v1.Length != v2.Length)
throw new ArgumentException("The solutions are of different length.");
IList<RealVector> solutions = new List<RealVector>();
for (int i = 0; i < k.Value; i++) {
RealVector solution = v1.Clone() as RealVector;
for (int j = 0; j < solution.Length; j++)
solution[j] = v1[j] + 1 / (k.Value - i) * (v2[j] - v1[j]);
solutions.Add(solution);
}
IList<IItem> selection = new List<IItem>();
if (solutions.Count > 0) {
int noSol = (int)(solutions.Count * n.Value);
if (noSol <= 0) noSol++;
double stepSize = (double)solutions.Count / (double)noSol;
for (int i = 0; i < noSol; i++)
selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5)));
}
return new ItemArray<IItem>(selection);
}
示例2: Apply
public static ItemArray<IItem> Apply(IItem initiator, IItem[] guides, DistanceMatrix distances, PercentValue n) {
if (!(initiator is Permutation) || guides.Any(x => !(x is Permutation)))
throw new ArgumentException("Cannot relink path because some of the provided solutions have the wrong type.");
if (n.Value <= 0.0)
throw new ArgumentException("RelinkingAccuracy must be greater than 0.");
Permutation v1 = initiator.Clone() as Permutation;
Permutation[] targets = new Permutation[guides.Length];
Array.Copy(guides, targets, guides.Length);
if (targets.Any(x => x.Length != v1.Length))
throw new ArgumentException("At least one solution is of different length.");
IList<Permutation> solutions = new List<Permutation>();
for (int i = 0; i < v1.Length; i++) {
int currCityIndex = i;
int bestCityIndex = (i + 1) % v1.Length;
double currDistance = distances[v1[currCityIndex], v1[bestCityIndex]];
// check each guiding solution
targets.ToList().ForEach(solution => {
// locate current city
var node = solution.Select((x, index) => new { Id = x, Index = index }).Single(x => x.Id == v1[currCityIndex]);
int pred = solution[(node.Index - 1 + solution.Length) % solution.Length];
int succ = solution[(node.Index + 1) % solution.Length];
// get distances to neighbors
var results = new[] { pred, succ }.Select(x => new { Id = x, Distance = distances[x, node.Id] });
var bestCity = results.Where(x => x.Distance < currDistance).OrderBy(x => x.Distance).FirstOrDefault();
if (bestCity != null) {
bestCityIndex = v1.Select((x, index) => new { Id = x, Index = index }).Single(x => x.Id == bestCity.Id).Index;
currDistance = bestCity.Distance;
}
});
Invert(v1, currCityIndex + 1, bestCityIndex);
solutions.Add(v1.Clone() as Permutation);
}
IList<IItem> selection = new List<IItem>();
if (solutions.Count > 0) {
int noSol = (int)(solutions.Count * n.Value);
if (noSol <= 0) noSol++;
double stepSize = (double)solutions.Count / (double)noSol;
for (int i = 0; i < noSol; i++)
selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5)));
}
return new ItemArray<IItem>(selection);
}
示例3: Apply
public static ItemArray<IItem> Apply(IItem initiator, IItem guide, PercentValue n) {
if (!(initiator is Permutation) || !(guide is Permutation))
throw new ArgumentException("Cannot relink path because one of the provided solutions or both have the wrong type.");
if (n.Value <= 0.0)
throw new ArgumentException("RelinkingAccuracy must be greater than 0.");
Permutation firstInitiator = initiator.Clone() as Permutation;
Permutation firstGuide = guide.Clone() as Permutation;
Permutation secondInitiator = firstGuide.Clone() as Permutation;
Permutation secondGuide = firstInitiator.Clone() as Permutation;
if (firstInitiator.Length != firstGuide.Length)
throw new ArgumentException("The solutions are of different length.");
IList<Permutation> solutions = new List<Permutation>();
for (int i = 0; i < firstInitiator.Length / 2; i++) {
if (firstInitiator[i] != firstGuide[i]) {
var target = firstInitiator.Select((x, index) => new { Value = x, ValueIndex = index }).First(x => x.Value == firstGuide[i]);
// XOR swap
firstInitiator[i] ^= firstInitiator[target.ValueIndex];
firstInitiator[target.ValueIndex] ^= firstInitiator[i];
firstInitiator[i] ^= firstInitiator[target.ValueIndex];
solutions.Add(firstInitiator.Clone() as Permutation);
}
int j = secondInitiator.Length - 1 - i;
if (secondInitiator[j] != secondGuide[j]) {
var target = secondInitiator.Select((x, index) => new { Value = x, ValueIndex = index }).First(x => x.Value == secondGuide[j]);
// XOR swap
secondInitiator[j] ^= secondInitiator[target.ValueIndex];
secondInitiator[target.ValueIndex] ^= secondInitiator[j];
secondInitiator[j] ^= secondInitiator[target.ValueIndex];
solutions.Add(secondInitiator.Clone() as Permutation);
}
}
IList<IItem> selection = new List<IItem>();
if (solutions.Count > 0) {
int noSol = (int)(solutions.Count * n.Value);
if (noSol <= 0) noSol++;
double stepSize = (double)solutions.Count / (double)noSol;
for (int i = 0; i < noSol; i++)
selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5)));
}
return new ItemArray<IItem>(selection);
}
示例4: Apply
public static ItemArray<IItem> Apply(IItem initiator, IItem guide, PercentValue n) {
if (!(initiator is BinaryVector) || !(guide is BinaryVector))
throw new ArgumentException("Cannot relink path because one of the provided solutions or both have the wrong type.");
if (n.Value <= 0.0)
throw new ArgumentException("RelinkingAccuracy must be greater than 0.");
BinaryVector firstInitiator = initiator.Clone() as BinaryVector;
BinaryVector firstGuide = guide.Clone() as BinaryVector;
BinaryVector secondInitiator = firstGuide.Clone() as BinaryVector;
BinaryVector secondGuide = firstInitiator.Clone() as BinaryVector;
if (firstInitiator.Length != firstGuide.Length)
throw new ArgumentException("The solutions are of different length.");
IList<BinaryVector> solutions = new List<BinaryVector>();
for (int i = 0; i < firstInitiator.Length / 2; i++) {
if (firstInitiator[i] != firstGuide[i]) {
firstInitiator[i] = firstGuide[i];
solutions.Add(firstInitiator.Clone() as BinaryVector);
}
int j = secondInitiator.Length - 1 - i;
if (secondInitiator[j] != secondGuide[j]) {
secondInitiator[j] = secondGuide[j];
solutions.Add(secondInitiator.Clone() as BinaryVector);
}
}
IList<IItem> selection = new List<IItem>();
if (solutions.Count > 0) {
int noSol = (int)(solutions.Count * n.Value);
if (noSol <= 0) noSol++;
double stepSize = (double)solutions.Count / (double)noSol;
for (int i = 0; i < noSol; i++)
selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5)));
}
return new ItemArray<IItem>(selection);
}