func populationToSlice(pop *ga.Population, dest []cl.CL_uint) { destPtr := 0 length := pop.Length() numBlocks := blocksPerSolution(pop) for _, solution := range pop.Solutions { toCopy := length for j := 0; j < numBlocks; j++ { // Determine the number of bits to copy. blockSize := 32 if toCopy < 32 { blockSize = toCopy } toCopy -= blockSize // Copy the bits into 32-bit integer. var raw uint32 raw = 0 for k := 0; k < blockSize; k++ { index := j*32 + k if solution.Bits.Has(index) { raw |= (1 << uint32(k)) } } dest[destPtr] = cl.CL_uint(raw) destPtr++ } } }
func sliceToPopulation(src []cl.CL_uint, pop *ga.Population) bool { numBlocks := blocksPerSolution(pop) buffer := make([]uint32, numBlocks) foundOptimal := false for i, _ := range pop.Solutions { for j := 0; j < numBlocks; j++ { buffer[j] = uint32(src[i*numBlocks+j]) } pop.Solutions[i].Bits, _ = bitset.FromUInt32s(buffer, pop.Length()) fitness, optimal := problems[problemIndex].evaluator.Evaluate(pop.Solutions[i].Bits) pop.Solutions[i].Fitness = fitness if optimal { foundOptimal = true } } return foundOptimal }
func blocksPerSolution(pop *ga.Population) int { return ((pop.Length() - 1) >> 5) + 1 }