using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EvoBoat.EvoAlg.GA;

namespace EvoBoat.EvoAlg.IntProg
{
    public class IP_GA<P,S> : GA<P, S, IntVector, int>
        where P : IntProgProblem<S>
        where S : VecSolution<IntVector, int>, new()
    {
        public static Func<EAlg<P, S, IntVector, int>, S, int, S> MutationFunc = (alg, sol, index) =>
        {
            S sol1 = new S() { Content = sol.Content.Clone() };
            for (int d = 0; d < alg.Problem.Dimension; d++)
                if (alg.Rand.Next() % 2 == 0)
                    sol1.Content[d] = alg.Rand.Next(alg.Problem.Lowers[d], alg.Problem.Uppers[d]);
            alg.Evaluate(sol1);
            return sol1;
        };

        public IP_GA() : base()
        { }

        public IP_GA(P problem, int seed = 0) : base(problem, seed)
        {
            this.Factory = new IntVectorSolutionFactory<P, S>(problem);
        }

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

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