package adaline;


import com.naaturel.ANN.domain.abstraction.AlgorithmStep;
import com.naaturel.ANN.domain.abstraction.Model;
import com.naaturel.ANN.domain.model.helpers.ModelCreator;
import com.naaturel.ANN.domain.model.neuron.Neuron;
import com.naaturel.ANN.infrastructure.dataset.DataSet;
import com.naaturel.ANN.infrastructure.dataset.DatasetExtractor;
import com.naaturel.ANN.domain.model.neuron.*;
import com.naaturel.ANN.domain.model.training.TrainingPipeline;
import com.naaturel.ANN.implementation.adaline.AdalineTrainingContext;
import com.naaturel.ANN.implementation.gradientDescent.*;
import com.naaturel.ANN.implementation.simplePerceptron.SimpleCorrectionStep;
import com.naaturel.ANN.implementation.simplePerceptron.SimpleDeltaStep;
import com.naaturel.ANN.implementation.simplePerceptron.SimpleErrorRegistrationStep;
import com.naaturel.ANN.implementation.simplePerceptron.SimplePredictionStep;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AdalineTest {

    private DataSet dataset;
    private AdalineTrainingContext context;

    private Model model;

    private TrainingPipeline pipeline;

    @BeforeEach
    public void init(){
        dataset = new DatasetExtractor()
                .extract("C:/Users/Laurent/Desktop/ANN-framework/src/main/resources/assets/and-gradient.csv", 1);

        List<Synapse> syns = new ArrayList<>();
        syns.add(new Synapse(new Input(0), new Weight(0)));
        syns.add(new Synapse(new Input(0), new Weight(0)));

        model = ModelCreator.createModel(new int[]{1}, 2, new Linear(1, 0));

        context = new AdalineTrainingContext(model, dataset);

        List<AlgorithmStep> steps = List.of(
                new SimplePredictionStep(context),
                new SimpleDeltaStep(context),
                new SquareLossStep(context),
                new SimpleErrorRegistrationStep(context),
                new SimpleCorrectionStep(context)
        );

        pipeline = new TrainingPipeline(steps)
                .stopCondition(ctx -> ctx.globalLoss <= 0.1329F || ctx.epoch > 10000)
                .beforeEpoch(ctx -> {
                    ctx.globalLoss = 0.0F;
                });
    }

    @Test
    public void test_the_whole_algorithm(){

        List<Float> expectedGlobalLosses = List.of(
                0.501522F,
                0.498601F
        );

        context.learningRate = 0.03F;
        pipeline.afterEpoch(ctx -> {
            ctx.globalLoss /= context.dataset.size();

            int index = ctx.epoch-1;
            if(index >= expectedGlobalLosses.size()) return;

            //assertEquals(expectedGlobalLosses.get(index), context.globalLoss, 0.00001f);
        });

        pipeline.run(context);
        assertEquals(214, context.epoch);
    }
}

