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

namespace EvoBoat.EvoAlg.DE
{
    public class Num_DEAlg<S> : DEAlg<NumOptProblem<S>, S, Vector, double>
        where S : VecSolution<Vector, double>, new()
    {
        public Num_DEAlg(int seed = 0) : base(seed)
        {
        }

        public Num_DEAlg(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 double Difference(double pos1, double pos2)
        {
            return pos1 - pos2;
        }

        public override double Increase(double pos, double v)
        {
            return pos + v;
        }

        public override void Repair(S sol, int index, int d, double oldv)
        {
            if (sol.Content[d] < Problem.Lowers[d] || sol.Content[d] > Problem.Uppers[d])
                sol.Content[d] = Problem.Lowers[d] + Rand.NextDouble() * Problem.Ranges[d];
        }
    }
}
