-
Notifications
You must be signed in to change notification settings - Fork 0
/
Party.java
184 lines (165 loc) · 3.59 KB
/
Party.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package negotiator.groupn;
import java.util.Collections;
import java.util.Vector;
import java.util.HashMap;
import negotiator.Bid;
import negotiator.issue.IssueDiscrete;
import negotiator.issue.ValueDiscrete;
import negotiator.utility.UtilitySpace;
public class Party {
String ID;
int counts[][];
double weights[];
public Vector<BidHolder> orderedBids;
Party(String id,int[][] issueVals)
{
this.ID =id;
counts = new int[issueVals.length][];
for(int i=0;i<issueVals.length;i++)
{
counts[i] = new int[issueVals[i].length];
}
weights = new double[issueVals.length];
this.orderedBids=new Vector<BidHolder>();
}
public double getPredictedUtility(Bid b,UtilitySpace us)
{
double total = 0;
for(int i=0;i<this.counts.length;i++)
{
total += getValueForIssue(b, us, i);
}
return total;
}
public double getValueForIssue(Bid b, UtilitySpace us,int i)
{
double eval =0;
try {
IssueDiscrete id = (IssueDiscrete)us.getIssue(i);
int choice = id.getValueIndex((ValueDiscrete)b.getValue(i+1));
eval = weights[i]*getVal(i, choice);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return eval;
}
public double getVal(int i,int j)
{
int ord=0;
for(int k =0;k<counts[i].length;k++)
{
if(counts[i][k]<=counts[i][j])
ord++;
}
double part = 1.0/counts[i].length;
return ord*part;
}
public void calcWeights()
{
calcWeightsWithGini();
//calcWeightsWithEntropy();
}
public void calcWeightsWithGini()
{
double sumweight =0;
for(int i =0;i<weights.length;i++)
{
this.weights[i] = weigthWithGini(i);
sumweight+=this.weights[i];
}
for(int i =0;i<weights.length;i++)
{
this.weights[i] /= sumweight;
}
}
public void calcWeightsWithEntropy()
{
double sumweight =0;
double maxent = 0;
for(int i =0;i<weights.length;i++)
{
this.weights[i] = weigthWithEntropy(i);
if(this.weights[i]>maxent)
maxent=this.weights[i];
}
for(int i =0;i<weights.length;i++)
{
if(maxent==0)
this.weights[i]=1;
else
this.weights[i]=maxent/this.weights[i];
sumweight+=this.weights[i];
}
for(int i =0;i<weights.length;i++)
{
this.weights[i] /= sumweight;
}
}
public double getWeight(int ind)
{
return this.weigthWithGini(ind);
}
private double weigthWithGini(int ind)
{
double w = 0;
int sum =0;
for(int i =0;i<this.counts[ind].length;i++)
{
sum+=this.counts[ind][i];
double r = 1.0*this.counts[ind][i]*this.counts[ind][i];
w+=r;
}
return w/(sum*sum);
}
private double weigthWithEntropy(int ind)
{
double w = 0;
int sum =0;
for(int i =0;i<this.counts[ind].length;i++)
{
sum+=this.counts[ind][i];
}
for(int i =0;i<this.counts[ind].length;i++)
{
if(this.counts[ind][i]==0)
continue;
double p = 1.0*this.counts[ind][i]/sum;
w+= - p*Math.log(p);
}
return w/(sum*sum);
}
@Override
public boolean equals(Object obj) {
return this.ID.equals(((Party) obj).ID);
}
public void show()
{
for(int i =0;i<this.counts.length;i++)
{
System.out.print(this.weights[i]+" ");
for(int j =0;j<this.counts[i].length;j++)
{
System.out.print(this.counts[i][j]+" ");
}
System.out.println();
}
}
public void orderBids(UtilitySpace us)
{
for(BidHolder b : orderedBids)
{
b.v = this.getPredictedUtility(b.b, us);
}
Collections.sort(orderedBids);
}
public void setOrderedBids(Vector<BidHolder> acceptableBids,UtilitySpace us) {
for(BidHolder bh :acceptableBids)
{
BidHolder bh1 = new BidHolder();
bh1.b=bh.b;
bh1.v = this.getPredictedUtility(bh.b, us);
this.orderedBids.add(bh1);
}
}
}