本文整理汇总了C#中HeuristicLab.Encodings.PermutationEncoding.Permutation类的典型用法代码示例。如果您正苦于以下问题:C# Permutation类的具体用法?C# Permutation怎么用?C# Permutation使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Permutation类属于HeuristicLab.Encodings.PermutationEncoding命名空间,在下文中一共展示了Permutation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SolveTest
public void SolveTest() {
double[,] costs = new double[,] {
{ 5, 9, 3, 6 },
{ 8, 7, 8, 2 },
{ 6, 10, 12, 7 },
{ 3, 10, 8, 6 }};
double quality;
Permutation p;
p = new Permutation(PermutationTypes.Absolute, LinearAssignmentProblemSolver.Solve(new DoubleMatrix(costs), out quality));
Assert.AreEqual(18, quality);
Assert.IsTrue(p.Validate());
costs = new double[,] {
{ 11, 7, 10, 17, 10 },
{ 13, 21, 7, 11, 13 },
{ 13, 13, 15, 13, 14 },
{ 18, 10, 13, 16, 14 },
{ 12, 8, 16, 19, 10 }};
p = new Permutation(PermutationTypes.Absolute, LinearAssignmentProblemSolver.Solve(new DoubleMatrix(costs), out quality));
Assert.AreEqual(51, quality);
Assert.IsTrue(p.Validate());
costs = new double[,] {
{ 3, 1, 1, 4 },
{ 4, 2, 2, 5 },
{ 5, 3, 4, 8 },
{ 4, 2, 5, 9 }};
p = new Permutation(PermutationTypes.Absolute, LinearAssignmentProblemSolver.Solve(new DoubleMatrix(costs), out quality));
Assert.AreEqual(13, quality);
Assert.IsTrue(p.Validate());
}
示例2: Apply
/// <summary>
/// Performs a slight variation of the order crossover of two permutations.
/// </summary>
/// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
/// <remarks>
/// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving
/// the positions. Then, from the beginning of the permutation, copies the missing values from the second permutation
/// in the order they occur.
/// </remarks>
/// <param name="random">A random number generator.</param>
/// <param name="parent1">The first parent permutation to cross.</param>
/// <param name="parent2">The second parent permutation to cross.</param>
/// <returns>The new permutation resulting from the crossover.</returns>
public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
if (parent1.Length != parent2.Length) throw new ArgumentException("OrderCrossover2: The parent permutations are of unequal length.");
int[] result = new int[parent1.Length];
bool[] copied = new bool[result.Length];
int breakPoint1 = random.Next(result.Length - 1);
int breakPoint2 = random.Next(breakPoint1 + 1, result.Length);
for (int i = breakPoint1; i <= breakPoint2; i++) { // copy part of first permutation
result[i] = parent1[i];
copied[parent1[i]] = true;
}
int index = 0;
for (int i = 0; i < parent2.Length; i++) { // copy remaining part of second permutation
if (index == breakPoint1) { // skip already copied part
index = breakPoint2 + 1;
}
if (!copied[parent2[i]]) {
result[index] = parent2[i];
index++;
}
}
return new Permutation(parent1.PermutationType, result);
}
示例3: Apply
/// <summary>
/// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
/// based on randomly chosen positions to define which position to take from where.
/// </summary>
/// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
/// <param name="random">The random number generator.</param>
/// <param name="parent1">First parent</param>
/// <param name="parent2">Second Parent</param>
/// <returns>Child</returns>
public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
if (parent1.Length != parent2.Length) throw new ArgumentException("PositionBasedCrossover: The parent permutations are of unequal length.");
int length = parent1.Length;
int[] result = new int[length];
bool[] randomPosition = new bool[length];
bool[] numberCopied = new bool[length];
int randomPosNumber = random.Next(length);
for (int i = 0; i < randomPosNumber; i++) { // generate random bit mask
randomPosition[random.Next(length)] = true;
}
for (int i = 0; i < length; i++) { // copy numbers masked as true from second permutation
if (randomPosition[i]) {
result[i] = parent2[i];
numberCopied[parent2[i]] = true;
}
}
int index = 0;
for (int i = 0; i < length; i++) { // copy numbers masked as false from first permutation
if (!numberCopied[parent1[i]]) {
if (randomPosition[index]) {
while (randomPosition[index]) {
index++;
}
}
result[index] = parent1[i];
index++;
}
}
return new Permutation(parent1.PermutationType, result);
}
示例4: Apply
public static void Apply(Permutation permutation, int breakPoint1, int breakPoint2) {
for (int i = 0; i <= (breakPoint2 - breakPoint1) / 2; i++) { // invert permutation between breakpoints
int temp = permutation[breakPoint1 + i];
permutation[breakPoint1 + i] = permutation[breakPoint2 - i];
permutation[breakPoint2 - i] = temp;
}
}
示例5: EvaluateByCoordinates
public static double EvaluateByCoordinates(Permutation permutation, TranslocationMove move, DoubleMatrix coordinates, TSPTranslocationMovePathEvaluator evaluator) {
if (move.Index1 == move.Index3
|| move.Index2 == permutation.Length - 1 && move.Index3 == 0
|| move.Index1 == 0 && move.Index3 == permutation.Length - 1 - move.Index2) return 0;
int edge1source = permutation.GetCircular(move.Index1 - 1);
int edge1target = permutation[move.Index1];
int edge2source = permutation[move.Index2];
int edge2target = permutation.GetCircular(move.Index2 + 1);
int edge3source, edge3target;
if (move.Index3 > move.Index1) {
edge3source = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1);
edge3target = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1 + 1);
} else {
edge3source = permutation.GetCircular(move.Index3 - 1);
edge3target = permutation[move.Index3];
}
double moveQuality = 0;
// remove three edges
moveQuality -= evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
coordinates[edge1target, 0], coordinates[edge1target, 1]);
moveQuality -= evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
coordinates[edge2target, 0], coordinates[edge2target, 1]);
moveQuality -= evaluator.CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
coordinates[edge3target, 0], coordinates[edge3target, 1]);
// add three edges
moveQuality += evaluator.CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
coordinates[edge1target, 0], coordinates[edge1target, 1]);
moveQuality += evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
coordinates[edge3target, 0], coordinates[edge3target, 1]);
moveQuality += evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
coordinates[edge2target, 0], coordinates[edge2target, 1]);
return moveQuality;
}
示例6: Apply
/// <summary>
/// Performs the order crossover of two permutations.
/// </summary>
/// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
/// <exception cref="InvalidOperationException">Thrown if the numbers in the permutation elements are not in the range [0,N) with N = length of the permutation.</exception>
/// <remarks>
/// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving
/// the positions. Then, starting from the end of the copied interval, copies the missing values from the second permutation
/// in the order they occur.
/// </remarks>
/// <param name="random">A random number generator.</param>
/// <param name="parent1">The first parent permutation to cross.</param>
/// <param name="parent2">The second parent permutation to cross.</param>
/// <returns>The new permutation resulting from the crossover.</returns>
public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
if (parent1.Length != parent2.Length) throw new ArgumentException("OrderCrossover: The parent permutations are of unequal length.");
int length = parent1.Length;
int[] result = new int[length];
bool[] copied = new bool[length];
int breakPoint1 = random.Next(length - 1);
int breakPoint2 = random.Next(breakPoint1 + 1, length);
try {
for (int j = breakPoint1; j <= breakPoint2; j++) { // copy part of first permutation
result[j] = parent1[j];
copied[parent1[j]] = true;
}
int index = ((breakPoint2 + 1 >= length) ? (0) : (breakPoint2 + 1));
int i = index; // for moving in parent2
while (index != breakPoint1) {
if (!copied[parent2[i]]) {
result[index] = parent2[i];
index++;
if (index >= length) index = 0;
}
i++;
if (i >= length) i = 0;
}
}
catch (IndexOutOfRangeException) {
throw new InvalidOperationException("OrderCrossover: The permutation must consist of numbers in the interval [0;N) with N = length of the permutation.");
}
return new Permutation(parent1.PermutationType, result);
}
示例7: Apply
public static double Apply(Permutation assignment, TranslocationMove move, DoubleMatrix weights, DoubleMatrix distances) {
double moveQuality = 0;
int min = Math.Min(move.Index1, move.Index3);
int max = Math.Max(move.Index2, move.Index3 + (move.Index2 - move.Index1));
int iOffset, changeOffset;
if (move.Index1 < move.Index3) {
iOffset = move.Index2 - move.Index1 + 1;
changeOffset = min + max - move.Index2;
} else {
iOffset = move.Index1 - move.Index3;
changeOffset = min + move.Index2 - move.Index1 + 1;
}
for (int i = min; i <= max; i++) {
if (i == changeOffset) iOffset -= (max - min + 1);
int jOffset = ((move.Index1 < move.Index3) ? (move.Index2 - move.Index1 + 1) : (move.Index1 - move.Index3));
for (int j = 0; j < assignment.Length; j++) {
moveQuality -= weights[i, j] * distances[assignment[i], assignment[j]];
if (j < min || j > max) {
moveQuality -= weights[j, i] * distances[assignment[j], assignment[i]];
moveQuality += weights[i, j] * distances[assignment[i + iOffset], assignment[j]];
moveQuality += weights[j, i] * distances[assignment[j], assignment[i + iOffset]];
} else {
if (j == changeOffset) jOffset -= (max - min + 1);
moveQuality += weights[i, j] * distances[assignment[i + iOffset], assignment[j + jOffset]];
}
}
}
return moveQuality;
}
示例8: Generate
public static IEnumerable<TwoPointFiveMove> Generate(Permutation permutation) {
int length = permutation.Length;
if (length == 1) throw new ArgumentException("Exhaustive 2.5-MoveGenerator: There cannot be a 2.5 move given a permutation of length 1.", "permutation");
int totalMoves = (length) * (length - 1) / 2;
if (permutation.PermutationType == PermutationTypes.RelativeUndirected) {
if (totalMoves - 3 > 0) {
for (int i = 0; i < length - 1; i++) {
for (int j = i + 1; j < length; j++) {
// doesn't make sense to inverse the whole permutation or the whole but one in case of relative undirected permutations
if (j - i >= length - 2) continue;
yield return new TwoPointFiveMove(i, j, true);
yield return new TwoPointFiveMove(i, j, false);
}
}
} else { // when length is 3 or less, there's actually no difference, but for the sake of not crashing the algorithm create a dummy move
yield return new TwoPointFiveMove(0, 1, true);
}
} else {
for (int i = 0; i < length - 1; i++)
for (int j = i + 1; j < length; j++) {
yield return new TwoPointFiveMove(i, j, true);
yield return new TwoPointFiveMove(i, j, false);
}
}
}
示例9: GetSubSequenceAtPosition
private static int[] GetSubSequenceAtPosition(Permutation p1, int index, int subSequenceLength) {
var result = new int[subSequenceLength];
for (int i = 0; i < subSequenceLength; i++)
result[i] = p1[i + index];
return result;
}
示例10: ExchangeSubsequences
private static void ExchangeSubsequences(Permutation p1, int index1, Permutation p2, int index2, int subSequenceLength) {
var aux = (Permutation)p1.Clone();
for (int i = 0; i < subSequenceLength; i++) {
p1[i + index1] = p2[i + index2];
p2[i + index2] = aux[i + index1];
}
}
示例11: Apply
public static void Apply(Permutation permutation, TwoPointFiveMove move) {
if (move.IsInvert) {
InversionManipulator.Apply(permutation, move.Index1, move.Index2);
} else {
TranslocationManipulator.Apply(permutation, move.Index1, move.Index1, move.Index2);
}
}
示例12: MyClassInitialize
public static void MyClassInitialize(TestContext testContext) {
random = new MersenneTwister();
symmetricDistances = new DoubleMatrix(ProblemSize, ProblemSize);
symmetricWeights = new DoubleMatrix(ProblemSize, ProblemSize);
asymmetricDistances = new DoubleMatrix(ProblemSize, ProblemSize);
asymmetricWeights = new DoubleMatrix(ProblemSize, ProblemSize);
nonZeroDiagonalDistances = new DoubleMatrix(ProblemSize, ProblemSize);
nonZeroDiagonalWeights = new DoubleMatrix(ProblemSize, ProblemSize);
for (int i = 0; i < ProblemSize - 1; i++) {
for (int j = i + 1; j < ProblemSize; j++) {
symmetricDistances[i, j] = random.Next(ProblemSize * 100);
symmetricDistances[j, i] = symmetricDistances[i, j];
symmetricWeights[i, j] = random.Next(ProblemSize * 100);
symmetricWeights[j, i] = symmetricWeights[i, j];
asymmetricDistances[i, j] = random.Next(ProblemSize * 100);
asymmetricDistances[j, i] = random.Next(ProblemSize * 100);
asymmetricWeights[i, j] = random.Next(ProblemSize * 100);
asymmetricWeights[j, i] = random.Next(ProblemSize * 100);
nonZeroDiagonalDistances[i, j] = random.Next(ProblemSize * 100);
nonZeroDiagonalDistances[j, i] = random.Next(ProblemSize * 100);
nonZeroDiagonalWeights[i, j] = random.Next(ProblemSize * 100);
nonZeroDiagonalWeights[j, i] = random.Next(ProblemSize * 100);
}
nonZeroDiagonalDistances[i, i] = random.Next(ProblemSize * 100);
nonZeroDiagonalWeights[i, i] = random.Next(ProblemSize * 100);
}
int index = random.Next(ProblemSize);
if (nonZeroDiagonalDistances[index, index] == 0)
nonZeroDiagonalDistances[index, index] = random.Next(1, ProblemSize * 100);
index = random.Next(ProblemSize);
if (nonZeroDiagonalWeights[index, index] == 0)
nonZeroDiagonalWeights[index, index] = random.Next(1, ProblemSize * 100);
assignment = new Permutation(PermutationTypes.Absolute, ProblemSize, random);
}
示例13: Apply
/// <summary>
/// Moves a randomly chosen interval of elements to another (randomly chosen) position in the given
/// <paramref name="permutation"/> array and reverses it.
/// </summary>
/// <param name="random">The random number generator.</param>
/// <param name="permutation">The permutation array to manipulate.</param>
public static void Apply(IRandom random, Permutation permutation) {
Permutation original = (Permutation)permutation.Clone();
int breakPoint1, breakPoint2, insertPoint, insertPointLimit;
breakPoint1 = random.Next(original.Length - 1);
breakPoint2 = random.Next(breakPoint1 + 1, original.Length);
insertPointLimit = original.Length - breakPoint2 + breakPoint1 - 1; // get insertion point in remaining part
if (insertPointLimit > 0)
insertPoint = random.Next(insertPointLimit);
else
insertPoint = 0;
int i = 0; // index in new permutation
int j = 0; // index in old permutation
while (i < original.Length) {
if (i == insertPoint) { // copy translocated area
for (int k = breakPoint2; k >= breakPoint1; k--) {
permutation[i] = original[k];
i++;
}
}
if (j == breakPoint1) { // skip area between breakpoints
j = breakPoint2 + 1;
}
if ((i < original.Length) && (j < original.Length)) {
permutation[i] = original[j];
i++;
j++;
}
}
}
示例14: Apply
/// <summary>
/// Mixes the elements of the given <paramref name="permutation"/> randomly
/// in a randomly chosen interval.
/// </summary>
/// <param name="random">The random number generator.</param>
/// <param name="permutation">The permutation to manipulate.</param>
public static void Apply(IRandom random, Permutation permutation) {
int breakPoint1, breakPoint2;
int[] scrambledIndices, remainingIndices, temp;
int selectedIndex, index;
breakPoint1 = random.Next(permutation.Length - 1);
breakPoint2 = random.Next(breakPoint1 + 1, permutation.Length);
scrambledIndices = new int[breakPoint2 - breakPoint1 + 1];
remainingIndices = new int[breakPoint2 - breakPoint1 + 1];
for (int i = 0; i < remainingIndices.Length; i++) { // initialise indices
remainingIndices[i] = i;
}
for (int i = 0; i < scrambledIndices.Length; i++) { // generate permutation of indices
selectedIndex = random.Next(remainingIndices.Length);
scrambledIndices[i] = remainingIndices[selectedIndex];
temp = remainingIndices;
remainingIndices = new int[temp.Length - 1];
index = 0;
for (int j = 0; j < remainingIndices.Length; j++) {
if (index == selectedIndex) {
index++;
}
remainingIndices[j] = temp[index];
index++;
}
}
Apply(permutation, breakPoint1, scrambledIndices);
}
示例15: Apply
/// <summary>
/// Performs the partially matched crossover on <paramref name="parent1"/> and <paramref name="parent2"/>.
/// </summary>
/// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length or when the permutations are shorter than 4 elements.</exception>
/// <exception cref="InvalidOperationException">Thrown if the numbers in the permutation elements are not in the range [0,N) with N = length of the permutation.</exception>
/// <remarks>
/// Initially the new offspring is a clone of <paramref name="parent2"/>.
/// Then a segment is extracted from <paramref name="parent1"/> and copied to the offspring position by position.
/// Whenever a position is copied, the number at that position currently in the offspring is transfered to the position where the copied number has been.
/// E.g.: Position 15 is selected to be copied from <paramref name="parent1"/> to <paramref name="parent2"/>. At position 15 there is a '3' in <paramref name="parent1"/> and a '5' in the new offspring.
/// The '5' in the offspring is then moved to replace the '3' in the offspring and '3' is written at position 15.
/// </remarks>
/// <param name="random">A random number generator.</param>
/// <param name="parent1">The first parent permutation to cross.</param>
/// <param name="parent2">The second parent permutation to cross.</param>
/// <returns>The new permutation resulting from the crossover.</returns>
public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
if (parent1.Length != parent2.Length) throw new ArgumentException("PartiallyMatchedCrossover: The parent permutations are of unequal length.");
if (parent1.Length < 4) throw new ArgumentException("PartiallyMatchedCrossover: The parent permutation must be at least of size 4.");
int length = parent1.Length;
int[] result = new int[length];
int[] invResult = new int[length];
int breakPoint1, breakPoint2;
do {
breakPoint1 = random.Next(length - 1);
breakPoint2 = random.Next(breakPoint1 + 1, length);
} while (breakPoint2 - breakPoint1 >= length - 2); // prevent the case [0,length-1) -> clone of parent1
// clone parent2 and calculate inverse permutation (number -> index)
try {
for (int j = 0; j < length; j++) {
result[j] = parent2[j];
invResult[result[j]] = j;
}
}
catch (IndexOutOfRangeException) {
throw new InvalidOperationException("PartiallyMatchedCrossover: The permutation must consist of consecutive numbers from 0 to N-1 with N = length of the permutation.");
}
for (int j = breakPoint1; j <= breakPoint2; j++) {
int orig = result[j]; // save the former value
result[j] = parent1[j]; // overwrite the former value with the new value
int index = invResult[result[j]]; // look where the new value is in the offspring
result[index] = orig; // write the former value to this position
invResult[orig] = index; // update the inverse mapping
// it's not necessary to do 'invResult[result[j]] = j' as this will not be needed again
}
return new Permutation(parent1.PermutationType, result);
}