using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EvoBoat.EvoAlg.GA
{
    public class Num_SGA<S> : SGA<NumOptProblem<S>, S, Vector, double>
        where S : VecSolution<Vector, double>, new()
    {
        public Num_SGA() : base()
        { }
        
        public Num_SGA(NumOptProblem<S> problem, int seed = 0) : base(problem, seed)
        {
            this.Factory = new VectorSolutionFactory<NumOptProblem<S>, S>(problem);
        }

        public override void SetFactory()
        {
            if (Problem == null)
                throw new ArgumentNullException("Please assign the problem instance before set the factory");
            Factory = new VectorSolutionFactory<NumOptProblem<S>, S>(Problem);
        }

        public override S Crossover(S sol1, S sol2)
        {
            int n = sol1.Content.Length;
            S ch = Factory.CreateEmpty();
            double a = Rand.NextDouble();
            for (int d = 0; d < n; d++)
                ch.Content[d] = a * sol1.Content[d] + (1 - a) * sol2.Content[d];
            this.Evaluate(ch);
            return ch;
        }

        public override S Mutate(S sol, int index)
        {
            return Num_GA<S>.MutationFunc(this, sol, index);
        }
    }
}
