Class Samples
java.lang.Object
org.moeaframework.analysis.sample.Samples
- All Implemented Interfaces:
Iterable<Sample>,DataStream<Sample>,Displayable,Formattable<Sample>
public class Samples
extends Object
implements Iterable<Sample>, Formattable<Sample>, DataStream<Sample>
A collection of samples, typically associated with a parameter set that generated the samples.
Samples can be stored in a file using the save(File) and load(File, ParameterSet) methods.
The file begins with a header line listing the parameter names. Then, each sample is written on its own line,
with the parameter values separated by whitespace. The ordering of parameters must match the provided
ParameterSet.
-
Constructor Summary
ConstructorsConstructorDescriptionSamples(ParameterSet parameterSet) Constructs an empty collection of samples.Samples(ParameterSet parameterSet, Iterable<Sample> samples) Constructs an empty collection of samples.Samples(ParameterSet parameterSet, Collection<Sample> samples) Constructs an empty collection of samples. -
Method Summary
Modifier and TypeMethodDescriptionReturns the contents of this object as aTabularDatainstance, which can be used to save, print, or format the data in various ways.voiddistributeAll(Consumer<Sample> consumer) Evaluates each sample in parallel, distributing across all available processors.<T> SampledResults<T>distributeAll(Function<Sample, T> function) Evaluates each sample in parallel, distributing across all available processors, and collecting the results in aSampledResults.booleanvoidevaluateAll(Consumer<Sample> consumer) Evaluates each sample.<T> SampledResults<T>evaluateAll(Function<Sample, T> function) Evaluates each sample, collecting the results in aSampledResults.<T> SampledResults<T>evaluateAll(Function<Sample, T> function, ExecutorService executor) Evaluates each sample using the provided executor service, potentially distributing the evaluations in parallel.voidget(int index) Returns the sample at the given index.That parameter set defining the parameters included in these samples.inthashCode()iterator()static Samplesload(File file, ParameterSet parameterSet) Loads the samples from a file.static Samplesload(Reader reader, ParameterSet parameterSet) Loads the samples from a file.voidSaves the samples to a file.voidSaves the samples to a file.intsize()Returns the number of samples.stream()Returns aStreamof the values in this data stream.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.moeaframework.analysis.stream.DataStream
any, distinct, enumerate, filter, first, groupBy, isEmpty, keyedOn, map, measure, reduce, reduce, single, singleOrDefault, skip, sorted, sorted, values, valuesMethods inherited from interface org.moeaframework.util.format.Displayable
displayMethods inherited from interface org.moeaframework.util.format.Formattable
display, display, display, save, save, saveMethods inherited from interface java.lang.Iterable
spliterator
-
Constructor Details
-
Samples
Constructs an empty collection of samples.- Parameters:
parameterSet- the parameter set defining the parameters included in each sample
-
Samples
Constructs an empty collection of samples.- Parameters:
parameterSet- the parameter set defining the parameters included in each samplesamples- the collection of samples
-
Samples
Constructs an empty collection of samples.- Parameters:
parameterSet- the parameter set defining the parameters included in each samplesamples- the collection of samples
-
-
Method Details
-
getParameterSet
That parameter set defining the parameters included in these samples.- Returns:
- the parameter set
-
size
public int size()Returns the number of samples.- Specified by:
sizein interfaceDataStream<Sample>- Returns:
- the number of samples
-
get
Returns the sample at the given index.- Parameters:
index- the index of the sample- Returns:
- the sample at the given index
- Throws:
IndexOutOfBoundsException- if the index is out of bounds
-
evaluateAll
Evaluates each sample, collecting the results in aSampledResults.- Type Parameters:
T- the return type of the function- Parameters:
function- the function used to evaluate each sample- Returns:
- the results
-
evaluateAll
Evaluates each sample.- Parameters:
consumer- the consumer used to evaluate each sample
-
distributeAll
public <T> SampledResults<T> distributeAll(Function<Sample, T> function) throws InterruptedException, ExecutionExceptionEvaluates each sample in parallel, distributing across all available processors, and collecting the results in aSampledResults.- Type Parameters:
T- the return type of the function- Parameters:
function- the function used to evaluate each sample- Returns:
- the results
- Throws:
InterruptedException- if the current thread was interrupted while waiting for evaluations to completeExecutionException- if the function threw an exception
-
distributeAll
public void distributeAll(Consumer<Sample> consumer) throws InterruptedException, ExecutionException Evaluates each sample in parallel, distributing across all available processors.- Parameters:
consumer- the consumer used to evaluate each sample- Throws:
InterruptedException- if the current thread was interrupted while waiting for evaluations to completeExecutionException- if the consumer threw an exception
-
evaluateAll
public <T> SampledResults<T> evaluateAll(Function<Sample, T> function, ExecutorService executor) throws InterruptedException, ExecutionExceptionEvaluates each sample using the provided executor service, potentially distributing the evaluations in parallel. As such, the function should not have any side-effects. The caller is expected to manage the lifecycle of the executor.- Type Parameters:
T- the return type of the function- Parameters:
function- the function used to evaluate each sampleexecutor- the executor for distributing the evaluations- Returns:
- the results
- Throws:
InterruptedException- if the current thread was interrupted while waiting for evaluations to completeExecutionException- if the function threw an exception
-
iterator
-
hashCode
public int hashCode() -
equals
-
asTabularData
Description copied from interface:FormattableReturns the contents of this object as aTabularDatainstance, which can be used to save, print, or format the data in various ways.- Specified by:
asTabularDatain interfaceDataStream<Sample>- Specified by:
asTabularDatain interfaceFormattable<Sample>- Returns:
- the
TabularDatainstance
-
stream
Description copied from interface:DataStreamReturns aStreamof the values in this data stream.- Specified by:
streamin interfaceDataStream<Sample>- Returns:
- the stream
-
forEach
-
load
Loads the samples from a file.- Parameters:
file- the source fileparameterSet- the parameter set- Returns:
- the loaded samples
- Throws:
IOException- if an I/O error occurred
-
load
Loads the samples from a file.- Parameters:
reader- the readerparameterSet- the parameter set- Returns:
- the loaded samples
- Throws:
IOException- if an I/O error occurred
-
save
Saves the samples to a file.- Parameters:
file- the destination file- Throws:
IOException- if an I/O error occurred
-
save
Saves the samples to a file.- Parameters:
writer- the writer- Throws:
IOException- if an I/O error occurred
-