Skip to content

Commit 540d11f

Browse files
authored
Merge pull request #78 from IDSIA/dev
Release 0.2.0
2 parents d552cdd + cd95cd3 commit 540d11f

241 files changed

Lines changed: 9054 additions & 5724 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
0.2.0
2+
===========
3+
4+
- New Factor hierarchy ( #74 ). Major changes are:
5+
* All factors are now **interfaces**.
6+
* For each `factor` we can have multiple implementations.
7+
* `Factor`s are **immutable** now: the creation of a new factor is delegated to the `constructor` or to a dedicated `Factory` class.
8+
* `SymbolicFactor`s can now be used to keep track of operations done by algorithms.
9+
- Added code for isipta21benchamrk ( #76 #77 )
10+
- Removed old methods marked as `@deprecated`
11+
12+
0.1.7.a
13+
===========
14+
15+
- Fixed an issue with sampling of `BayesianFactors` #75
16+
117
0.1.7
218
===========
319

README.md

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,49 @@ learning and inference algorithms for credal models.
1010
An example of exact inference in a credal network is given below.
1111

1212
```java
13-
import ch.idsia.crema.factor.credal.vertex.VertexFactor;
14-
import ch.idsia.crema.inference.ve.CredalVariableElimination;
1513
import ch.idsia.crema.core.ObservationBuilder;
1614
import ch.idsia.crema.core.Strides;
15+
import ch.idsia.crema.factor.credal.vertex.separate.VertexFactor;
16+
import ch.idsia.crema.factor.credal.vertex.separate.VertexFactorFactory;
17+
import ch.idsia.crema.inference.ve.CredalVariableElimination;
18+
import ch.idsia.crema.model.graphical.DAGModel;
1719
import ch.idsia.crema.model.graphical.GraphicalModel;
1820

1921
public class Starting {
20-
public static void main(String[] args) {
21-
double p = 0.2;
22-
double eps = 0.0001;
23-
24-
/* CN defined with vertex Factor */
25-
26-
// Define the model (with vertex factors)
27-
GraphicalModel<VertexFactor> model = new DAGModel<>();
28-
int A = model.addVariable(3);
29-
int B = model.addVariable(2);
30-
31-
model.addParent(B,A);
32-
33-
// Define a credal set of the partent node
34-
VertexFactor fu = new VertexFactor(model.getDomain(A), Strides.empty());
35-
fu.addVertex(new double[]{0., 1-p, p});
36-
fu.addVertex(new double[]{1-p, 0., p});
37-
38-
model.setFactor(A,fu);
39-
40-
// Define the credal set of the child
41-
VertexFactor fx = new VertexFactor(model.getDomain(B), model.getDomain(A));
42-
fx.addVertex(new double[]{1., 0.,}, 0);
43-
fx.addVertex(new double[]{1., 0.,}, 1);
44-
fx.addVertex(new double[]{0., 1.,}, 2);
45-
46-
model.setFactor(B,fx);
47-
48-
// Run exact inference
49-
CredalVariableElimination<VertexFactor> inf = new CredalVariableElimination<>(model);
50-
inf.query(A, ObservationBuilder.observe(B,0));
51-
}
22+
public static void main(String[] args) {
23+
double p = 0.2;
24+
double eps = 0.0001;
25+
26+
/* CN defined with vertex Factor */
27+
28+
// Define the model (with vertex factors)
29+
GraphicalModel<VertexFactor> model = new DAGModel<>();
30+
int A = model.addVariable(3);
31+
int B = model.addVariable(2);
32+
33+
model.addParent(B, A);
34+
35+
// Define a credal set of the partent node
36+
VertexFactor fu = VertexFactorFactory.factory().domain(model.getDomain(A), Strides.empty())
37+
.addVertex(new double[]{0., 1 - p, p})
38+
.addVertex(new double[]{1 - p, 0., p})
39+
.get();
40+
41+
model.setFactor(A, fu);
42+
43+
// Define the credal set of the child
44+
VertexFactor fx = VertexFactorFactory.factory().domain(model.getDomain(B), model.getDomain(A))
45+
.addVertex(new double[]{1., 0.,}, 0)
46+
.addVertex(new double[]{1., 0.,}, 1)
47+
.addVertex(new double[]{0., 1.,}, 2)
48+
.get();
49+
50+
model.setFactor(B, fx);
51+
52+
// Run exact inference
53+
CredalVariableElimination inf = new CredalVariableElimination();
54+
inf.query(model, ObservationBuilder.observe(B, 0), A);
55+
}
5256
}
5357
```
5458

@@ -68,7 +72,7 @@ Add the following code in the pom.xml of your project:
6872
<dependency>
6973
<groupId>ch.idsia</groupId>
7074
<artifactId>crema</artifactId>
71-
<version>0.1.6</version>
75+
<version>0.1.7</version>
7276
<scope>compile</scope>
7377
</dependency>
7478
</dependencies>

examples/BNdefinition.java

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,51 @@
1+
package examples;
2+
3+
import ch.idsia.crema.factor.bayesian.BayesianDefaultFactor;
14
import ch.idsia.crema.factor.bayesian.BayesianFactor;
25
import ch.idsia.crema.model.graphical.BayesianNetwork;
36

47

58
public class BNdefinition {
6-
public static void main(String[] args) {
7-
8-
BayesianNetwork model = new BayesianNetwork();
9-
BayesianFactor[] f = new BayesianFactor[5];
10-
11-
// Winter?
12-
int A = model.addVariable(2);
13-
f[A] = new BayesianFactor(model.getDomain(A), new double[]{.6, .4}, false);
14-
15-
// Sprinkler?
16-
int B = model.addVariable(2);
17-
model.addParent(B, A);
18-
f[B] = new BayesianFactor(model.getDomain(A, B), false);
19-
f[B].setData(new int[]{B, A}, new double[]{.2, .8, .75, .25});
20-
21-
// Rain?
22-
int C = model.addVariable(2);
23-
model.addParent(C, A);
24-
f[C] = new BayesianFactor(model.getDomain(A, C), false);
25-
f[C].setData(new int[]{C, A}, new double[]{.8, .2, .1, .9});
26-
27-
// Wet Grass?
28-
int D = model.addVariable(2);
29-
model.addParent(D, B);
30-
model.addParent(D, C);
31-
f[D] = new BayesianFactor(model.getDomain(B, C, D), false);
32-
f[D].setData(new int[]{D, B, C}, new double[]{.95, .05, .9, .1, .8, .2, 0, 1});
33-
34-
// Slippery Road?
35-
int E = model.addVariable(2);
36-
model.addParent(E, C);
37-
f[E] = new BayesianFactor(model.getDomain(C, E), false);
38-
f[E].setData(new int[]{E, C}, new double[]{.7, .3, 0, 1});
39-
40-
model.setFactors(f);
41-
42-
System.out.println(model);
43-
44-
for(int x: model.getVariables()){
45-
for(int y: model.getChildren(x)){
46-
System.out.print("("+x+"-->"+y+")");
47-
}
48-
}
49-
50-
}
9+
public static void main(String[] args) {
10+
11+
BayesianNetwork model = new BayesianNetwork();
12+
BayesianFactor[] f = new BayesianFactor[5];
13+
14+
// Winter?
15+
int A = model.addVariable(2);
16+
f[A] = new BayesianDefaultFactor(model.getDomain(A), new double[]{.6, .4});
17+
18+
// Sprinkler?
19+
int B = model.addVariable(2);
20+
model.addParent(B, A);
21+
f[B] = new BayesianDefaultFactor(model.getDomain(A, B), new int[]{B, A}, new double[]{.2, .8, .75, .25});
22+
23+
// Rain?
24+
int C = model.addVariable(2);
25+
model.addParent(C, A);
26+
f[C] = new BayesianDefaultFactor(model.getDomain(A, C), new int[]{C, A}, new double[]{.8, .2, .1, .9});
27+
28+
// Wet Grass?
29+
int D = model.addVariable(2);
30+
model.addParent(D, B);
31+
model.addParent(D, C);
32+
f[D] = new BayesianDefaultFactor(model.getDomain(B, C, D), new int[]{D, B, C}, new double[]{.95, .05, .9, .1, .8, .2, 0, 1});
33+
34+
// Slippery Road?
35+
int E = model.addVariable(2);
36+
model.addParent(E, C);
37+
f[E] = new BayesianDefaultFactor(model.getDomain(C, E), new int[]{E, C}, new double[]{.7, .3, 0, 1});
38+
39+
model.setFactors(f);
40+
41+
System.out.println(model);
42+
43+
for (int x : model.getVariables()) {
44+
for (int y : model.getChildren(x)) {
45+
System.out.print("(" + x + "-->" + y + ")");
46+
}
47+
}
48+
49+
}
5150
}
5251
//54

examples/BayesianFactors.java

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,61 @@
1+
package examples;
2+
13
import ch.idsia.crema.core.Domain;
24
import ch.idsia.crema.core.DomainBuilder;
5+
import ch.idsia.crema.factor.bayesian.BayesianDefaultFactor;
36
import ch.idsia.crema.factor.bayesian.BayesianFactor;
7+
import ch.idsia.crema.factor.bayesian.BayesianFactorFactory;
48
import ch.idsia.crema.model.graphical.BayesianNetwork;
59

610

711
public class BayesianFactors {
8-
public static void main(String[] args) {
9-
10-
// Create domain of 2 variables of sizes 2,3
11-
Domain d1 = DomainBuilder.var(0, 1).size(2, 3);
12+
public static void main(String[] args) {
1213

13-
// Some operations over a domain
14-
d1.getVariables(); // vector of variables (int[])
15-
d1.getSize(); // number of variables
16-
d1.getCardinality(0); // size of a specific variable
17-
d1.getSizeAt(0);
14+
// Create domain of 2 variables of sizes 2,3
15+
Domain d1 = DomainBuilder.var(0, 1).size(2, 3);
1816

17+
// Some operations over a domain
18+
d1.getVariables(); // vector of variables (int[])
19+
d1.getSize(); // number of variables
20+
d1.getCardinality(0); // size of a specific variable
21+
d1.getSizeAt(0);
1922

20-
// Crate a factor over the domain
21-
BayesianFactor f = new BayesianFactor(d1); // P([0, 1]) -> P([X|Y])
2223

23-
f.getDomain();
24+
// Crate a factor over the domain and set the data (i.e., values) of the factor
25+
BayesianFactor f = new BayesianDefaultFactor(d1, // P([0, 1]) -> P([X|Y])
26+
new double[]{
27+
// x1 x2
28+
0.2, 0.8, // y1
29+
0.5, 0.5, // y2
30+
0.1, 0.9 // y3
31+
});
32+
f.getDomain();
2433

25-
// Set the data (i.e., values) of the factor
26-
// x1 x2
27-
f.setData(new double[]{0.2, 0.8, //y1
28-
0.5, 0.5, //y2
29-
0.1, 0.9 //y3
30-
});
34+
// or using the dedicated Factory class:
35+
f = BayesianFactorFactory.factory()
36+
.domain(d1)
37+
.data(new double[]{0.2, 0.8, 0.5, 0.5, 0.1, 0.9})
38+
.get();
3139

32-
f.marginalize(0).getData(); // = double[3] { 1.0, 1.0, 1.0 }
40+
f.marginalize(0).getData(); // = double[3] { 1.0, 1.0, 1.0 }
3341

3442

35-
////// A factor can also be defined in the context of a specific model
43+
////// A factor can also be defined in the context of a specific model
3644

3745

38-
// Define the BN
39-
BayesianNetwork model = new BayesianNetwork();
40-
model.addVariables(2, 3);
41-
model.addParent(0, 1);
46+
// Define the BN
47+
BayesianNetwork model = new BayesianNetwork();
48+
model.addVariables(2, 3);
49+
model.addParent(0, 1);
4250

43-
// extract a domain with varaiables 0 and 1 in the BN
44-
Domain d2 = model.getDomain(0, 1);
51+
// extract a domain with varaiables 0 and 1 in the BN
52+
Domain d2 = model.getDomain(0, 1);
4553

46-
// Create an empty factor with that domain
47-
f = new BayesianFactor(model.getDomain(0, 1));
54+
// Create an empty factor with that domain
55+
f = new BayesianDefaultFactor(model.getDomain(0, 1), null);
4856

4957

50-
}
58+
}
5159
}
5260
//53
5361

0 commit comments

Comments
 (0)