Looping question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robocop

    Looping question

    Having just started using C again after some years off, i've been
    stumped by a problem i think someone more experienced could probably
    solve pretty easily. I have these 4 objects (vectors), and i want to
    find a combination of these 4 objects into two pairs, where the sum of
    a specific attribute of each vector in each pair comes closest to some
    base value. So all i want to do is run through the permutations of
    these 4 objects and find the combination that results in two pairs
    looking as close as possible to some base value.

    The only thing i could think of creating a temporary vector,
    initiating it by just dumping in one permutation, then looping over my
    vector with the 4 objects in it, doing different permutations,
    comparing those to the values in the temporary vector, and if this
    permutation is better, pushing back the current pair values into the
    temporary vector. After all the looping is done, i would just use the
    temporary vector as my best fits.

    Could anyone think of a possible solution that is simpler? I'm
    currently trying to implement this solution but it's not quite
    working. Any comments would be greatly appreciated.

  • Richard Heathfield

    #2
    Re: Looping question

    Robocop said:
    Having just started using C again after some years off, i've been
    stumped by a problem i think someone more experienced could probably
    solve pretty easily. I have these 4 objects (vectors), and i want to
    find a combination of these 4 objects into two pairs, where the sum of
    a specific attribute of each vector in each pair comes closest to some
    base value. So all i want to do is run through the permutations of
    these 4 objects and find the combination that results in two pairs
    looking as close as possible to some base value.
    >
    The only thing i could think of creating a temporary vector,
    initiating it by just dumping in one permutation, then looping over my
    vector with the 4 objects in it, doing different permutations,
    comparing those to the values in the temporary vector, and if this
    permutation is better, pushing back the current pair values into the
    temporary vector. After all the looping is done, i would just use the
    temporary vector as my best fits.
    >
    Could anyone think of a possible solution that is simpler? I'm
    currently trying to implement this solution but it's not quite
    working. Any comments would be greatly appreciated.

    If you absolutely must have a best fit, then brute force is the only way -
    try everything, see what works best. Sounds like you're already doing
    that.

    If you can get away with a "good enough for rock n' roll" fit, however,
    your options widen considerably. For example, you might want to consider a
    straight Monte Carlo, or shotgun hillclimbing, or a genetic algorithm.

    But you want simple, right? Well, I think that puts us right back to brute
    force again.

    If you have C code that isn't working, I suggest you throw out as much of
    the code as you can without affecting the brokenness that you're
    experiencing (but don't throw out error-checking, or we'll only tell you
    to put it back in!). Very often, performing this reduction exercise will
    allow you to find the bug yourself. If it doesn't, however, at least it
    will leave you with a program suitable for posting to comp.lang.c so that
    we can take a look and maybe spot the problem.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    Working...