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

namespace EvoBoat.EvoAlg.PSO
{
    public class Num_CLPSOAlg<S> : CLPSOAlg<NumOptProblem<S>, S, Vector, double>
        where S : VecSolution<Vector, double>, ICloneable<S>, new()
    {
        public Num_CLPSOAlg() : base()
        { }
        
        public Num_CLPSOAlg(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.Lowers[d];
                velocities[index][d] = 0;
            }
            else if (sol.Content[d] > Problem.Uppers[d])
            {
                sol.Content[d] = Problem.Uppers[d];
                velocities[index][d] = 0;
            }
        }
    }
}
