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

namespace EvoBoat.EvoAlg.BBO
{
    public class Num_LocalBBOAlg<S> : LocalBlendBBOAlg<NumOptProblem<S>, S, Vector, double>
        where S : VecSolution<Vector, double>, new()
    {
        public Num_LocalBBOAlg() : base()
        {
            this.FunSetToplogy = SetRandomTopology(Rand, 3);
        }

        public Num_LocalBBOAlg(NumOptProblem<S> problem, int seed = 0) : base(problem, seed)
        {
            this.FunSetToplogy = SetRandomTopology(Rand, 3);
            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 double Blend(S sol1, S sol2, int d)
        {
            double a = Rand.NextDouble();
            return a * sol1.Content[d] + (1 - a) * sol2.Content[d];
        }

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

        public static Num_LocalBBOAlg<S> LRingBBOAlg()
        {
            return new Num_LocalBBOAlg<S>() { Name = "LRingBBO", FunSetToplogy = SetRingTopology };
        }

        public static Num_LocalBBOAlg<S> LRingBBOAlg(NumOptProblem<S> problem, int seed = 0)
        {
            return new Num_LocalBBOAlg<S>(problem, seed) { Name = "LRingBBO", FunSetToplogy = SetRingTopology };
        }

        public static Num_LocalBBOAlg<S> LSquareBBOAlg()
        {
            return new Num_LocalBBOAlg<S>() { Name = "LSquareBBO", FunSetToplogy = SetSquareTopology };
        }

        public static Num_LocalBBOAlg<S> LSquareBBOAlg(NumOptProblem<S> problem, int seed = 0)
        {
            return new Num_LocalBBOAlg<S>(problem, seed) { Name = "LSquareBBO", FunSetToplogy = SetSquareTopology };
        }

        public static Num_LocalBBOAlg<S> LRandBBOAlg(int kSize = 3)
        {
            Num_LocalBBOAlg<S> alg = new Num_LocalBBOAlg<S>() { Name = "LRandBBO" };
            alg.FunSetToplogy = SetRandomTopology(alg.Rand, kSize);
            return alg;
        }

        public static Num_LocalBBOAlg<S> LRandBBOAlg(NumOptProblem<S> problem, int kSize = 3, int seed = 0)
        {
            Num_LocalBBOAlg<S> alg = new Num_LocalBBOAlg<S>(problem, seed) { Name = "LRandBBO" };
            alg.FunSetToplogy = SetRandomTopology(alg.Rand, kSize);
            return alg;
        }
    }
}
