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

namespace EvoBoat.EvoAlg.ZOProg
{
    public class ZO_GA<P, S> : GA<P, S, BoolVector, bool>
        where P : ZeroOneProgProblem<S>
        where S : VecSolution<BoolVector, bool>, new()
    {
        public static Func<EAlg<P, S, BoolVector, bool>, 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] = !sol1.Content[d];
            alg.Evaluate(sol1);
            return sol;
        };

        public ZO_GA() : base()
        { }

        public ZO_GA(P problem, int seed = 0) : base(problem, seed)
        {
            this.Factory = new BoolVectorSolutionFactory<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 BoolVectorSolutionFactory<P, S>(Problem);
        }

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