in reply to Exchange Heuristic and Swapping Code

Unless your problems have extra constraints, then refusing to swap more than one at a time will allow you to get stuck in a local minima, and never find the true solution.

For example, if you have a capacity/goal of 10, and your current solution is [6], with leftovers of [1,2,3,4] No single swap will improve your score, but trading your single value for all the leftovers would get you a perfect score.

In a similar way, [5,2] with [4,4] left over, and a max and goal of 8. Swapping the 5 for a 4 makes it worse. Swapping the 2 for a 4 puts you over the max. N-to-1 swaps and 1-to-N swaps also fail. The only way to get the optimal solution without making the intermediate solution worse is a 2-for-2 swap.

Replies are listed 'Best First'.
Re^2: Exchange Heuristic and Swapping Code
by nat47 (Sexton) on Mar 03, 2015 at 17:03 UTC
    @solution1,2,3,4,5

    @leftovers6,7,8

    pick [0] of @solution and examine combinations 6,2,3,4,5, 7,2,3,4,5, and 9,2,3,4,5. If these are not closer to goal state then do the same with 1 so that 1,6,3,4,5, 1,7,3,4,5, etc

    This makes sense then, that it could run through the whole thing and not find a better solution?