-
Notifications
You must be signed in to change notification settings - Fork 0
/
Converter.java
175 lines (165 loc) · 5.08 KB
/
Converter.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
import java.io.File;
import java.util.ArrayList;
import gurobi.GRB;
import gurobi.GRBVar;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class Converter {
/**
* Converts given Excel sheet into two dimensional ArrayList
*
* @param address of file
* @param sheet to be converted
* @return ArrayList with equivalent content
*/
public static ArrayList<ArrayList<Double>> excelToArrayList(String address, int sheet, int firstRow, int firstCol) {
try {
File f = new File(address);
Workbook Wb = Workbook.getWorkbook(f);
Sheet sh = Wb.getSheet(sheet);
ArrayList<ArrayList<Double>> mathArray = new ArrayList<>();
int row = sh.getRows();
int col = sh.getColumns();
for (int i = firstRow; i < row; i++) {
ArrayList<Double> colArr = new ArrayList<>();
for (int j = firstCol; j < col; j++) {
Cell c = sh.getCell(j, i);
colArr.add(Double.parseDouble(c.getContents().replace(',', '.')));
}
mathArray.add(colArr);
}
return mathArray;
} catch (Exception e) {
System.out.println("Fehler: " + e.getMessage());
return null;
}
}
/**
* Converts two dimensional ArrayList into two dimensional array
*
* @param mathArray ArrayList to be converted
* @return Array with equivalent content
*/
public static double[][] arrayListToArray(ArrayList<ArrayList<Double>> mathArray) {
int rows = mathArray.size();
int cols = mathArray.get(0).size();
double[][] res = new double[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
res[i][j] = mathArray.get(i).get(j);
}
}
return res;
}
/**
* Converts two dimensional ArrayList into two dimensional array
*
* @param mathArray ArrayList to be converted
* @param rows number of of the result (should be larger than
* numberOfGroups)
* @param cols number of columns of the result (should be
* larger than each element of
* numberOfStudentsInGroup)
* @param numberOfGroups length of superlist
* @param numberOfStudentsInGroup length of each sublist
* @return Array with equivalent content
*/
public static double[][] arrayListToArray(ArrayList<ArrayList<Double>> mathArray, int rows, int cols,
int numberOfGroups, int[] numberOfStudentsInGroup) {
double[][] res = new double[rows][cols];
for (int j = 0; j < numberOfGroups; j++) {
for (int i = 0; i < numberOfStudentsInGroup[j]; i++) {
res[(int) Math.round(mathArray.get(j).get(i))][j] = 1;
}
}
return res;
}
/**
* Converts from continuous [-1,1] preferences to discrete {0,1} preferences
*
* @param res array to be normalized
* @return array with binary preferences
*/
public static double[][] binaryPreferences(double[][] inp) {
int rows = inp.length;
int cols = inp[0].length;
double[][] res = new double[rows][cols];
for (int i = 0; i < rows; i++) {
double maxInRow = 0;
int indexMaxInRow = -1;
for (int j = 0; j < cols; j++) {
if (inp[i][j] > maxInRow) {
maxInRow = inp[i][j];
indexMaxInRow = j;
}
}
for (int j = 0; j < cols; j++)
res[i][j] = (j == indexMaxInRow) ? 1 : 0;
}
return res;
}
/**
* Converts from inter-social x_ss variables to social-group x_sg variables
*
* @param x inter-social x_ss variables
* @param G max. number of groups
* @return social-group x_sg variables
*/
public static double[][] convertFromXtoXsg(GRBVar[][] x, int G) {
try {
int S = x.length;
double[][] y = new double[S][S];
for (int i = 0; i < S; i++) {
for (int j = i + 1; j < S; j++) {
y[i][j] = x[i][j].get(GRB.DoubleAttr.X);
}
}
int[] numberOfStudentsInGroup = new int[G];
int numberOfGroups = 0;
ArrayList<ArrayList<Double>> res = new ArrayList<>();
for (int i = 0; i < S; i++) {
if (y[i][i] < -1 / 2)
continue;
numberOfGroups++;
ArrayList<Double> rowArr = new ArrayList<>();
rowArr.add(i * 1.0);
numberOfStudentsInGroup[numberOfGroups - 1]++;
for (int j = i + 1; j < S; j++) {
if (y[i][j] > 1 / 2) {
rowArr.add(j * 1.0);
y[j][j] = -1;
numberOfStudentsInGroup[numberOfGroups - 1]++;
}
}
res.add(rowArr);
}
return arrayListToArray(res, S, G, numberOfGroups, numberOfStudentsInGroup);
} catch (Exception e) {
System.out.println("Fehler: " + e.getMessage());
return null;
}
}
/**
* Normalizes non-zero rows to L1-norm = 1
*
* @param res array to be normalized
* @return array with L1-normalized rows
*/
public static double[][] normalizePreferences(double[][] res) {
int rows = res.length;
int cols = res[0].length;
for (int i = 0; i < rows; i++) {
double rowSum = 0;
for (int j = 0; j < cols; j++) {
rowSum += Math.abs(res[i][j]);
}
if (rowSum != 0) {
for (int j = 0; j < cols; j++) {
res[i][j] = res[i][j] / rowSum;
}
}
}
return res;
}
}