Genetic Programming Engine FAQ

Verifying the Base Class

The BaseClassVerifier ensures that the type passed to its constructor is a compatible IIndividual Base Class for use by the GPEngine.  It is ensured that the type is for a class; that it is not abstract; that it is not sealed; that it inherits from the IIndividual interface; and that it has a default, parameterless constructor.  It also ensures that there is at least one available method.

public BaseClassVerifier( Type baseClassType ) {

    //check for null arguments

    if( baseClassType == null ) {

        throw new ArgumentNullException( "baseClassType", "The type passed to

          BaseClassVerifier.constructor was null." );

    }

    //check that this is an instantiable class of type IIndividual with a default

      constructor

    if( !baseClassType.IsClass ) {

        throw new ArgumentException( "The type passed to BaseClassVerifier.constructor

          was not a class.", "baseClassType" );

    }

    if( baseClassType.IsAbstract ) {

        throw new ArgumentException( "The type passed to BaseClassVerifier.constructor

          was abstract.", "baseClassType" );

    }

    if( baseClassType.IsSealed ) {

        throw new ArgumentException( "The type passed to BaseClassVerifier.constructor

          was sealed.", "baseClassType" );

    }

    if( baseClassType.GetInterface( typeof(IIndividual).FullName ) == null ) {

        throw new ArgumentException( "The type passed to BaseClassVerifier.constructor

          was not of type IIndividual.", "baseClassType" );

    }

    MethodInfo testMethod = baseClassType.GetMethod( "Test", Type.EmptyTypes );

    if( !testMethod.IsVirtual || testMethod.IsFinal ) {

        throw new ArgumentException( "The type passed to BaseClassVerifier.constructor

          does not define the Test method as virtual.", "baseClassType" );

    }

    if( baseClassType.GetConstructor( Type.EmptyTypes ) == null ) {

        throw new ArgumentException( "The type passed to BaseClassVerifier.constructor

          does not have a parameterless constructor.", "baseClassType" );

    }

    //get type's methods

    MethodInfoCollection extraMethods =

        new MethodInfoCollection( baseClassType.GetMethods( BindingFlags.Public |

          BindingFlags.Instance ) );

    //remove methods inherited from object in-line

    for( int index = 0; index < extraMethods.Count; ++index ) {

        if( extraMethods[index].DeclaringType == typeof(object) ) {

            extraMethods.RemoveAt( index );

            --index;

        }

    }

    //remove interface methods

    MethodInfo[] removeMethods =

      baseClassType.GetInterfaceMap( typeof(IIndividual) ).TargetMethods;

    foreach( MethodInfo interfaceMethod in removeMethods ) {

        if( extraMethods.Contains( interfaceMethod ) ) extraMethods.Remove(

          interfaceMethod );

    }

        //check for at least one available method

    if( extraMethods.Empty ) {

        throw new ArgumentException( "The type passed to

          BaseClassVerifier.constructor does not define any extra methods.",

          "baseClassType" );

    }

    //everything checks out

    Instance = (IIndividual) Activator.CreateInstanceFrom(

      baseClassType.Assembly.CodeBase, baseClassType.FullName ).Unwrap();

    m_AvailableMethods = new ReadOnlyMethodInfoCollection( extraMethods );

    m_BaseClassType = baseClassType;

}

The BaseClassType method gets the type of the Base Class.

public Type BaseClassType {

    get {

        return m_BaseClassType;

    }

}

private readonly Type m_BaseClassType;

The Instance method gets the instantiated copy of the verified IIndividual.

private IIndividual Instance {

    get {

        return m_Instance;

    }

    set {

        m_Instance = value;

    }

}

private IIndividual m_Instance;

The AvailableMethods method gets the public methods not declared in the IIndividual interface.  These methods will be used to build new IIndividuals.

public ReadOnlyMethodInfoCollection AvailableMethods {

    get {

        return m_AvailableMethods;

    }

}

private ReadOnlyMethodInfoCollection m_AvailableMethods;

The CheckEnvironment method checks that the given IEnvironment can be used by IIndividuals .

public bool CheckEnvironment( IEnvironment environment ) {

    try {

        Instance.Environment = environment;

    }

    catch {

        return false;

    }

    return true;

}

 

See Also

IIndividual | GPEngine | IEnvironment