DifferentialEvolution::DifferentialEvolution()
    : PopulationBasedOptimisationAlgorithm() {
    setInitialisingFunctions({{
            [this](
                const arma::uword numberOfDimensions_,
                const arma::mat& initialParameters_) {
                population_ = initialParameters_;

                return initialParameters_;
            },
            "Population initialisation"
        },
        {   [this](
                const arma::uword numberOfDimensions_,
                const arma::mat& initialParameters_) {
                localBestObjectiveValues_.set_size(populationSize_);
                localBestObjectiveValues_.fill(std::numeric_limits<double>::infinity());

                return initialParameters_;
            },
            "Local best objective values initialisation"
        }
    });

    setNextParametersFunctions({{
            [this](
                const arma::uword numberOfDimensions_,
                const arma::mat& parameters_,
                const arma::rowvec& objectiveValues_,
                const arma::rowvec& differences_) {
                for (arma::uword n = 0; n < populationSize_; ++n) {
                    if (objectiveValues_(n) < localBestObjectiveValues_(n)) {
                        population_.col(n) = parameters_.col(n);
                    }
                }

                arma::mat populationCandidates(arma::size(population_));
                for (arma::uword n = 0; n < populationSize_; ++n) {
                    arma::uvec randomIndices = randomPermutationVector(populationSize_, 3);
                    populationCandidates.col(n) = population_.col(randomIndices(0)) + scalingFactor_ * (population_.col(randomIndices(1)) - population_.col(randomIndices(2)));
                }

                return populationCandidates;
            },
            "Differential evolution"
        }
    });

    setScalingFactor(0.5);
}
static OSStatus MakeImageDocument(CFURLRef url, CFStringRef imageType, const ExportInfo *exportInfo)
{
    OSStatus err = noErr;
    CGContextRef c = NULL;
    CGImageRef image;
    Boolean useDisplayColorSpace;
    // First make a bitmap context for a US Letter size
    // raster at the requested resolution. 
    int dpi = exportInfo->dpi;
    size_t width = (size_t)(8.5*dpi), height = (size_t)11*dpi;

    // For JPEG output type the bitmap should not be transparent. If other types are added that
	// do not support transparency, this code should be updated to check for those types as well.
    Boolean needTransparentBitmap = 
	    !(CFStringCompare(imageType, kUTTypeJPEG, kCFCompareCaseInsensitive) == kCFCompareEqualTo);

    // Create an RGB Bitmap context using the generic calibrated RGB color space
    // instead of the display color space.
    useDisplayColorSpace = false;
    c = createRGBBitmapContext(width, height, useDisplayColorSpace, 
						    needTransparentBitmap);
    
    if(c == NULL){
		fprintf(stderr, "Couldn't make destination bitmap context!\n");
		// Users of this code should update this to be an error code they find useful.
		return memFullErr;
    }
      
    // Scale the coordinate system based on the resolution in dots per inch.
    CGContextScaleCTM(c, dpi/72, dpi/72);
    
    // Set the font smoothing parameter to false since it's better to
    // draw any text without special LCD text rendering when creating
    // rendered data for export.
    if(CGContextSetShouldSmoothFonts != NULL)
		CGContextSetShouldSmoothFonts(c, false);

    // Set the scaling factor for shadows. This is a hack so that
    // drawing code that needs to know the scaling factor can
    // obtain it. Better would be that DispatchDrawing and the code
    // it calls would take this scaling factor as a parameter.
    setScalingFactor(dpi/72);
    
    // Draw into that raster...
    DispatchDrawing(c, exportInfo->command);
    
    // Set the scaling factor back to 1.0.
    setScalingFactor(1.0);
    
    // Create an image from the raster data. Calling
	// createImageFromBitmapContext gives up ownership
	// of the raster data used by the context.
    image = createImageFromBitmapContext(c);

	// Release the context now that the image is created.
    CGContextRelease(c);
	c = NULL;

    if(image == NULL){
		// Users of this code should update this to be an error code they find useful.
		return memFullErr;  
    }

    // Now export the image.
    if(exportInfo->useQTForExport)
		exportCGImageToFileWithQT(image, url, imageType, exportInfo->dpi);
    else
		exportCGImageToFileWithDestination(image, url, imageType, exportInfo->dpi);
    
    CGImageRelease(image);
    return err;
}