source: src/main/java/agents/anac/y2019/dandikagent/utils.java@ 316

Last change on this file since 316 was 201, checked in by Katsuhide Fujita, 5 years ago

Add ANAC 2019 agents (2)

  • Property svn:executable set to *
File size: 5.0 KB
Line 
1package agents.anac.y2019.dandikagent;
2
3import agents.org.apache.commons.math.stat.regression.OLSMultipleLinearRegression;
4import genius.core.Bid;
5import genius.core.Domain;
6import genius.core.issue.Issue;
7import genius.core.issue.IssueDiscrete;
8import genius.core.issue.Value;
9import genius.core.issue.ValueDiscrete;
10import java.util.*;
11
12public class utils {
13
14 public static List<List<String>> generateAllPossibleBids(List<List<String>> input, int i) {
15
16 if (i == input.size()) {
17 List<List<String>> result = new ArrayList<List<String>>();
18 result.add(new ArrayList<String>());
19 return result;
20 }
21
22 List<List<String>> result = new ArrayList<List<String>>();
23 List<List<String>> recursive = generateAllPossibleBids(input, i + 1); // recursive call
24
25 for (int j = 0; j < input.get(i).size(); j++) {
26 for (int k = 0; k < recursive.size(); k++) {
27 List<String> newList = new ArrayList<String>(recursive.get(k));
28 newList.add(input.get(i).get(j));
29 result.add(newList);
30 }
31 }
32 return result;
33 }
34
35 public static double predict(OLSMultipleLinearRegression regression, double[] x) {
36 if (regression == null) {
37 throw new IllegalArgumentException("regression must not be null.");
38 }
39 double[] beta = regression.estimateRegressionParameters();
40
41 // intercept at beta[0]
42 double prediction = beta[0];
43 for (int i = 1; i < beta.length; i++) {
44 prediction += beta[i] * x[i - 1];
45 }
46 //
47 return prediction;
48 }
49
50 public static double scale(double x, double lowerBound, double max) {
51 return ((x - lowerBound) / (max - lowerBound)) * (1 - lowerBound) + lowerBound;
52 }
53
54 public static int getIssueCount(List<List<ValueDiscrete>> allIssues) {
55 int countAll = 0;
56 ArrayList<String> allIssuesAsArray = new ArrayList<>();
57
58
59 for (int i = 0; i < allIssues.size(); i++) {
60 for (int j = 0; j < allIssues.get(i).size(); j++) {
61 allIssuesAsArray.add(allIssues.get(i).get(j).toString());
62 countAll++;
63 }
64 }
65 return countAll;
66 }
67
68 public static void getIssueDiscrete(List<Issue> issues, List<List<ValueDiscrete>> allIssues) {
69 for (Issue x : issues) {
70 IssueDiscrete is = (IssueDiscrete) x;
71 allIssues.add(is.getValues());
72 }
73 }
74
75 public static double[][] encodeBids(List<Bid> bidOrder, int countAll, List<List<ValueDiscrete>> allIssues) {
76 double[][] oneHotEncoded = new double[bidOrder.size()][countAll];
77 int count = 0;
78 for (int i = 0; i < oneHotEncoded.length; i++) {
79 for (int j = 0; j < oneHotEncoded[0].length; j++) {
80 for (int k = 0; k < bidOrder.get(i).getValues().values().size(); k++) {
81 for (int l = 0; l < allIssues.get(k).size(); l++) {
82 if (bidOrder.get(i).getValues().values().toArray()[k].toString().equals(allIssues.get(k).get(l).toString())) {
83 oneHotEncoded[i][count] = 1.0;
84 } else {
85 oneHotEncoded[i][count] = 0.0;
86 }
87 count++;
88 }
89 }
90 count = 0;
91 }
92 }
93 return oneHotEncoded;
94 }
95
96 public static Bid asBid(Domain domain, String[] asString) {
97 HashMap<Integer, Value> values = new HashMap();
98 for (int i = 1; i <= asString.length; i++) {
99 Value val = new ValueDiscrete(asString[i - 1]);
100 values.put(i, val);
101 }
102 return new Bid(domain, values);
103 }
104
105 public static List<String> bidToListOfString(Bid input) {
106 List<String> result = new ArrayList<>();
107
108 for(int i = 0;i<input.getValues().size();i++){
109 result.add(input.getValues().values().toArray()[i].toString());
110 }
111 return result;
112 }
113
114 public static double[][] encodeListOfStrings(List<List<String>> bidOrder, int countAll, List<List<ValueDiscrete>> allIssues) {
115 double[][] oneHotEncoded = new double[bidOrder.size()][countAll];
116 int count = 0;
117 for (int i = 0; i < oneHotEncoded.length; i++) {
118 for (int j = 0; j < oneHotEncoded[0].length; j++) {
119 for (int k = 0; k < bidOrder.get(i).size(); k++) {
120 for (int l = 0; l < allIssues.get(k).size(); l++) {
121 if (bidOrder.get(i).get(k).equals(allIssues.get(k).get(l).toString())) {
122 oneHotEncoded[i][count] = 1.0;
123 } else {
124 oneHotEncoded[i][count] = 0.0;
125 }
126 count++;
127 }
128 }
129 count = 0;
130 }
131 }
132 return oneHotEncoded;
133 }
134}
Note: See TracBrowser for help on using the repository browser.