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 SimilarityFunction {
|
---|
10 |
|
---|
11 | private double fWeights[];
|
---|
12 | private ArrayList<Criteria> fCriteria;
|
---|
13 | private Domain fDomain;
|
---|
14 | private SIMILARITYTYPE fType;
|
---|
15 | private int fIssueIndex;
|
---|
16 |
|
---|
17 | public SimilarityFunction(Domain pDomain) {
|
---|
18 | fDomain = pDomain;
|
---|
19 | fCriteria = new ArrayList<Criteria>();
|
---|
20 | }
|
---|
21 |
|
---|
22 | public double getSimilarityValue(Bid pMyBid, Bid pOpponentBid) {
|
---|
23 | double lResult = 0;
|
---|
24 | switch (fType) {
|
---|
25 | case BINARY:
|
---|
26 | try {
|
---|
27 | if (pMyBid.getValue(fIssueIndex).equals(
|
---|
28 | pOpponentBid.getValue(fIssueIndex)))
|
---|
29 | lResult = 1;
|
---|
30 | else
|
---|
31 | lResult = 0;
|
---|
32 | } catch (Exception e) {
|
---|
33 | e.printStackTrace();
|
---|
34 | }
|
---|
35 | break;
|
---|
36 | case CUSTOM_DEFINED:
|
---|
37 | for (int i = 0; i < fCriteria.size(); i++) {
|
---|
38 | Criteria lCriteria = fCriteria.get(i);
|
---|
39 | lResult += fWeights[i]
|
---|
40 | * (1 - Math.abs(lCriteria.getValue(pMyBid)
|
---|
41 | - lCriteria.getValue(pOpponentBid)));
|
---|
42 | }
|
---|
43 | break;
|
---|
44 | } // switch
|
---|
45 | return lResult;
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void loadFromXML(SimpleElement pRoot, int pIssueIndex) {
|
---|
49 | fIssueIndex = pIssueIndex;
|
---|
50 | switch (SIMILARITYTYPE.convertToType(pRoot.getAttribute("type"))) {
|
---|
51 | case BINARY:
|
---|
52 | fType = SIMILARITYTYPE.BINARY;
|
---|
53 | break;
|
---|
54 | case CUSTOM_DEFINED:
|
---|
55 | fType = SIMILARITYTYPE.CUSTOM_DEFINED;
|
---|
56 | Object[] lXMLCriteriaFn = pRoot
|
---|
57 | .getChildByTagName("criteria_function");
|
---|
58 | fWeights = new double[lXMLCriteriaFn.length];
|
---|
59 | // read similarity functions
|
---|
60 | for (int i = 0; i < lXMLCriteriaFn.length; i++) {
|
---|
61 | // TODO: DT: finish loading from XML for CriteriaDiscrete
|
---|
62 | // load weights
|
---|
63 | fWeights[i] = Double
|
---|
64 | .valueOf(((SimpleElement) (lXMLCriteriaFn[i]))
|
---|
65 | .getAttribute("weight"));
|
---|
66 | Criteria lCriteria = null;
|
---|
67 | switch (fDomain.getObjectivesRoot().getObjective(pIssueIndex)
|
---|
68 | .getType()) {
|
---|
69 | case REAL:
|
---|
70 | lCriteria = new CriteriaReal(fDomain, pIssueIndex);
|
---|
71 | lCriteria.loadFromXML((SimpleElement) (lXMLCriteriaFn[i]));
|
---|
72 | fCriteria.add(lCriteria);
|
---|
73 | break;
|
---|
74 | case DISCRETE:
|
---|
75 | lCriteria = new CriteriaDiscrete(pIssueIndex);
|
---|
76 | lCriteria.loadFromXML((SimpleElement) (lXMLCriteriaFn[i]));
|
---|
77 | fCriteria.add(lCriteria);
|
---|
78 | break;
|
---|
79 | }// switch
|
---|
80 | }// for
|
---|
81 | break;
|
---|
82 | }// switch
|
---|
83 | }
|
---|
84 |
|
---|
85 | }
|
---|