Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fixed_create_and_border #112

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions src/main/java/ch/idsia/crema/preprocess/creators/CreateCPT.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ch.idsia.crema.factor.credal.linear.interval.IntervalFactorFactory;
import ch.idsia.crema.utility.IndexIterator;

import java.util.ArrayList;
import java.util.Arrays;

public class CreateCPT {
Expand All @@ -19,21 +20,29 @@ public class CreateCPT {
*/
public double[] borders(double[] lower, double[] upper, Op operation) {

//shortcut in case the function is strictly monotone growing
//double[] extremes= new double[]{operation.execute(lower[0], upper[1]), operation.execute(upper[0], lower[1])};
//return Arrays.stream(extremes).sorted().toArray();
int DIM = (int) Math.pow(2, lower.length);
double[] results = new double[DIM];

double[] results = new double[2 * lower.length];
results[0] = operation.execute(lower[0], lower[1]);
results[1] = operation.execute(lower[0], upper[1]);
results[2] = operation.execute(upper[0], lower[1]);
results[3] = operation.execute(upper[0], upper[1]);

Arrays.sort(results);
double firstElement = results[0];
double lastElement = results[results.length - 1];
for (int i = 0; i < DIM; i++) {
StringBuilder binary = new StringBuilder(Integer.toBinaryString(i));
for(int j = binary.length(); j < lower.length; j++) {
binary.insert( 0, '0' );
}
ArrayList<Double> paramList = new ArrayList<>();

for(int j = 0; j < binary.length(); j++) {
if(binary.charAt(j) == '0') {
paramList.add(lower[j]);
} else {
paramList.add(upper[j]);
}
}
results[i] = operation.execute(paramList.stream().mapToDouble(Double::doubleValue).toArray());
}

return new double[]{firstElement, lastElement};
return new double[]{
Arrays.stream(results).min().isPresent()? Arrays.stream(results).min().getAsDouble(): 0.0,
Arrays.stream(results).max().isPresent()? Arrays.stream(results).max().getAsDouble(): 0.0};
}

/**
Expand All @@ -51,7 +60,7 @@ public IntervalFactor create(int childVar, int[] parentsVars, double[] childCuts

// root nodes creation
// add child node
int dimChild = childCuts.length + 1;
int dimChild = childCuts.length-1;
Strides stridesChild = Strides.var(childVar, dimChild);

// create domain
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/idsia/crema/preprocess/creators/Op.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

@FunctionalInterface
public interface Op {
double execute(double a, double b);
double execute(double... params);
}
20 changes: 10 additions & 10 deletions src/test/java/ch/idsia/crema/preprocess/creators/CreateCPTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@ public void testCreate() {
double[] cutsBMI = new double[]{0.0, 15.0, 16, 18.5, 25.0, 30.0, 35.0, 40.0, 100.0};

double[][] parents = new double[][]{cutsW, cutsH};
Op bmi = (w, h) -> w / h / h;
Op bmi = (double... num) -> num[0] / num[1] / num[1];

IntervalFactor cpt = creator.create(2, new int[]{0, 1}, cutsBMI, parents, bmi);
//System.out.println(cpt);

// w1 and h1
assertArrayEquals(cpt.getLower(0, 0), new double[10]);
assertArrayEquals(cpt.getUpper(0, 0), creator.createUpper(new int[]{4}, 10));
assertArrayEquals(cpt.getLower(0, 0), new double[8]);
assertArrayEquals(cpt.getUpper(0, 0), creator.createUpper(new int[]{4}, 8));

// w2 and h1
assertArrayEquals(cpt.getLower(1, 0), new double[10]);
assertArrayEquals(cpt.getUpper(1, 0), creator.createUpper(new int[]{4, 5}, 10));
assertArrayEquals(cpt.getLower(1, 0), new double[8]);
assertArrayEquals(cpt.getUpper(1, 0), creator.createUpper(new int[]{4, 5}, 8));

// w5 and h6
assertArrayEquals(cpt.getLower(4, 5), new double[10]);
assertArrayEquals(cpt.getUpper(4, 5), creator.createUpper(new int[]{4}, 10));
assertArrayEquals(cpt.getLower(4, 5), new double[8]);
assertArrayEquals(cpt.getUpper(4, 5), creator.createUpper(new int[]{4}, 8));

// w12 and h9
assertArrayEquals(cpt.getLower(11, 8), new double[10]);
assertArrayEquals(cpt.getUpper(11, 8), creator.createUpper(new int[]{5, 6}, 10));
assertArrayEquals(cpt.getLower(11, 8), new double[8]);
assertArrayEquals(cpt.getUpper(11, 8), creator.createUpper(new int[]{5, 6}, 8));
}

@Test
public void testBorders() {
double[] parentIntervalLower = new double[]{55.0, 1.55};
double[] parentIntervalUpper = new double[]{60.0, 1.60};
//example of the BMI
Op bmi = (w, h) -> w / h / h;
Op bmi = (double... num) -> num[0] / num[1] / num[1];

//bmi low = 21.48
//bmi high = 24.97
Expand Down
Loading