Network shared memory - NetMemSamples - Factorization | |||||||||
|
Genetic factorization
This sample proposes a genetic method of factorizing a large number.
The idea is to construct a gene (individual) pool on the server side and the clients can get individuals from the server, run a genetic algorithm and then write back the most succesfull individuals on the server.
The server part - serverDistribFactorization.cpp - allocates several pages: the first page contains a large integer N - this is the number to be factorized - and the rest of the pages contains each a pair of integer m, n that m*n should be equal to our number.
The client side - distribFactorization.cpp - connects to the server, get the number to be factorized and get a random population from the server and then perform a genetic algorithm on this population: combine and mutate the population, sort and keep the most succesfull pairs. After several generations, the client write back on the server the kept individuals. Then the client can initialize again the population with other individuals and proceed with the algorithm. The fitness function is very simple - ABS(N-m*n): the little this number is, the successfull is the individual. Large integer
The LargeInteger class can perform arithmetic on 32-base numbers - it can handle
only positive numbers. The number is represented as a 32-bit unsigned long
vector and the implementation contains overloading for almost all operators. In
this sample the number to be factorized is a 10 digits large integer (320 bits)
and the individual is represented by a pair of 5 digits numbers, so every page
of memory allocated on the server have 40 bytes. The gene pool contains 10000
individuals - all the constants are defined in distribFactorization.h.
|