001/*
002 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.tribuo.classification.example;
018
019import org.tribuo.Example;
020import org.tribuo.Trainer;
021import org.tribuo.classification.Label;
022import org.tribuo.impl.ArrayExample;
023
024import java.util.ArrayList;
025import java.util.List;
026
027/**
028 * A data source of two interleaved half circles.
029 * <p>
030 * Also known as the two moons dataset.
031 */
032public final class InterlockingCrescentsDataSource extends DemoLabelDataSource {
033
034    /**
035     * For OLCUT.
036     */
037    private InterlockingCrescentsDataSource() {
038        super();
039    }
040
041    /**
042     * Constructs an interlocking crescents data source.
043     *
044     * @param numSamples The number of samples to generate.
045     */
046    public InterlockingCrescentsDataSource(int numSamples) {
047        super(numSamples, Trainer.DEFAULT_SEED);
048        postConfig();
049    }
050
051    @Override
052    protected List<Example<Label>> generate() {
053        List<Example<Label>> list = new ArrayList<>();
054
055        for (int i = 0; i < numSamples / 2; i++) {
056            double[] values = new double[2];
057            values[0] = Math.cos(Math.PI * ((double) i) / ((numSamples / 2) - 1));
058            values[1] = Math.sin(Math.PI * ((double) i) / ((numSamples / 2) - 1));
059            list.add(new ArrayExample<>(FIRST_CLASS, FEATURE_NAMES, values));
060        }
061
062        for (int i = numSamples / 2; i < numSamples; i++) {
063            int j = i - numSamples / 2;
064            double[] values = new double[2];
065
066            values[0] = 1 - Math.cos(Math.PI * ((double) j) / ((numSamples / 2) - 1));
067            values[1] = 0.5 - Math.sin(Math.PI * ((double) j) / ((numSamples / 2) - 1));
068            list.add(new ArrayExample<>(SECOND_CLASS, FEATURE_NAMES, values));
069        }
070
071        return list;
072    }
073
074    @Override
075    public String toString() {
076        return "InterlockingCrescents(numSamples=" + numSamples + ")";
077    }
078}