/************************************************************************\ |* gtf is a framework for analyzing two-player zero-sum games *| |* Copyright (C) 2005 Troels Bjerre Sorensen *| |* *| |* This program is free software; you can redistribute it and/or modify *| |* it under the terms of the GNU General Public License as published by *| |* the Free Software Foundation; either version 2 of the License, or *| |* (at your option) any later version. *| |* *| |* This program is distributed in the hope that it will be useful, but *| |* WITHOUT ANY WARRANTY; without even the implied warranty of *| |* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *| |* General Public License for more details. *| |* *| |* You should have received a copy of the GNU General Public License *| |* along with this program; if not, write to the *| |* Free Software Foundation, Inc., 59 Temple Place - Suite 330, *| |* Boston, MA 02111-1307, USA. *| \************************************************************************/ package gtf; import gtf.util.*; import gtf.game.*; import java.io.*; import java.util.*; public class CounterStrategyVisitor> extends RealizationVisitor { Strategy counter, strategies; Map nodes = new HashMap(); Map vals = new HashMap(); Map parrent = new HashMap(); public CounterStrategyVisitor(Strategy strategies) { this.strategies = strategies; } public Strategy getStrategies() { return strategies; } public Strategy getCounterStrategies() { if (counter == null) calcCounterStrategies(); return counter; } public void visitImpPlayerNode(ImpPlayerNode node) { switch (node.getPlayer()) { case 1: nodes.put(p1know, node); for (GameTree child : node) parrent.put(p1know + child.getP1Learn(), p1real); break; case 2: nodes.put(p2know, node); for (GameTree child : node) parrent.put(p2know + child.getP2Learn(), p2real); break; } super.visitImpPlayerNode(node); } public void visitGameLeaf(GameLeaf leaf) { Rational value = leaf.getValue(); T weight = strategies.probOf(p2real).multiplyFrac(value).multiplyFrac(prob); T temp = vals.get(p1real); if (temp != null) weight = weight.add(temp); vals.put(p1real, weight); weight = strategies.probOf(p1real).multiplyFrac(value).multiplyFrac(prob).negate(); temp = vals.get(p2real); if (temp != null) weight = weight.add(temp); vals.put(p2real, weight); } private void calcCounterStrategies() { Map strat = new HashMap(); strat.put("p1", strategies.ONE); strat.put("p2", strategies.ONE); String[] keys = nodes.keySet().toArray(new String[0]); Arrays.sort(keys, new StringLengthComparator()); for (int i = keys.length - 1 ; i >= 0 ; i--) { ImpPlayerNode node = nodes.get(keys[i]); int player = node.getPlayer(); String bestchoice = null; T max = strategies.ONE.negInf(); for (GameTree child : node) { String childreal = keys[i] + child.getPlayerLearn(player); T temp = vals.get(childreal); if (max.less(temp)) { max = temp; bestchoice = childreal; } } strat.put(bestchoice, strategies.ONE); String parrentreal = parrent.get(bestchoice); T temp = vals.get(parrentreal); if (temp != null) max = max.add(temp); vals.put(parrent.get(bestchoice), max); } keys = strat.keySet().toArray(new String[0]); Arrays.sort(keys, new StringLengthComparator()); Strategy temp = new Strategy(strat); for (int i = 2 ; i < keys.length ; i++) { if (temp.probOf(parrent.get(keys[i])).isZero()) { strat.remove(keys[i]); } } counter = new Strategy(strat); } }