Print

Print


On Feb 25, 2014, at 1:43 AM, Francisco Lopez de la Franca <[log in to unmask]> wrote:

> But, how do I do that? I mean, where do I check (in the code) that the individual generated does not fulfill the requirement of appearing every node at least once?

Probably the easiest way to do this would be to make a BreedingPipeline subclass which generates kids, checks them for validity, and if they're invalid, generates new kids.  It tries this some N times, and if it's always failed, it gives up and uses the parents.  For example, you could subclass ReproductionPipeline like this:

I whipped up a class which might be useful to you here:

/*
  Copyright 2014 by Sean Luke
  Licensed under the Academic Free License version 3.0
  See the file "LICENSE" for more information
*/


package ec.breed;
import ec.*;
import ec.util.*;

/* 
 * CheckingPipeline.java
 * 
 * Created: Tue Feb 25 08:33:02 EST 2014
 * By: Sean Luke
 */

/**
 * CheckingPipeline is a BreedingPipeline which just passes through the
 * individuals it receives from its source 0, but only if those individuals
 * ALL pass a validation check (the method allValid(), which you must override).
 * It tries to find valid individuals some num-times times, and if it cannot, it
 * instead reproduces individuals from its source 1 and returns them instead.
 *
 * <p>In some cases you may wish instead to produce some N individuals which
 * are individually checked for validity, rather than together.  To enforce this,
 * assuming your sources don't naturally reutrn one child at a time, you
 * can add ForceBreedingPipelines as children and as parents to the
 * CheckingPipeline.
 *
 <p><b>Typical Number of Individuals Produced Per <tt>produce(...)</tt> call</b><br>
 ...as many as either child produces

 <p><b>Number of Sources</b><br>
 2

 <p><b>Parameters</b><br>
 <table>
 <tr><td valign=top><i>base.</i><tt>num-times</tt><br>
 <font size=-1>int >= 1</font></td>
 <td valign=top>(number of times we try to get children from source 0 before giving up and using source 1)</td></tr>

 </table>
 <p><b>Default Base</b><br>
 breed.check

 * @author Sean Luke
 * @version 1.0 
 */

public class CheckingPipeline extends BreedingPipeline
    {
    public static final String P_CHECK = "check";
    public static final String P_NUMTIMES = "num-times";
    public static final int NUM_SOURCES = 2;
    
    public Parameter defaultBase() { return BreedDefaults.base().push(P_CHECK); }

    public int numSources() { return NUM_SOURCES; }

	int numTimes = 0;

    public void setup(final EvolutionState state, final Parameter base)
        {
        super.setup(state,base);
        Parameter def = defaultBase();
        numTimes = state.parameters.getInt(base.push(P_NUMTIMES), def.push(P_NUMTIMES),1);
        if (numTimes < 1)
        	state.output.fatal("CheckingPipeline must have a num-times value >= 1.", 
        		base.push(P_NUMTIMES),
        		def.push(P_NUMTIMES));
        if (likelihood != 1.0)
            state.output.warning("CheckingPipeline given a likelihood other than 1.0.  This is nonsensical and will be ignored.",
                base.push(P_LIKELIHOOD),
                def.push(P_LIKELIHOOD));
        }
    
    public boolean allValid(Individual[] inds, int numInds, int subpopulation, EvolutionState state, int thread)
    	{
    	return true;
    	}
    	
    public int produce(
        final int min, 
        final int max, 
        final int start,
        final int subpopulation,
        final Individual[] inds,
        final EvolutionState state,
        final int thread) 
        {
        Individual[] inds2 = new Individual[max];
        
        for(int i = 0; i < numTimes; i++)
        	{
			// grab individuals from our source and stick 'em right into inds.
			// we'll modify them from there
			int n = sources[0].produce(min,max,0,subpopulation,inds2,state,thread);
			
			// check for validity
			if (!allValid(inds, n, subpopulation, state, thread))
				continue;
				
			// success!
			System.arraycopy(inds2, 0, inds, start, n);  // dump 'em in
			if (sources[0] instanceof SelectionMethod)
				for(int q=start; q < n+start; q++)
					inds[q] = (Individual)(inds[q].clone());
			return n;
			}
			
		// failure!  Grab from the other source
		int n = sources[1].produce(min,max,0,subpopulation,inds2,state,thread);
		if (sources[0] instanceof SelectionMethod)
			for(int q=start; q < n+start; q++)
				inds[q] = (Individual)(inds[q].clone());
		return n;
        }
    }