gov.sandia.cognition.learning.data.DefaultWeightedInputOutputPair Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cognitive-foundry Show documentation
Show all versions of cognitive-foundry Show documentation
A single jar with all the Cognitive Foundry components.
/*
* File: DefaultWeightedInputOutputPair.java
* Authors: Justin Basilico
* Company: Sandia National Laboratories
* Project: Cognitive Foundry
*
* Copyright May 26, 2010, 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.learning.data;
import gov.sandia.cognition.util.Pair;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* A default implementation of the {@code WeightedInputOutputPair} interface.
* Stores pointers to the input and output plus the weight as a double.
*
* @param
* The type for the input object in the pair.
* @param
* The type for the output object in the pair.
*
* @author Justin Basilico
* @since 3.0
*/
public class DefaultWeightedInputOutputPair
extends DefaultInputOutputPair
implements WeightedInputOutputPair
{
/** The default weight is {@value}. */
public static final double DEFAULT_WEIGHT = 1.0;
/** Weighting term for the InputOutputPair. */
private double weight;
/**
* Creates a new {@code DefaultWeightedInputOutputPair} with null as the
* input and output and a default weight of 1.0.
*/
public DefaultWeightedInputOutputPair()
{
this(null, null, 1.0);
}
/**
* Creates a new {@code DefaultWeightedInputOutputPair} with the given
* input, output, and weight.
*
* @param input
* The input.
* @param output
* The output.
* @param weight
* The weight.
*/
public DefaultWeightedInputOutputPair(
final InputType input,
final OutputType output,
final double weight)
{
super(input, output);
this.setWeight(weight);
}
/**
* Creates a new {@code DefaultWeightedInputOutputPair} with the given
* input and output from the given pair plus a weight.
*
* @param pair
* The pair to provide the input and output values.
* @param weight
* The weight.
*/
public DefaultWeightedInputOutputPair(
final Pair extends InputType, ? extends OutputType> pair,
final double weight)
{
this(pair.getFirst(), pair.getSecond(), weight);
}
/**
* Creates a new {@code DefaultWeightedInputOutputPair} that is a shallow
* copy of the given {@code WeightedInputOuptutPair}.
*
* @param other
* The other weighted input-output pair to copy the values of.
*/
public DefaultWeightedInputOutputPair(
final WeightedInputOutputPair extends InputType, ? extends OutputType> other)
{
this(other.getInput(), other.getOutput(), other.getWeight());
}
public double getWeight()
{
return this.weight;
}
/**
* Sets the weight for the pair.
*
* @param weight Weighting term for the InputOutputPair.
*/
public void setWeight(
final double weight)
{
this.weight = weight;
}
/**
* Convenience method to create a new, empty
* {@code DefaultWeightedInputOutputPair}.
*
* @param
* The type for the input object in the pair.
* @param
* The type for the output object in the pair.
* @return
* A new, empty {@code DefaultWeightedInputOutputPair}.
*/
public static DefaultWeightedInputOutputPair
create()
{
return new DefaultWeightedInputOutputPair();
}
/**
* Convenience method to create a new
* {@code DefaultWeightedInputOutputPair}.
*
* @param
* The type for the input object in the pair.
* @param
* The type for the output object in the pair.
* @param input
* The input.
* @param output
* The output.
* @param weight
* The weight.
* @return
* A new default weighted input-output pair with the given input,
* output, and weight.
*/
public static DefaultWeightedInputOutputPair
create(
final InputType input,
final OutputType output,
final double weight)
{
return new DefaultWeightedInputOutputPair(input, output, weight);
}
/**
* Takes two Collections of data and creates a single
* ArrayList out of them.
*
* @param The type of the input.
* @param The type of the output.
* @param inputs
* Collection of the data to transform into the input of the
* WeightedInputOutputPair, must have the same size as targets
* @param outputs
* Collection of the data to transform into the output of the
* WeightedInputOutputPair, must have the same size as inputs
* @param weights
* Collection of weights, must have the same size as inputs/targets
* @return
* ArrayList of the same type as the input/output
* Collections
*/
public static ArrayList>
mergeCollections(
final Collection inputs,
final Collection outputs,
final Collection extends Number> weights)
{
final int count = inputs.size();
if ((count != outputs.size()) &&
(count != weights.size()))
{
throw new IllegalArgumentException(
"The inputs, outputs, and weights collections "
+ "must be the same size ("
+ inputs.size() + ", " + outputs.size() + ", " + weights.size()
+ ")");
}
final Iterator ii = inputs.iterator();
final Iterator io = outputs.iterator();
final Iterator extends Number> iw = weights.iterator();
final ArrayList> result =
new ArrayList>(count);
for (int n = 0; n < count; n++)
{
result.add(create(ii.next(), io.next(), iw.next().doubleValue()));
}
return result;
}
}