Hi David, The problem is because the goal inside minimize/2 (and other predicates where you give the goal to be executed as an argument) is not compiled, but is expanded and interpreted at run-time. This expansion treats the hash table variable TheHash, passed via param(TheHash), as a normal logical variable, i.e. it does not change while the loop is executed. Unfortunately, the hash table should be updated non-logically in each iteration, and this is not expanded correctly. The work-around is to move the loop, or the whole goal (as suggested by Joachim), out to an auxillary predicate. Here is an example of moving the loop: test_hash(Keys, Values) :- List = [1, 2, 3], minimize( ( X :: 1 .. 10, indomain(X), Y #= -X, hash_create(TheHash), loop(List, TheHash), hash_list(TheHash, Keys, Values), hash_count(TheHash, Count), printf("Hash contents after loop %w %w size %w\n", [Keys, Values, Count]), hash_list(TheHash, Keys, Values) ), Y ), printf( "X %w Y %w\n", [X, Y] ). loop(List, TheHash) :- ( foreach(Key, List), param(TheHash) do Val is 2 * Key, hash_set(TheHash, Key, Val), hash_list(TheHash, Keys, Values), hash_count(TheHash, Count), printf("Hash contents in loop %w %w size %w\n", [Keys, Values, Count]) ). In this case, loop/2 is compiled and behaves correctly: [eclipse 2]: test_hash(K,V). Hash contents in loop [1] [2] size 1 .... Found a solution with cost -10 Found no solution with cost -1.0Inf .. -11 X 10 Y -10 K = [1, 2, 3] V = [2, 4, 6] Yes (0.00s cpu) [eclipse 3]: Cheers, Kish David Norman wrote: > I can't seem to get a hash table to update when used inside a do loop > inside a minimize goal. The hash remains empty. I'm running version > 5.10 #77 on windows. > > Any ideas? > > Thanks, > David > > > Sample Code: > > :- lib(hash). > :- lib(ic). > :- lib(branch_and_bound). > test_hash(Keys, Values) :- > List = [1, 2, 3], > > minimize( > ( > X :: 1 .. 10, > indomain(X), > Y #= -X, > hash_create(TheHash), > ( > foreach(Key, List), > param(TheHash) > do > Val is 2 * Key, > hash_set(TheHash, Key, Val), > hash_list(TheHash, Keys, Values), > hash_count(TheHash, Count), > printf("Hash contents in loop %w %w size %w\n", [Keys, > Values, Count]) > ), > hash_list(TheHash, Keys, Values), > hash_count(TheHash, Count), > printf("Hash contents after loop %w %w size %w\n", [Keys, > Values, Count]), > > hash_list(TheHash, Keys, Values) > ), > Y ), > > printf( "X %w Y %w\n", [X, Y] ). > > > Execution: > > ?- test_hash(Keys, Values). > Keys = [] > Values = [] > Yes (0.02s cpu) > > > Output: > > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents after loop [] [] size 0 > Found a solution with cost -1 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents after loop [] [] size 0 > Found a solution with cost -2 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents after loop [] [] size 0 > Found a solution with cost -3 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents after loop [] [] size 0 > Found a solution with cost -4 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents after loop [] [] size 0 > Found a solution with cost -5 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents after loop [] [] size 0 > Found a solution with cost -6 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents after loop [] [] size 0 > Found a solution with cost -7 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents after loop [] [] size 0 > Found a solution with cost -8 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents after loop [] [] size 0 > Found a solution with cost -9 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents in loop [] [] size 0 > Hash contents after loop [] [] size 0 > Found a solution with cost -10 > Found no solution with cost -1.0Inf .. -11 > X 10 Y -10 > > > > ------------------------------------------------------------------------ > PC Magazine’s 2007 editors’ choice for best web mail—award-winning > Windows Live Hotmail. Check it out! > <http://imagine-windowslive.com/hotmail/?locale=en-us&ocid=TXT_TAGHM_migration_HMWL_mini_pcmag_0707> > > ------------------------------------------------------------------------ > > _______________________________________________ > ECLiPSe-Users mailing list > ECLiPSe-Users_at_...2... > http://www.crosscoreop.com/mailman/options/eclipse-users >Received on Thu Jul 12 2007 - 01:47:49 CEST
This archive was generated by hypermail 2.3.0 : Wed Sep 25 2024 - 15:13:20 CEST