|
1 | 1 | package aima.core.probability; |
2 | 2 |
|
| 3 | +import aima.core.probability.bayes.BayesInference; |
3 | 4 | import aima.core.probability.bayes.BayesianNetwork; |
4 | 5 | import aima.core.probability.domain.FiniteDomain; |
| 6 | +import aima.core.probability.proposition.AssignmentProposition; |
5 | 7 | import org.junit.Test; |
6 | 8 |
|
| 9 | +import java.util.List; |
| 10 | + |
7 | 11 | public abstract class DecisionNetwork { |
8 | 12 |
|
9 | 13 | private BayesianNetwork network; |
10 | 14 | private RandomVariable action; |
| 15 | + private BayesInference inferenceProcedure; |
| 16 | + |
| 17 | + public DecisionNetwork(BayesianNetwork network, |
| 18 | + RandomVariable action, BayesInference inferenceProcedure) { |
| 19 | + this.network = network; |
| 20 | + this.action = action; |
| 21 | + this.inferenceProcedure = inferenceProcedure; |
| 22 | + } |
11 | 23 |
|
12 | 24 | public abstract double getUtilityForAction(RandomVariable action, Object value ); |
13 | 25 |
|
14 | | - public Object getBestAction(){ |
15 | | - double maxUtilValue = Double.MIN_VALUE; |
16 | | - Object actionValue = null; |
| 26 | + public double getExpectedUtility(RandomVariable action, |
| 27 | + List<AssignmentProposition> evidence){ |
| 28 | + double utility = 0; |
| 29 | + CategoricalDistribution distribution = inferenceProcedure.ask((new RandomVariable[]{action}), |
| 30 | + ((AssignmentProposition[])evidence.toArray()),this.getNetwork()); |
17 | 31 | for (Object value : |
18 | 32 | ((FiniteDomain) action.getDomain()).getPossibleValues()) { |
19 | | - if (maxUtilValue < getUtilityForAction(action,value)){ |
20 | | - maxUtilValue = getUtilityForAction(action,value); |
21 | | - actionValue = value; |
22 | | - } |
| 33 | + utility += distribution.getValue(value)*this.getUtilityForAction(action,value); |
23 | 34 | } |
24 | | - return actionValue; |
| 35 | + return utility; |
| 36 | + } |
| 37 | + |
| 38 | + public Object getBestAction(){ |
| 39 | + return action; |
25 | 40 | } |
26 | 41 |
|
27 | 42 | public BayesianNetwork getNetwork() { |
|
0 commit comments