Question

I am trying to use AForge.Net Genetics library to create a simple application for optimization purposes. I have a scenario where I have four input parameters, therefore I tried to modify the "OptimizationFunction2D.cs" class located in the AForge.Genetic project to handle four parameters. While converting the binary chromosomes into 4 parameters (type = double) I am not sure if my approach is correct as I don't know how to verify the extracted values. Below is the code segment where my code differs from the original AForge code:

public double[] Translate( IChromosome chromosome )
    {
        // get chromosome's value
        ulong val = ((BinaryChromosome) chromosome).Value;
        // chromosome's length
        int length = ((BinaryChromosome) chromosome).Length;

        // length of W component
        int wLength = length/4;
        // length of X component
        int xLength = length / 4;
        // length of Y component
        int yLength = length / 4;
        // length of Z component
        int zLength = length / 4;

        // W maximum value - equal to X mask
        ulong wMax = 0xFFFFFFFFFFFFFFFF >> (64 - wLength);
        // X maximum value
        ulong xMax = 0xFFFFFFFFFFFFFFFF >> (64 - xLength);
        // Y maximum value - equal to X mask
        ulong yMax = 0xFFFFFFFFFFFFFFFF >> (64 - yLength);
        // Z maximum value
        ulong zMax = 0xFFFFFFFFFFFFFFFF >> (64 - zLength);

        // W component
        double wPart = val & wMax;
        // X component;
        double xPart = (val >> wLength) & xMax;
        // Y component;
        double yPart = (val >> (wLength + xLength) & yMax);
        // Z component;
        double zPart = val >> (wLength + xLength + yLength);

        // translate to optimization's function space
        double[] ret = new double[4];

        ret[0] = wPart * _rangeW.Length / wMax + _rangeW.Min;
        ret[1] = xPart * _rangeX.Length / xMax + _rangeX.Min;
        ret[2] = yPart * _rangeY.Length / yMax + _rangeY.Min;
        ret[3] = zPart * _rangeZ.Length / zMax + _rangeZ.Min;

        return ret;
    }

I am not sure if am correctly separating the chromosome value into four part (wPart/xPart/yPart/zPart). The original function in the AForge.Genetic library looks like this:

public double[] Translate( IChromosome chromosome )
    {
        // get chromosome's value
        ulong   val = ( (BinaryChromosome) chromosome ).Value;
        // chromosome's length
        int     length = ( (BinaryChromosome) chromosome ).Length;
        // length of X component
        int     xLength = length / 2;
        // length of Y component
        int     yLength = length - xLength;
        // X maximum value - equal to X mask
        ulong   xMax = 0xFFFFFFFFFFFFFFFF >> ( 64 - xLength );
        // Y maximum value
        ulong   yMax = 0xFFFFFFFFFFFFFFFF >> ( 64 - yLength );

        // X component
        double  xPart = val & xMax;
        // Y component;
        double  yPart = val >> xLength;

        // translate to optimization's function space
        double[] ret = new double[2];

        ret[0] = xPart * rangeX.Length / xMax + rangeX.Min;
        ret[1] = yPart * rangeY.Length / yMax + rangeY.Min;

        return ret;
    }

Can someone please confirm if my conversion process is correct or is there a better way of doing it.

Was it helpful?

Solution

No, it works but you don't need it to be so complicated.

ulong wMax = 0xFFFFFFFFFFFFFFFF >> (64 - wLength);

this returns the same value to all the results wMax xMax yMax zMax so just do one and call it componentMask

part = (val >> (wLength * pos) & componentMask);

where pos is the 0 based position of the component. so 0 for w, 1 for x ...

the rest is ok.

EDIT: if the Length is not divided by 4 you can make the last part be just val >> (wLength * pos) to make it have the remaining bits.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top