本文整理汇总了C#中ItemArray类的典型用法代码示例。如果您正苦于以下问题:C# ItemArray类的具体用法?C# ItemArray怎么用?C# ItemArray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ItemArray类属于命名空间,在下文中一共展示了ItemArray类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SinglePointCrossoverCrossTest
public void SinglePointCrossoverCrossTest() {
var target = new PrivateObject(typeof(SinglePointCrossover));
ItemArray<IntegerVector> parents;
TestRandom random = new TestRandom();
bool exceptionFired;
// The following test checks if there is an exception when there are more than 2 parents
random.Reset();
parents = new ItemArray<IntegerVector>(new IntegerVector[] { new IntegerVector(5), new IntegerVector(6), new IntegerVector(4) });
exceptionFired = false;
try {
IntegerVector actual;
actual = (IntegerVector)target.Invoke("Cross", random, parents);
}
catch (System.ArgumentException) {
exceptionFired = true;
}
Assert.IsTrue(exceptionFired);
// The following test checks if there is an exception when there are less than 2 parents
random.Reset();
parents = new ItemArray<IntegerVector>(new IntegerVector[] { new IntegerVector(4) });
exceptionFired = false;
try {
IntegerVector actual;
actual = (IntegerVector)target.Invoke("Cross", random, parents);
}
catch (System.ArgumentException) {
exceptionFired = true;
}
Assert.IsTrue(exceptionFired);
}
示例2: Apply
public static LinearLinkage Apply(IRandom random, ItemArray<LinearLinkage> parents) {
var len = parents[0].Length;
var child = new LinearLinkage(len);
var childGroup = new List<HashSet<int>>();
var currentParent = random.Next(parents.Length);
var groups = parents.Select(x => x.GetGroups().Select(y => new HashSet<int>(y)).ToList()).ToList();
bool remaining;
do {
var maxGroup = groups[currentParent].Select((v, i) => Tuple.Create(i, v))
.MaxItems(x => x.Item2.Count)
.SampleRandom(random).Item1;
var group = groups[currentParent][maxGroup];
groups[currentParent].RemoveAt(maxGroup);
childGroup.Add(group);
remaining = false;
for (var p = 0; p < groups.Count; p++) {
for (var j = 0; j < groups[p].Count; j++) {
foreach (var elem in group) groups[p][j].Remove(elem);
if (!remaining && groups[p][j].Count > 0) remaining = true;
}
}
currentParent = (currentParent + 1) % parents.Length;
} while (remaining);
child.SetGroups(childGroup);
return child;
}
示例3: DiscreteCrossoverApplyTest
public void DiscreteCrossoverApplyTest() {
TestRandom random = new TestRandom();
RealVector parent1, parent2, expected, actual;
ItemArray<RealVector> parents;
bool exceptionFired;
// The following test is not based on published examples
random.Reset();
random.IntNumbers = new int[] { 0, 0, 1, 0, 1 };
parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
parents = new ItemArray<RealVector>(new RealVector[] { parent1, parent2 });
expected = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.8 });
actual = DiscreteCrossover.Apply(random, parents);
Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
// The following test is not based on published examples
random.Reset();
random.IntNumbers = new int[] { 0, 0, 1, 0, 1, 0 };
parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1, 0.9 }); // this parent is longer
parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
parents = new ItemArray<RealVector>(new RealVector[] { parent1, parent2 });
exceptionFired = false;
try {
actual = DiscreteCrossover.Apply(random, parents);
}
catch (System.ArgumentException) {
exceptionFired = true;
}
Assert.IsTrue(exceptionFired);
}
示例4: Apply
public override IOperation Apply() {
ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize);
for (int i = 0; i < SwarmSize; i++) {
neighbors[i] = new IntArray(new[] { (SwarmSize + i - 1) % SwarmSize, (i + 1) % SwarmSize });
}
Neighbors = neighbors;
return base.Apply();
}
示例5: GetItems
public IList<Item> GetItems(Critter critter)
{
var container = GetContainer (critter, false);
if (container == null)
return new List<Item> (0);
var itemArray = new ItemArray ();
container.GetItems (0, itemArray);
return new List<Item> (itemArray);
}
示例6: Average
private DoubleArray Average(IRandom random, ItemArray<DoubleArray> parents) {
int length = parents[0].Length;
var result = new DoubleArray(length);
for (int i = 0; i < length; i++) {
for (int p = 0; p < parents.Length; p++) {
result[i] += parents[p][i];
}
result[i] /= parents.Length;
}
return result;
}
示例7: PutItems
public void PutItems(Critter critter, IList<Item> items)
{
var container = GetContainer (critter, true);
if (container == null)
return;
var itemArray = new ItemArray ();
itemArray.AddRange (items);
Global.MoveItems (itemArray, container, 0);
}
示例8: Apply
/// <summary>
/// Performs a discrete crossover operation on multiple parents.
/// </summary>
/// <exception cref="ArgumentException">Thrown when the vectors of the parents are of different length.</exception>
/// <param name="random">A random number generator.</param>
/// <param name="parents">An array containing the parents that should be crossed.</param>
/// <returns>The newly created real vector, resulting from the crossover operation.</returns>
public static RealVector Apply(IRandom random, ItemArray<RealVector> parents) {
int length = parents[0].Length;
for (int i = 0; i < parents.Length; i++) {
if (parents[i].Length != length)
throw new ArgumentException("DiscreteCrossover: The parents' vectors are of different length.", "parents");
}
RealVector result = new RealVector(length);
for (int i = 0; i < length; i++) {
result[i] = parents[random.Next(parents.Length)][i];
}
return result;
}
示例9: Apply
/// <summary>
/// Performs a discrete crossover operation of any number of given parents.
/// </summary>
/// <exception cref="ArgumentException">Thrown when the vectors of the parents are of different length or when there are less than 2 parents.</exception>
/// <param name="random">A random number generator.</param>
/// <param name="parents">The list of parents for the crossover operation.</param>
/// <returns>The newly created integer vector, resulting from the crossover operation.</returns>
public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents) {
if (parents.Length < 2) throw new ArgumentException("DiscreteCrossover: There are less than two parents to cross.");
int length = parents[0].Length;
for (int i = 0; i < parents.Length; i++) {
if (parents[i].Length != length)
throw new ArgumentException("DiscreteCrossover: The parents' vectors are of different length.", "parents");
}
var result = new IntegerVector(length);
for (int i = 0; i < length; i++) {
result[i] = parents[random.Next(parents.Length)][i];
}
return result;
}
示例10: Apply
public override IOperation Apply() {
ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize);
for (int i = 0; i < SwarmSize; i++) {
var numbers = Enumerable.Range(0, SwarmSize).ToList();
numbers.RemoveAt(i);
var selectedNumbers = new List<int>(NrOfConnections);
for (int j = 0; j < NrOfConnections && numbers.Count > 0; j++) {
int index = Random.Next(numbers.Count);
selectedNumbers.Add(numbers[index]);
numbers.RemoveAt(index);
}
neighbors[i] = new IntArray(selectedNumbers.ToArray());
}
Neighbors = neighbors;
return base.Apply();
}
示例11: InstrumentedApply
public override IOperation InstrumentedApply() {
ItemArray<IVRPEncoding> parents = new ItemArray<IVRPEncoding>(ParentsParameter.ActualValue.Length);
for (int i = 0; i < ParentsParameter.ActualValue.Length; i++) {
IVRPEncoding solution = ParentsParameter.ActualValue[i];
if (!(solution is PrinsEncoding)) {
parents[i] = PrinsEncoding.ConvertFrom(solution, ProblemInstance);
} else {
parents[i] = solution;
}
}
ParentsParameter.ActualValue = parents;
ChildParameter.ActualValue =
Crossover(RandomParameter.ActualValue, parents[0] as PrinsEncoding, parents[1] as PrinsEncoding);
return base.InstrumentedApply();
}
示例12: Apply
public static LinearLinkage Apply(IRandom random, ItemArray<LinearLinkage> parents) {
var len = parents[0].Length;
var child = new LinearLinkage(len);
var remaining = new SortedSet<int>(Enumerable.Range(0, len));
do {
var groups = parents.Select(x => x.GetGroupForward(remaining.Min).Where(y => remaining.Contains(y)).ToList()).ToList();
var max = groups.Select((v, idx) => Tuple.Create(idx, v.Count)).MaxItems(x => x.Item2).SampleRandom(random).Item1;
var i = groups[max][0];
for (var k = 1; k < groups[max].Count; k++) {
child[i] = groups[max][k];
remaining.Remove(i);
i = child[i];
}
child[i] = i;
remaining.Remove(i);
} while (remaining.Count > 0);
return child;
}
示例13: Apply
/// <summary>
/// Performs the average crossover (intermediate recombination) on a list of parents.
/// </summary>
/// <exception cref="ArgumentException">Thrown when there is just one parent or when the parent vectors are of different length.</exception>
/// <remarks>
/// There can be more than two parents.
/// </remarks>
/// <param name="random">The random number generator.</param>
/// <param name="parents">The list of parents.</param>
/// <returns>The child vector (average) of the parents.</returns>
public static RealVector Apply(IRandom random, ItemArray<RealVector> parents) {
int length = parents[0].Length, parentsCount = parents.Length;
if (parents.Length < 2) throw new ArgumentException("AverageCrossover: The number of parents is less than 2.", "parents");
RealVector result = new RealVector(length);
try {
double avg;
for (int i = 0; i < length; i++) {
avg = 0;
for (int j = 0; j < parentsCount; j++)
avg += parents[j][i];
result[i] = avg / (double)parentsCount;
}
}
catch (IndexOutOfRangeException) {
throw new ArgumentException("AverageCrossover: The parents' vectors are of different length.", "parents");
}
return result;
}
示例14: Apply
public static LinearLinkage Apply(IRandom random, ItemArray<LinearLinkage> parents) {
var len = parents[0].Length;
var p = random.Next(parents.Length);
var child = new LinearLinkage(len);
var remaining = new SortedSet<int>(Enumerable.Range(0, len));
do {
var i = remaining.Min;
foreach (var g in parents[p].GetGroupForward(i)) {
if (!remaining.Contains(g)) continue;
child[i] = g;
i = g;
remaining.Remove(g);
}
child[i] = i;
remaining.Remove(i);
p = (p + 1) % parents.Length;
} while (remaining.Count > 0);
return child;
}
示例15: ShowMatrixOnConsole
public static string ShowMatrixOnConsole(ItemArray matrix)
{
string x = string.Empty;
for (int row = Globals.Rows - 1; row >= 0; row--)
{
for (int column = 0; column < Globals.Columns; column++)
{
if (matrix[row, column] != null)
{
x += matrix[row, column].Value + "|";
}
else
{
x += "X" + "|";
}
}
x += Environment.NewLine;
}
Debug.Log(x);
return x;
}