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.math4.neuralnet.sofm;
019
020import java.util.Iterator;
021
022import org.apache.commons.math4.neuralnet.Network;
023
024/**
025 * Trainer for Kohonen's Self-Organizing Map.
026 *
027 * @since 3.3
028 */
029public class KohonenTrainingTask implements Runnable {
030    /** SOFM to be trained. */
031    private final Network net;
032    /** Training data. */
033    private final Iterator<double[]> featuresIterator;
034    /** Update procedure. */
035    private final KohonenUpdateAction updateAction;
036
037    /**
038     * Creates a (sequential) trainer for the given network.
039     *
040     * @param net Network to be trained with the SOFM algorithm.
041     * @param featuresIterator Training data iterator.
042     * @param updateAction SOFM update procedure.
043     */
044    public KohonenTrainingTask(Network net,
045                               Iterator<double[]> featuresIterator,
046                               KohonenUpdateAction updateAction) {
047        this.net = net;
048        this.featuresIterator = featuresIterator;
049        this.updateAction = updateAction;
050    }
051
052    /**
053     * {@inheritDoc}
054     */
055    @Override
056    public void run() {
057        while (featuresIterator.hasNext()) {
058            updateAction.update(net, featuresIterator.next());
059        }
060    }
061}