G
Guest
Hello everyone,
I am having some issues with what should be an insanely simple problem.
Basically, I'm working on an implementation of the TSP algorithm (I have a
static set of 5 locations, so I'm just brute forcing my way through it), and
during execution I build a list of 120 potential distances to be travelled,
stored in an array of type double. I then have another function which
traverses this array and locates the position of the minimum value. The
problem is that it doesn't work as it should. Below is my "find minimum"
algorithm:
for(int m=0; m<input.Length; m++)
{
if(input[m] < min)
{
min=input[m];
pos = m;
}
}
with input being the array of doubles. It's fairly obvious what should
happen in theory. In practice, however this causes issues. I generate the
paths to be used in my algorithm randomly, as i kind of precursor to a faster
algorithm I am thinking of implementing, so when I recalculate the route, the
position of the minimum could be different, while the actual value of the
minimum is still the same. However, the code occasionally returns a value
that is greater than the minimum! THe most frequent minimum value it
retreives is 81.4, but it will at times return 83.2, even if 81.4 is still in
the list. At first I thought that this was a problem with the
behind-the-scenes conversion of double-precision numbers, but I compared the
difference against an epsilon and still encountered the problem.
Does anyone have any ideas why this apparently straightforward algorithm is
so frustratingly broken?
Thanks for your time!
Matt Billock
I am having some issues with what should be an insanely simple problem.
Basically, I'm working on an implementation of the TSP algorithm (I have a
static set of 5 locations, so I'm just brute forcing my way through it), and
during execution I build a list of 120 potential distances to be travelled,
stored in an array of type double. I then have another function which
traverses this array and locates the position of the minimum value. The
problem is that it doesn't work as it should. Below is my "find minimum"
algorithm:
for(int m=0; m<input.Length; m++)
{
if(input[m] < min)
{
min=input[m];
pos = m;
}
}
with input being the array of doubles. It's fairly obvious what should
happen in theory. In practice, however this causes issues. I generate the
paths to be used in my algorithm randomly, as i kind of precursor to a faster
algorithm I am thinking of implementing, so when I recalculate the route, the
position of the minimum could be different, while the actual value of the
minimum is still the same. However, the code occasionally returns a value
that is greater than the minimum! THe most frequent minimum value it
retreives is 81.4, but it will at times return 83.2, even if 81.4 is still in
the list. At first I thought that this was a problem with the
behind-the-scenes conversion of double-precision numbers, but I compared the
difference against an epsilon and still encountered the problem.
Does anyone have any ideas why this apparently straightforward algorithm is
so frustratingly broken?
Thanks for your time!
Matt Billock