Ejemplo n.º 1
0
        public double FindMin(IFunction function)
        {
            double[] currentState = RandGenerator.RandomArray
                                        (dimensions, searchSpace[0], searchSpace[1]);

            double T     = maxTemp;
            double alpha = 0.999;

            for (int i = 0; i < iterTreshold; i++)
            {
                double[] newState = new double[dimensions];
                currentState.CopyTo(newState, 0);
                for (int j = 0; j < dimensions; j++)
                {
                    newState[j] += RandGenerator.RandomNormal(-1.0, 1.0, 1) * T;
                }
                if (AcceptanceProb(function.Calculate(currentState), function.Calculate(newState), T) >= rand.NextDouble())
                {
                    currentState = newState;
                }

                T *= alpha;
            }
            return(function.Calculate(currentState));
        }
Ejemplo n.º 2
0
        public double FindMin(IFunction fun)
        {
            List <double[]> nests = new List <double[]>();

            for (int i = 0; i < nestCount; i++)
            {
                nests.Add(RandGenerator.RandomArray(dimension, Space[0], Space[1]));
            }

            for (int i = 0; i < iteration; i++)
            {
                int      NestIndex    = rand.Next(0, nests.Count);
                double[] flightResult = Move(nests[NestIndex], iterationWalk);
                int      cNestIndex   = ChooseNest(NestIndex, nests);

                if (fun.Calculate(flightResult) <= fun.Calculate(nests[cNestIndex]))
                {
                    nests[cNestIndex] = flightResult;
                }

                nests = nests.OrderBy(x => fun.Calculate(x)).ToList();
                Kill(nests, survivaleRate);

                for (int j = nests.Count; j < nestCount; j++)
                {
                    nests.Add(RandGenerator.RandomArray(dimension, Space[0], Space[1]));
                }
            }

            nests = nests.OrderBy(x => fun.Calculate(x)).ToList();
            return(fun.Calculate(nests[0]));
        }
Ejemplo n.º 3
0
        public double FindMin(IFunction fun)
        {
            List <Lift> population = new List <Lift>();

            for (int i = 0; i < populations; i++)
            {
                Lift l = new Lift(RandGenerator.RandomArray(dimension, Space[0], Space[1]));
                population.Add(l);
            }

            double[] bestPos = RandGenerator.RandomArray(dimension, Space[0], Space[1]);
            double   best    = fun.Calculate(bestPos);

            for (int l = 0; l < generations; l++)
            {
                for (int i = 0; i < populations; i++)
                {
                    for (int j = 0; j < populations; j++)
                    {
                        if (i == j)
                        {
                            continue;
                        }

                        if (fun.Calculate(population[j].Pos) < fun.Calculate(population[i].Pos))
                        {
                            double dist = Calculate(population[i], population[j]);

                            double[] Position = new double[dimension];

                            population[i].Pos.CopyTo(Position, 0);

                            for (int k = 0; k < dimension; k++)
                            {
                                Position[k] += (population[j].Pos[k] - population[i].Pos[k]) * dist;
                                Position[k] += ((random.NextDouble() - 0.5) * rndFactor);
                            }
                            if (fun.Calculate(Position) < fun.Calculate(population[i].Pos))
                            {
                                population[i].Pos = Position;
                            }
                        }
                    }
                    double[] pos = population.Aggregate((x, y) => fun.Calculate(x.Pos) < fun.Calculate(y.Pos) ? x : y).Pos;
                    if (fun.Calculate(pos) < best)
                    {
                        bestPos = pos;
                        best    = fun.Calculate(bestPos);
                    }
                }
            }
            return(fun.Calculate(bestPos));
        }
Ejemplo n.º 4
0
 private double[] Move(double[] start, int iteration)
 {
     double[] result = new double[start.Length];
     start.CopyTo(result, 0);
     for (int i = 0; i < iteration; i++)
     {
         for (int j = 0; j < result.Length; j++)
         {
             result[j] += RandGenerator.RandomNormal(-1.0, 1.0, 100) * walkSize;
         }
     }
     return(result);
 }
Ejemplo n.º 5
0
        public double FindMin(IFunction fun)
        {
            List<Molecule> molecules = new List<Molecule>();
            double[] bestSwarmPosition = RandGenerator.RandomArray(dimension, Space[0], Space[1]);


            for (int i = 0; i < moleculesCount; i++)
            {
                molecules.Add(
                    new Molecule(
                        RandGenerator.RandomArray(dimension, Space[0], Space[1]), 
                        RandGenerator.RandomArray(dimension, -1.0, 1.0))
                    );

                if (fun.Calculate(molecules.Last().bestPosition) < fun.Calculate(bestSwarmPosition))
                    bestSwarmPosition = molecules.Last().bestPosition;
                
            }

            for (int i = 0; i < iterarion; i++)
            {
                foreach (Molecule p in molecules)
                {
                    for (int j = 0; j < dimension; j++)
                    {
                        double randomPositionFactor = random.NextDouble();
                        double randomSwarmFactor = random.NextDouble();

                        p.velocity[j] = (p.velocity[j] * velocity) + bestPositionFactor * randomPositionFactor * 
                            (p.bestPosition[j] - p.position[j]) +bestSwarmPos * randomSwarmFactor * (bestSwarmPosition[j] - p.position[j]);
                    }

                    for (int j = 0; j < dimension; j++) p.position[j] += p.velocity[j];
                    
                    if (fun.Calculate(p.position) < fun.Calculate(p.bestPosition))  p.bestPosition = p.position;
                    if (fun.Calculate(p.position) < fun.Calculate(bestSwarmPosition)) bestSwarmPosition = p.position;
                      
                }
            }
            return fun.Calculate(bestSwarmPosition);
        }