c# - How to extract multiple parameters from a binary chromosome -


i trying use aforge.net genetics library create simple application optimization purposes. have scenario have 4 input parameters, therefore tried modify "optimizationfunction2d.cs" class located in aforge.genetic project handle 4 parameters. while converting binary chromosomes 4 parameters (type = double) not sure if approach correct don't know how verify extracted values. below code segment code differs original aforge code:

public double[] translate( ichromosome chromosome )     {         // 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 x mask         ulong wmax = 0xffffffffffffffff >> (64 - wlength);         // x maximum value         ulong xmax = 0xffffffffffffffff >> (64 - xlength);         // y maximum value - equal 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 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 not sure if correctly separating chromosome value 4 part (wpart/xpart/ypart/zpart). original function in aforge.genetic library looks this:

public double[] translate( ichromosome chromosome )     {         // 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 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 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 please confirm if conversion process correct or there better way of doing it.

no, works don't need complicated.

ulong wmax = 0xffffffffffffffff >> (64 - wlength); 

this returns same value results wmax xmax ymax zmax 1 , call componentmask

part = (val >> (wlength * pos) & componentmask); 

where pos 0 based position of component. 0 w, 1 x ...

the rest ok.

edit: if length not divided 4 can make last part val >> (wlength * pos) make have remaining bits.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -