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

namespace EvoBoat.EvoAlg.GA
{
    public class Num_GA<S> : GA<NumOptProblem<S>, S, Vector, double>
        where S : VecSolution<Vector, double>, new()
    {
        public static Func<EAlg<NumOptProblem<S>, S, Vector, double>, S, int, S> MutationFunc = (alg, sol, index) =>
        {
            Vector vec = sol.Content.Clone();
            for (int d = 0; d < alg.Problem.Dimension; d++)
                if (alg.Rand.Next() % 2 == 0)
                    vec[d] = alg.Problem.Lowers[d] + alg.Rand.NextDouble() * (alg.Problem.Uppers[d] - alg.Problem.Lowers[d]);
            S sol1 = new S() { Content = vec };
            alg.Evaluate(sol1);
            return sol1;
        };

        public Num_GA() : base()
        { }
        
        public Num_GA(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 Mutate(S sol, int index)
        {
            return MutationFunc(this, sol, index);
        }
    }
}
