EndCriteria::Type DifferentialEvolution::minimize(Problem& p, const EndCriteria& endCriteria) {
        EndCriteria::Type ecType;

        upperBound_ = p.constraint().upperBound(p.currentValue());
        lowerBound_ = p.constraint().lowerBound(p.currentValue());
        currGenSizeWeights_ = Array(configuration().populationMembers,
                                    configuration().stepsizeWeight);
        currGenCrossover_ = Array(configuration().populationMembers,
                                  configuration().crossoverProbability);

        std::vector<Candidate> population(configuration().populationMembers,
                                          Candidate(p.currentValue().size()));
        fillInitialPopulation(population, p);

        std::partial_sort(population.begin(), population.begin() + 1, population.end(),
                          sort_by_cost());
        bestMemberEver_ = population.front();
        Real fxOld = population.front().cost;
        Size iteration = 0, stationaryPointIteration = 0;

        // main loop - calculate consecutive emerging populations
        while (!endCriteria.checkMaxIterations(iteration++, ecType)) {
            calculateNextGeneration(population, p.costFunction());
            std::partial_sort(population.begin(), population.begin() + 1, population.end(),
                              sort_by_cost());
            if (population.front().cost < bestMemberEver_.cost)
                bestMemberEver_ = population.front();
            Real fxNew = population.front().cost;
            if (endCriteria.checkStationaryFunctionValue(fxOld, fxNew, stationaryPointIteration,
                                                         ecType))
                break;
            fxOld = fxNew;
        };
        p.setCurrentValue(bestMemberEver_.values);
        p.setFunctionValue(bestMemberEver_.cost);
        return ecType;
    }
示例#2
0
int main(int argc, char *argv[])
{

    int numCells, numGens, *cells, *cells2, i;

    /* Print usage when we don't have the three arguments necessary */
    if (argc != 3)
    {
        fprintf(stderr, "usage: %s (numCells) (numGens)\n", argv[0]);
        exit(1);
    }

    numCells = atoi(argv[1]);
    numGens = atoi(argv[2]);

    /* Seed random number generator */
    srand(time(0));


    cells = (int*) malloc (numCells * sizeof(int));
    cells2 = (int*) malloc(numCells * sizeof(int));

    if (cells == NULL || cells2 == NULL)
    {
        fprintf(stderr, "Memory Allocation Failure!\n");
        exit(1);
    }
    
    /* Initialize the arrays with random values */
    for (i = 0; i < numCells; i++)
    {
        int randomNumber = rand() % 2;
        cells[i] = randomNumber;
        cells2[i] = randomNumber;
    }

    /* Print the first generation. */
    printArray(numCells, cells);

    /* Alternate between two arrays, calculating the next generation and
        printing the automaton out. */
    for (i = 0; i < numGens; i++)
    {
        if (i % 2 == 0)
        {
            calculateNextGeneration(numCells, cells, cells2);
            printArray(numCells, cells2);
        }
        else
        {
            calculateNextGeneration(numCells, cells2, cells);
            printArray(numCells, cells);
        }
    }

    free(cells);
    free(cells2);

    print_memory_leaks();

    return 0;
}