1 | package agents.similarity;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 |
|
---|
5 | import genius.core.Bid;
|
---|
6 | import genius.core.Domain;
|
---|
7 | import genius.core.xml.SimpleElement;
|
---|
8 |
|
---|
9 | public class Similarity {
|
---|
10 | private Domain fDomain;
|
---|
11 | private double fWeights[];
|
---|
12 | private ArrayList<SimilarityFunction> fSimilarityFunctions;
|
---|
13 |
|
---|
14 | public Similarity(Domain pDomain) {
|
---|
15 | fDomain = pDomain;
|
---|
16 | fSimilarityFunctions = new ArrayList<SimilarityFunction>();
|
---|
17 | }
|
---|
18 |
|
---|
19 | public final double getSimilarity(Bid pMyBid, Bid pOpponentBid) {
|
---|
20 | double lSimilarity = 0;
|
---|
21 | for (int i = 0; i < fSimilarityFunctions.size(); i++) {
|
---|
22 | lSimilarity += fWeights[i]
|
---|
23 | * fSimilarityFunctions.get(i).getSimilarityValue(pMyBid,
|
---|
24 | pOpponentBid);
|
---|
25 | }
|
---|
26 |
|
---|
27 | return lSimilarity;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public void loadFromXML(SimpleElement pRoot) {
|
---|
31 | SimpleElement lXMLUtilitySpace = (SimpleElement) (pRoot
|
---|
32 | .getChildByTagName("utility_space")[0]);
|
---|
33 | SimpleElement lXMLObjective = (SimpleElement) (lXMLUtilitySpace
|
---|
34 | .getChildByTagName("objective")[0]);
|
---|
35 | Object[] lXMLIssue = lXMLObjective.getChildByTagName("issue");
|
---|
36 | fWeights = new double[lXMLIssue.length];
|
---|
37 | for (int j = 0; j < lXMLIssue.length; j++) {
|
---|
38 | Object[] lXMLSimFn = ((SimpleElement) (lXMLIssue[j]))
|
---|
39 | .getChildByTagName("similarity_function");
|
---|
40 | if (lXMLSimFn == null || lXMLSimFn.length == 0) {
|
---|
41 | continue;
|
---|
42 | }
|
---|
43 | SimilarityFunction lSimFn = new SimilarityFunction(fDomain);
|
---|
44 | // load weights
|
---|
45 | fWeights[j] = Double.valueOf(((SimpleElement) (lXMLSimFn[0]))
|
---|
46 | .getAttribute("weight"));
|
---|
47 | lSimFn.loadFromXML((SimpleElement) (lXMLSimFn[0]), Integer
|
---|
48 | .valueOf(((SimpleElement) (lXMLIssue[j]))
|
---|
49 | .getAttribute("index")));
|
---|
50 | fSimilarityFunctions.add(lSimFn);
|
---|
51 | }// for
|
---|
52 | }
|
---|
53 | }
|
---|