source: src/main/java/agents/similarity/SimilarityFunction.java@ 127

Last change on this file since 127 was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 2.3 KB
Line 
1package agents.similarity;
2
3import java.util.ArrayList;
4
5import genius.core.Bid;
6import genius.core.Domain;
7import genius.core.xml.SimpleElement;
8
9public 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}
Note: See TracBrowser for help on using the repository browser.