slightly ok pruning based on max score of other branches of DFS and

modifying depth of branch by pieces taken
This commit is contained in:
Dan Ballard 2011-09-12 22:51:43 -07:00
parent 78afdda3f0
commit da4ca314b8
1 changed files with 53 additions and 29 deletions

View File

@ -234,7 +234,9 @@ class Board
return boards return boards
end end
def score(team) def score()
scores = {}
[TEAM_1, TEAM_2].each {|team|
score = 0 score = 0
score += @stats[team]['count'] score += @stats[team]['count']
score += 12 - @stats[opposite_team(team)]['count'] score += 12 - @stats[opposite_team(team)]['count']
@ -243,60 +245,82 @@ class Board
elsif @stats[team]['count'] == 0 elsif @stats[team]['count'] == 0
score -= 12 score -= 12
end end
return score scores[team] = score
}
return scores
end end
def search(team) def search(team)
search_do(team, 0, 10, 100.0, 0.0, "R", {TEAM_1 => -100, TEAM_2 => -100}) search_do(team, team, 0, 6, 100.0, 0.0, @stats.dup, [])
end end
# the owner of the search: affects pruning bonuses
# team - current player this turn # team - current player this turn
# current depth - counting up to max_depth # current depth - counting up to max_depth
# max_depth - when we should stop our search # max_depth - when we should stop our search
# my_percent - the percent range this work covers # my_percent - the percent range this work covers
# done_percent - the percent of work already done # done_percent - the percent of work already done
# maxs - # stats - stats from 2 turns ago, ie before most recent opponent turn
def search_do(team, current_depth, max_depth, my_percent, done_percent, maxs) # maxs - depth indexed array of max scores
#puts location def search_do(owner, team, current_depth, max_depth, my_percent, done_percent, stats, maxs)
if depth == 0 #puts current_depth.to_s + '/' + max_depth.to_s + ' ' + done_percent.to_s + "%"
return score(team)
if current_depth < maxs.length and score()[team] < maxs[current_depth][team]
return score()
end end
if current_depth >= maxs.length or score()[team] > maxs[current_depth][team]
maxs[current_depth] = score()
end
if current_depth >= max_depth
return score()
end
if @stats[TEAM_1]['count'] == 0 if @stats[TEAM_1]['count'] == 0
puts "TEAM_2 WON!" puts "TEAM_2 WON!"
return score(team) return score()
elsif @stats[TEAM_2]['count'] == 0 elsif @stats[TEAM_2]['count'] == 0
puts "TEAM_1 WON!" puts "TEAM_1 WON!"
return score(team) puts to_s
return score()
else else
moves = gen_next_move_boards(team) moves = gen_next_move_boards(team)
if moves == [] if moves == []
puts "NO MOVES AVAILABLE?" puts "NO MOVES AVAILABLE?"
return score(team) return score()
else else
jumps = moves.find_all { |item| item['move'][0].is_a?(Array)} jumps = moves.find_all { |item| item['move'][0].is_a?(Array)}
if jumps.length > 0 if jumps.length > 0
moves = jumps moves = jumps
end end
done = 0.0 done = 0.0
item_percent = percent/moves.length item_percent = my_percent/moves.length
max = nil
for i in 0..moves.length-1 do for i in 0..moves.length-1 do
#moves.each { |move| depth_mod = 0
move = moves[i]['board'] move = moves[i]['board']
#puts "SEARCH" # modify depth on piece loss
sub_score = move.search_do(opposite_team(team), depth -1, item_percent, last_percent+done, location + "." +i.to_s, maxs.dup ) if stats[team]['count'] > move.stats[team]['count']
# search less is search owner lost peices
if owner == team
depth_mod = -4
else
depth_mod = 4
end
end
sub_score = move.search_do(owner, opposite_team(team), current_depth + 1, max_depth + depth_mod, item_percent, done_percent+done, @stats.dup, maxs)
done += item_percent done += item_percent
if done >= 0.01 if done >= 0.01
last_percent += done; done_percent += done;
puts "%.5f" % last_percent + "% depth: " + depth.to_s + " max: " + maxs.to_s puts "%.2f" % done_percent + "% depth: " + current_depth.to_s + "/" + max_depth.to_s + " max: " + maxs.to_s
done = 0.0 done = 0.0
end end
#puts "Score: " + sub_score.to_s #puts "Score: " + sub_score.to_s
if sub_score > maxs[team] if max == nil or sub_score[team] > max[team]
maxs[team] = sub_score max = sub_score
end end
end end
return maxs[team] return max
end end
end end
end end