#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Linq;
using System.Collections.Generic;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
using HeuristicLab.Random;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
[TestClass]
public class ArgumentCreaterTest {
private const int POPULATION_SIZE = 1000;
private const int MAX_TREE_SIZE = 100;
private const int MAX_TREE_HEIGHT = 10;
private TestContext testContextInstance;
///
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///
public TestContext TestContext {
get {
return testContextInstance;
}
set {
testContextInstance = value;
}
}
[TestMethod()]
public void ArgumentCreaterDistributionsTest() {
var trees = new List();
var grammar = Grammars.CreateArithmeticAndAdfGrammar();
var random = new MersenneTwister(31415);
int failedOps = 0;
for (int i = 0; i < POPULATION_SIZE; i++) {
SymbolicExpressionTree tree;
do {
tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
} while (!TreeHasAdfWithParameter(tree, 3));
var success = ArgumentCreater.CreateNewArgument(random, tree, grammar, 60000, 100, 3, 3);
if (!success) failedOps++;
Util.IsValid(tree);
trees.Add(tree);
}
// difficult to make sure that create argument operations succeed because trees are macro-expanded can potentially become very big
// => just test if only a small proportion fails
Assert.IsTrue(failedOps < POPULATION_SIZE * 0.01 ); // only 1% may fail
Console.WriteLine("ArgumentCreator: " + Environment.NewLine +
"Failed operations: " + failedOps * 100.0 / POPULATION_SIZE + " %" + Environment.NewLine +
Util.GetSizeDistributionString(trees, 200, 20) + Environment.NewLine +
Util.GetFunctionDistributionString(trees) + Environment.NewLine +
Util.GetNumberOfSubTreesDistributionString(trees) + Environment.NewLine +
Util.GetTerminalDistributionString(trees) + Environment.NewLine
);
}
private bool TreeHasAdfWithParameter(SymbolicExpressionTree tree, int maxParameters) {
return tree.Root.SubTrees.Count == 2 &&
tree.Root.SubTrees[1].GetAllowedSymbols(0).Where(x => x is Argument).Count() < maxParameters;
}
}
}