All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gov.sandia.cognition.framework.learning.EvaluatorBasedCognitiveModuleFactoryLearner Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * File:                CognitiveModuleFactoryEvaluatorLearner.java
 * Authors:             Justin Basilico and Kevin R. Dixon
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 *
 * Copyright June 21, 2007, Sandia Corporation.  Under the terms of Contract
 * DE-AC04-94AL85000, there is a non-exclusive license for use of this work by
 * or on behalf of the U.S. Government. Export of this program may require a
 * license from the United States Government. See CopyrightHistory.txt for
 * complete details.
 *
 */

package gov.sandia.cognition.framework.learning;

import gov.sandia.cognition.framework.learning.converter.CogxelConverter;
import gov.sandia.cognition.framework.CognitiveModel;
import gov.sandia.cognition.framework.CognitiveModelInput;
import gov.sandia.cognition.framework.CognitiveModelState;
import gov.sandia.cognition.learning.algorithm.BatchLearner;
import gov.sandia.cognition.evaluator.Evaluator;
import gov.sandia.cognition.util.AbstractCloneableSerializable;
import gov.sandia.cognition.util.ObjectUtil;
import java.util.Collection;
import java.util.LinkedList;

/**
 * The EvaluatorBasedCognitiveModuleFactoryLearner class implements a
 * CognitiveModuleFactoryLearner for the EvaluatorBasedCognitiveModuleFactory.
 * It can be used to adapt learning algorithms that learn Evaluator objects
 * to the Cognitive Framework.
 *
 * @param  Input type of the embedded Evaluator.
 * @param  Output type of the embedded Evaluator.
 * @param  Data type used to create the training set,
 *  for example "Vector" or "InputOutputPair", etc.
 * @author Justin Basilico
 * @author Kevin R. Dixon
 * @since  2.0
 */
public class EvaluatorBasedCognitiveModuleFactoryLearner
    extends AbstractCloneableSerializable
    implements CognitiveModuleFactoryLearner
{
    /**
     * The learner to use to learn the evaluator
     */
    private BatchLearner
        ,
         ? extends Evaluator>
        learner;
    
    /**
     * The CogxelConverter used to convert from a CogxelState to InputType
     */
    private CogxelConverter inputConverter;
    
    /**
     * The CogxelConverter used to convert OutputType to a CogxelState.
     */
    private CogxelConverter outputConverter;
    
    /**
     * The CogxelConverter used to convert from a CogxelState to 
     *  LearningDataType.
     */
    private CogxelConverter learningDataConverter;
    
    /**
     * Human-readable name for this module
     */
    private String name;
    
    /**
     * Default name for this module
     */
    public static final String DEFAULT_NAME = 
        "Learned Evaluator-based Cognitive Module";
    
    /**
     * Creates a new instance of CognitiveModuleFactoryEvaluatorLearner.
     */
    public EvaluatorBasedCognitiveModuleFactoryLearner()
    {
        this( (BatchLearner
                ,
                 ? extends Evaluator>) null, 
                 DEFAULT_NAME);
    }
    
    /**
     * Creates a new instance of CognitiveModuleFactoryEvaluatorLearner.
     *
     * @param  learner The learner to use to learn the evaluator.
     * @param name High-level name of the module
     */
    public EvaluatorBasedCognitiveModuleFactoryLearner(
        BatchLearner
        ,
         ? extends Evaluator> learner,
        String name )
    {
        this(learner, name, null, null, null);
    }

    /**
     * Creates a new instance of CognitiveModuleFactoryEvaluatorLearner.
     *
     * @param  learner The learner to use to learn the evaluator.
     * @param name High-level name of the module
     * @param  inputConverter The CogxelConverter used to convert from a
     *         CogxelState to InputType.
     * @param  outputConverter The CogxelConverter used to convert OutputType 
     *         to a CogxelState.
     * @param  learningDataConverter The CogxelConverter used to convert from 
     *         a CogxelState to LearningDataType. 
     */
    public EvaluatorBasedCognitiveModuleFactoryLearner(
        BatchLearner
            ,
             ? extends Evaluator> 
            learner,
        String name,
        CogxelConverter inputConverter,
        CogxelConverter outputConverter,
        CogxelConverter learningDataConverter )
    {
        super();
        
        this.setLearner(learner);
        this.setName( name );
        this.setInputConverter(inputConverter);
        this.setOutputConverter(outputConverter);
        this.setLearningDataConverter(learningDataConverter);
    }
    
    /**
     * Creates a new copy of a of CognitiveModuleFactoryEvaluatorLearner.
     *
     * @param  other The other CognitiveModuleFactoryEvaluatorLearner to copy.
     */
    public EvaluatorBasedCognitiveModuleFactoryLearner(
        EvaluatorBasedCognitiveModuleFactoryLearner other)
    {
        this(
            ObjectUtil.cloneSafe(other.getLearner()),
            other.getName(),
            other.getInputConverter().clone(),
            other.getOutputConverter().clone(),
            other.getLearningDataConverter().clone());
    }
    
    /**
     * Creates a copy of this EvaluatorBasedCognitiveModuleFactoryLearner.
     *
     * @return A copy of this EvaluatorBasedCognitiveModuleFactoryLearner.
     */
    @Override
    public EvaluatorBasedCognitiveModuleFactoryLearner clone()
    {
        return new EvaluatorBasedCognitiveModuleFactoryLearner(this);
    }
    
    /**
     * Learns a new EvaluatorBasedCognitiveModuleFactory
     * based on the given existing factory plus the given collection of 
     * CognitiveModelInput objects.
     *
     * @param  model The model to learn the module factory for.
     * @param  datasets The datasets to use to learn the module factory.
     * @return The learned EvaluatorBasedCognitiveModuleFactory.
     */
    public EvaluatorBasedCognitiveModuleFactory learn(
        CognitiveModel model, 
        Collection> datasets)
    {
        // Check the arguments to make sure they are valid.
        if ( model == null )
        {
            throw new IllegalArgumentException("model is null");
        }
        else if ( datasets == null || datasets.size() <= 0 )
        {
            throw new IllegalArgumentException("dataSets is null");
        }
        
        // Check the state to make sure it is valid.
        // Note we don't need an output converter at this point, so
        // we won't check to ensure it's not null (this means that outputs
        // won't be written to CogxelState with null output converter though)
        if ( this.getLearner() == null )
        {
            throw new IllegalStateException("Learner is null");
        }
        else if ( this.getInputConverter() == null )
        {
            throw new IllegalStateException("Input converter is null");
        }
        else if ( this.getLearningDataConverter() == null )
        {
            throw new IllegalStateException("Learning data converter is null");
        }
              
        // Create the CogxelState to LearningDataType converter.
        CogxelConverter converter = 
            this.getLearningDataConverter().clone();
        converter.setSemanticIdentifierMap(model.getSemanticIdentifierMap());
        
        // Create the Dataset to hold the learned data.
        Collection learningSet = 
            new LinkedList();
        
        // Go through the inputs data sequences.
        for ( Collection dataset : datasets )
        {
            // Reset the model for the new data sequence.
            model.resetCognitiveState();
            
            // Go through the sequence and give the model each of the inputs.
            for ( CognitiveModelInput input : dataset )
            {
                // Update the model based on the input
                model.update(input);
                
                // Get the model's current state.
                CognitiveModelState state = model.getCurrentState();
                
                // Get the learning example from the Cogxel converter.
                LearningDataType example = 
                    converter.fromCogxels(state.getCogxels());
                
                // Add the example to the learning set.
                learningSet.add(example);
            }
        }
        
        // Perform the learning algorithm.
        Evaluator learned = 
            this.getLearner().learn(learningSet);
        
        // Create the settings based on the learned Evaluator plus the
        // input and output CogxelConverters that this object was given.
        EvaluatorBasedCognitiveModuleSettings settings =
            new EvaluatorBasedCognitiveModuleSettings(
                learned, this.getInputConverter(), this.getOutputConverter());
        
        // 
        return new EvaluatorBasedCognitiveModuleFactory(
            settings, this.getName() );
    }

    /**
     * Gets the learner used to create the Evaluator for the module.
     *
     * @return The learner used to create the Evaluator for the module.
     */
    public BatchLearner
        ,
         ? extends Evaluator> 
        getLearner()
    {
        return this.learner;
    }
    
    /**
     * Sets the learner used to create the Evaluator for the module.
     *
     * @param  learner The learner used to create the Evaluator for the module.
     */
    public void setLearner(
        BatchLearner
        ,
         ? extends Evaluator> learner)
    {
        this.learner = learner;
    }

    /**
     * Gets the CogxelConverter used to convert from a CogxelState to InputType.
     *
     * @return The CogxelConverter used to convert from a CogxelState to 
     *         InputType.
     */
    public CogxelConverter getInputConverter()
    {
        return inputConverter;
    }

    /**
     * Sets the CogxelConverter used to convert from a CogxelState to InputType.
     *
     * @param  inputConverter The CogxelConverter used to convert from a 
     *         CogxelState to InputType.
     */
    public void setInputConverter(
        CogxelConverter inputConverter)
    {
        this.inputConverter = inputConverter;
    }

    /**
     * Gets the CogxelConverter used to convert OutputType to a CogxelState.
     *
     * @return The CogxelConverter used to convert OutputType to a CogxelState.
     */
    public CogxelConverter getOutputConverter()
    {
        return outputConverter;
    }

    /**
     * Sets the CogxelConverter used to convert OutputType to a CogxelState.
     *
     * @param  outputConverter The CogxelConverter used to convert OutputType 
     *         to a CogxelState.
     */
    public void setOutputConverter(
        CogxelConverter outputConverter)
    {
        this.outputConverter = outputConverter;
    }

    /**
     * Gets the CogxelConverter used to convert from a CogxelState to 
     * LearningDataType.
     *
     * @return The CogxelConverter used to convert from a CogxelState to 
     *         LearningDataType.
     */
    public CogxelConverter getLearningDataConverter()
    {
        return learningDataConverter;
    }

    /**
     * Sets the CogxelConverter used to convert from a CogxelState to 
     * LearningDataType.
     *
     * @param  learningDataConverter The CogxelConverter used to convert from 
     *         a CogxelState to LearningDataType.
     */
    public void setLearningDataConverter(
        CogxelConverter learningDataConverter)
    {
        this.learningDataConverter = learningDataConverter;
    }

    /**
     * Getter for name
     * @return 
     * Human-readable name for this module
     */
    public String getName()
    {
        return this.name;
    }

    /**
     * Setter for name
     * @param name 
     * Human-readable name for this module
     */
    public void setName(
        String name)
    {
        this.name = name;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy