001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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
018package org.apache.commons.math3.ml.neuralnet.sofm;
019
020import java.util.Iterator;
021import org.apache.commons.math3.ml.neuralnet.Network;
022
023/**
024 * Trainer for Kohonen's Self-Organizing Map.
025 *
026 * @since 3.3
027 */
028public class KohonenTrainingTask implements Runnable {
029    /** SOFM to be trained. */
030    private final Network net;
031    /** Training data. */
032    private final Iterator<double[]> featuresIterator;
033    /** Update procedure. */
034    private final KohonenUpdateAction updateAction;
035
036    /**
037     * Creates a (sequential) trainer for the given network.
038     *
039     * @param net Network to be trained with the SOFM algorithm.
040     * @param featuresIterator Training data iterator.
041     * @param updateAction SOFM update procedure.
042     */
043    public KohonenTrainingTask(Network net,
044                               Iterator<double[]> featuresIterator,
045                               KohonenUpdateAction updateAction) {
046        this.net = net;
047        this.featuresIterator = featuresIterator;
048        this.updateAction = updateAction;
049    }
050
051    /**
052     * {@inheritDoc}
053     */
054    public void run() {
055        while (featuresIterator.hasNext()) {
056            updateAction.update(net, featuresIterator.next());
057        }
058    }
059}