Find the right/best track combination for a given distance, using a genetic algorithm or ?. I have a list of tracks (model railroad tracks) with different length, example: TrackA on 3.0cm, TrackB on 5.0cm, TrackC on 6.5cm, TrackD on 10.5cm

Then I want to find out of what kind of track I should put together to get from point A to point B with a given distance and a margin. And I should also be able to a prioritizes the use of track type.

Example; Distance from point A to B is 1.7m, and I have lot of TrackC and few of TrackB. And I will allow a margin on +/- 0.5cm to the distance.

What kind of tracks should I use, and how many of each track, and how many combination do I have, sorted after the track where I have most of.

I have Google after some C# help using genetic algorithm, but I am lost in, how I can implement this in a good method.

Or just a mathematical method, that can solve my problem..

Please help..
You could easily formulate this as a integer linear programming (ILP) problem. This is a linear programming problem where the solutions are constrained to be integers. The problem is NP-hard as indicated in other answers, but software does exist to address this sort of problem.

Let $L$ be the ultimate length you are aiming for, $a$ be the number of
Track A used, $b$ be the number of Track B used, and so forth. Finally,
let $A$, $B$, $C$, and $D$ be the amount of each kind of track you have.

($a,b,c,d$ are variables; the rest are constants.)

Then the integer linear programming problem is formulated as:


$L - 0.5 \le 3.0 a + 5.0 b + 6.5 c + 10.5 d \le L + 0.5$
$0 \le a \le A$ 
$0 \le b \le B$ 
$0 \le c \le C$ 
$0 \le d \le D$ 
$a,b,c,d$ are integers
minimise $a+b+c+d$


The condition $a+b+c+d$ can be changed to what you want to minimise/maximise to suit your needs so long as 
it is linear in the variables $a,b,c,d$. The could be something capturing the costs of the various Tracks. All of the other ingredients can be changed in an obvious way, for different track lengths, different tolerance level, and so forth, so long as they are constants when you submit your question to an ILP system.

Plenty of software packages exist to solve such problems; many are listed on the page linked to above.