/
LaguerreManager.java
126 lines (109 loc) · 3.89 KB
/
LaguerreManager.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
package laguerre;
import experiment.CallableImpl;
import experiment.ManagerImpl;
import experiment.Oracle;
import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
import org.apache.commons.math3.analysis.solvers.LaguerreSolver;
import perturbation.PerturbationEngine;
import perturbation.enactor.NCallEnactorImpl;
import perturbation.location.PerturbationLocation;
import perturbation.log.LoggerImpl;
import perturbation.perturbator.AddNPerturbatorImpl;
import perturbation.perturbator.InvPerturbatorImpl;
import java.util.ArrayList;
import java.util.List;
/**
* Created by bdanglot on 26/05/16.
*/
public class LaguerreManager extends ManagerImpl<double[], Double> {
private final double EPSILON = 10e-6;
private final int MaxEval = 1000;
private final double bound = 10.0;
public LaguerreManager(int nbtask, int size) {
this(nbtask, size, 23);
}
public LaguerreManager(int nbtask, int size, int seed) {
super(seed);
super.CUP = LaguerreSolver.class;
super.initialize(nbtask, size);
}
@Override
protected double[] generateOneTask() {
double[] task = new double[super.sizeOfTask];
for (int i = 0; i < super.sizeOfTask; i++)
task[i] = Math.floor(-5.0 + 10.0 * randomForGenTask.nextDouble());//TODO Should we set a bound : -5.0 <= coeff <= 5.0
return task;
}
@Override
public CallableImpl<double[], Double> getCallable(double[] input) {
return new CallableImpl<double[], Double>(input) {
@Override
public Double call() throws Exception {
PolynomialFunction f = new PolynomialFunction(input);
LaguerreSolver solver = new LaguerreSolver();
return solver.solve(MaxEval, f, -bound, bound);
}
};
}
@Override
public Oracle<double[], Double> getOracle() {
return (input, output) -> {
double assertion = 0.0;
for (int i = input.length - 1; i >= 0; i--)
assertion += input[i] * Math.pow(output, i);
return Math.floor(assertion) <= this.EPSILON;
};
}
@Override
public String getName() {
return "laguerre";
}
@Override
public String getHeader() {
return super.indexTasks.size() + " polynomial of " + super.sizeOfTask + " degrees\ngenerated with " + seedForGenTask + " as seed\n" +
super.locations.size() + " perturbation points\n";
}
@Override
public double[] getTask(int indexOfTask) {
if (indexOfTask >= super.tasks.size())
return super.getTask(indexOfTask);
double[] clone = new double[super.sizeOfTask];
System.arraycopy(super.tasks.get(indexOfTask), 0, clone, 0, super.sizeOfTask);
return clone;
}
public static void main(String[] args) throws Exception {
int nbTask = 25;
PerturbationLocation location = LaguerreSolver.__L73;
location = LaguerreSolver.__L0;
location.setPerturbator(new AddNPerturbatorImpl(1));
location.setPerturbator(new InvPerturbatorImpl());
LaguerreManager manager = new LaguerreManager(nbTask, 50);
PerturbationEngine.loggers.put("ExploreExample", new LoggerImpl());
for (int t = 0; t < nbTask; t++) {
List<Integer> failures = new ArrayList<>();
List<Integer> errors = new ArrayList<>();
PerturbationEngine.loggers.get("ExploreExample").logOn(location);
manager.getCallable(manager.getTask(t)).call();
int nbCall = PerturbationEngine.loggers.get("ExploreExample").getCalls(location);
PerturbationEngine.loggers.get("ExploreExample").remove(location);
for (int i = 0; i < nbCall; i++) {
location.setEnactor(new NCallEnactorImpl(i));
try {
Double output = (Double)manager.getCallable(manager.getTask(t)).call();
boolean assertion = manager.getOracle().assertPerturbation(manager.getTask(t), output);
if (!assertion)
failures.add(i);
} catch (Exception e) {
errors.add(i);
}
}
if (!failures.isEmpty() || !errors.isEmpty()) {
System.out.println("task " + t);
// System.out.println(failures.size());
System.out.println(failures + " / " + nbCall);
// System.out.println(errors.size());
// System.out.println(errors);
}
}
}
}