Newer
Older
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
package io.gitlab.chaver.minimax.cli;
import com.google.gson.Gson;
import io.gitlab.chaver.minimax.io.Alternative;
import io.gitlab.chaver.minimax.io.IAlternative;
import io.gitlab.chaver.minimax.learn.oracle.LinearFunctionOracle;
import io.gitlab.chaver.minimax.learn.oracle.OWAOracle;
import io.gitlab.chaver.minimax.learn.oracle.ScoreFunctionOracle;
import io.gitlab.chaver.minimax.learn.train.AbstractRankingLearning;
import io.gitlab.chaver.minimax.learn.train.passive.KappalabRankLearn;
import io.gitlab.chaver.minimax.ranking.Ranking;
import io.gitlab.chaver.minimax.rules.io.RuleWithMeasures;
import io.gitlab.chaver.minimax.score.FunctionParameters;
import io.gitlab.chaver.minimax.score.IScoreFunction;
import io.gitlab.chaver.minimax.score.ScoreFunctionFactory;
import io.gitlab.chaver.minimax.util.RandomUtil;
import picocli.CommandLine;
import picocli.CommandLine.Option;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import static io.gitlab.chaver.minimax.learn.train.LearnUtil.*;
public class LearnFunctionAndRankCli implements Callable<Integer> {
@Option(names = "-d", description = "Path of the rules data", required = true)
private String dataPath;
@Option(names = "--tt", description = "Path of the training/test data", required = true)
private String trainingTestDataPath;
@Option(names = "-m", description = "Measures used in the function", required = true, split = ":")
private String[] measures;
@Option(names = "-o", description = "Name of the oracle", required = true)
private String oracleName;
@Option(names = "-l", description = "Learning algorithm", required = true)
private String learnAlgorithm;
@Option(names = "--seed", description = "Seed for random number generation")
private long seed = 2994274L;
@Option(names = "-r", description = "Path of the result files", required = true)
private String resPath;
private List<RuleWithMeasures> readRules(String path) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
Gson gson = new Gson();
List<RuleWithMeasures> rules = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
rules.add(gson.fromJson(line, RuleWithMeasures.class));
}
return rules;
}
}
private List<IAlternative> getAlternatives(List<RuleWithMeasures> rules) {
List<IAlternative> alternatives = new ArrayList<>();
for (RuleWithMeasures r : rules) {
double[] vector = Arrays
.stream(measures)
.mapToDouble(m -> r.getMeasureValues().get(m))
.toArray();
alternatives.add(new Alternative(vector));
}
return alternatives;
}
private Comparator<IAlternative> getOracle(double[] weights) {
if (oracleName.equals(OWAOracle.TYPE)) {
return new OWAOracle(weights);
}
if (oracleName.equals(LinearFunctionOracle.TYPE)) {
return new LinearFunctionOracle(weights);
}
throw new RuntimeException("Wrong oracle type: " + oracleName);
}
private AbstractRankingLearning getLearningAlgo(Ranking<IAlternative> expectedRanking) {
if (learnAlgorithm.equals("kappalab")) {
KappalabRankLearn kappalab = new KappalabRankLearn(expectedRanking);
return kappalab;
}
throw new RuntimeException("Wrong learning algorithm: " + learnAlgorithm);
}
@Override
public Integer call() throws Exception {
int nbTransactions = getNbTransactions(dataPath + "_prop.jsonl");
int nbMeasures = measures.length;
List<RuleWithMeasures> trainingRules = readRules(trainingTestDataPath + "_train.jsonl");
List<RuleWithMeasures> testRules = readRules(trainingTestDataPath + "_test.jsonl");
List<IAlternative> trainingAlternatives = getAlternatives(trainingRules);
List<IAlternative> testAlternatives = getAlternatives(testRules);
RandomUtil.getInstance().setSeed(seed);
double[] randomWeights = RandomUtil.getInstance().generateRandomWeights(nbMeasures);
Comparator<IAlternative> oracle = getOracle(randomWeights);
Ranking<IAlternative> expectedRanking = computeRankingWithOracle(oracle, trainingAlternatives);
AbstractRankingLearning algo = getLearningAlgo(expectedRanking);
FunctionParameters functionParameters = algo.learn();
IScoreFunction<IAlternative> func = ScoreFunctionFactory.getScoreFunction(functionParameters);
Ranking<IAlternative> actualTestAlternativesRanking = computeRankingWithOracle(new ScoreFunctionOracle(func), testAlternatives);
List<RuleWithMeasures> testRulesRankedWithLearnedFunc = Arrays
.stream(actualTestAlternativesRanking.getRanking())
.mapToObj(i -> testRules.get(i))
.collect(Collectors.toCollection(ArrayList::new));
return 0;
}
public static void main(String[] args) {
int exitCode = new CommandLine(new LearnFunctionAndRankCli()).execute(args);
System.exit(exitCode);
}
}