001 /*
002 * Java Genetic Algorithm Library (jenetics-3.7.0).
003 * Copyright (c) 2007-2016 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019 */
020 package org.jenetics.tool.trial;
021
022 import static java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024
025 import java.io.Serializable;
026 import java.util.Arrays;
027 import java.util.List;
028
029 import javax.xml.bind.annotation.XmlAccessType;
030 import javax.xml.bind.annotation.XmlAccessorType;
031 import javax.xml.bind.annotation.XmlElement;
032 import javax.xml.bind.annotation.XmlRootElement;
033 import javax.xml.bind.annotation.XmlType;
034 import javax.xml.bind.annotation.adapters.XmlAdapter;
035 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
036
037 import org.jenetics.util.ISeq;
038
039 /**
040 * Collection of sample {@code Data} objects.
041 *
042 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
043 * @version 3.4
044 * @since 3.4
045 */
046 @XmlJavaTypeAdapter(DataSet.Model.Adapter.class)
047 public final class DataSet implements Serializable {
048
049 private static final long serialVersionUID = 1L;
050
051 private final ISeq<Data> _sets;
052
053 private DataSet(final ISeq<Data> sets) {
054 _sets = requireNonNull(sets);
055
056 if (_sets.isEmpty()) {
057 throw new IllegalArgumentException(
058 "Data set names must not be empty."
059 );
060 }
061 }
062
063 public ISeq<Data> values() {
064 return _sets;
065 }
066
067 public Data get(final String name) {
068 return _sets.stream()
069 .filter(d -> d.getName().equals(name))
070 .findFirst()
071 .get();
072 }
073
074 public int nextParamIndex() {
075 final ISeq<Integer> indexes = _sets.map(Data::nextParamIndex);
076 if (!indexes.forAll(i -> indexes.get(0).equals(i))) {
077 throw new IllegalStateException("Inconsistent state.");
078 }
079
080 return indexes.get(0);
081 }
082
083 public int dataSize() {
084 return _sets.get(0).dataSize();
085 }
086
087 public void add(final double[] values) {
088 if (values.length != _sets.length()) {
089 throw new IllegalArgumentException(format(
090 "Expected %d values, but got %d.", _sets.length(), values.length
091 ));
092 }
093
094 for (int i = 0; i < values.length; ++i) {
095 _sets.get(i).currentSample().add(values[i]);
096 }
097 }
098
099 /**
100 * Create a new {@code DataSet} object with the given number of parameters
101 * and the data set names.
102 *
103 * @param parameterCount the number of parameters one data sample consist of
104 * @param dataSetNames the names of the created {@code Data} sets
105 * @return a new data set object
106 */
107 public static DataSet of(
108 final int parameterCount,
109 final String... dataSetNames
110 ) {
111 return new DataSet(
112 Arrays.stream(dataSetNames)
113 .map(name -> Data.of(name, parameterCount))
114 .collect(ISeq.toISeq())
115 );
116 }
117
118 /* *************************************************************************
119 * JAXB object serialization
120 * ************************************************************************/
121
122 @XmlRootElement(name = "data-set")
123 @XmlType(name = "org.jenetics.tool.trial.DataSet")
124 @XmlAccessorType(XmlAccessType.FIELD)
125 static final class Model {
126
127 @XmlElement(name = "data")
128 public List<Data> dataSet;
129
130 public static final class Adapter extends XmlAdapter<Model, DataSet> {
131 @Override
132 public Model marshal(final DataSet data) {
133 final Model model = new Model();
134 model.dataSet = data.values().asList();
135 return model;
136 }
137
138 @Override
139 public DataSet unmarshal(final Model model) {
140 return new DataSet(ISeq.of(model.dataSet));
141 }
142 }
143
144 }
145
146 }
|