Skip to content

Commit

Permalink
Merge pull request #78 from IDSIA/dev
Browse files Browse the repository at this point in the history
Release 0.2.0
  • Loading branch information
cbonesana authored Jun 18, 2021
2 parents d552cdd + cd95cd3 commit 540d11f
Show file tree
Hide file tree
Showing 241 changed files with 9,054 additions and 5,724 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
0.2.0
===========

- New Factor hierarchy ( #74 ). Major changes are:
* All factors are now **interfaces**.
* For each `factor` we can have multiple implementations.
* `Factor`s are **immutable** now: the creation of a new factor is delegated to the `constructor` or to a dedicated `Factory` class.
* `SymbolicFactor`s can now be used to keep track of operations done by algorithms.
- Added code for isipta21benchamrk ( #76 #77 )
- Removed old methods marked as `@deprecated`

0.1.7.a
===========

- Fixed an issue with sampling of `BayesianFactors` #75

0.1.7
===========

Expand Down
74 changes: 39 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,49 @@ learning and inference algorithms for credal models.
An example of exact inference in a credal network is given below.

```java
import ch.idsia.crema.factor.credal.vertex.VertexFactor;
import ch.idsia.crema.inference.ve.CredalVariableElimination;
import ch.idsia.crema.core.ObservationBuilder;
import ch.idsia.crema.core.Strides;
import ch.idsia.crema.factor.credal.vertex.separate.VertexFactor;
import ch.idsia.crema.factor.credal.vertex.separate.VertexFactorFactory;
import ch.idsia.crema.inference.ve.CredalVariableElimination;
import ch.idsia.crema.model.graphical.DAGModel;
import ch.idsia.crema.model.graphical.GraphicalModel;

public class Starting {
public static void main(String[] args) {
double p = 0.2;
double eps = 0.0001;

/* CN defined with vertex Factor */

// Define the model (with vertex factors)
GraphicalModel<VertexFactor> model = new DAGModel<>();
int A = model.addVariable(3);
int B = model.addVariable(2);

model.addParent(B,A);

// Define a credal set of the partent node
VertexFactor fu = new VertexFactor(model.getDomain(A), Strides.empty());
fu.addVertex(new double[]{0., 1-p, p});
fu.addVertex(new double[]{1-p, 0., p});

model.setFactor(A,fu);

// Define the credal set of the child
VertexFactor fx = new VertexFactor(model.getDomain(B), model.getDomain(A));
fx.addVertex(new double[]{1., 0.,}, 0);
fx.addVertex(new double[]{1., 0.,}, 1);
fx.addVertex(new double[]{0., 1.,}, 2);

model.setFactor(B,fx);

// Run exact inference
CredalVariableElimination<VertexFactor> inf = new CredalVariableElimination<>(model);
inf.query(A, ObservationBuilder.observe(B,0));
}
public static void main(String[] args) {
double p = 0.2;
double eps = 0.0001;

/* CN defined with vertex Factor */

// Define the model (with vertex factors)
GraphicalModel<VertexFactor> model = new DAGModel<>();
int A = model.addVariable(3);
int B = model.addVariable(2);

model.addParent(B, A);

// Define a credal set of the partent node
VertexFactor fu = VertexFactorFactory.factory().domain(model.getDomain(A), Strides.empty())
.addVertex(new double[]{0., 1 - p, p})
.addVertex(new double[]{1 - p, 0., p})
.get();

model.setFactor(A, fu);

// Define the credal set of the child
VertexFactor fx = VertexFactorFactory.factory().domain(model.getDomain(B), model.getDomain(A))
.addVertex(new double[]{1., 0.,}, 0)
.addVertex(new double[]{1., 0.,}, 1)
.addVertex(new double[]{0., 1.,}, 2)
.get();

model.setFactor(B, fx);

// Run exact inference
CredalVariableElimination inf = new CredalVariableElimination();
inf.query(model, ObservationBuilder.observe(B, 0), A);
}
}
```

Expand All @@ -68,7 +72,7 @@ Add the following code in the pom.xml of your project:
<dependency>
<groupId>ch.idsia</groupId>
<artifactId>crema</artifactId>
<version>0.1.6</version>
<version>0.1.7</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
89 changes: 44 additions & 45 deletions examples/BNdefinition.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
package examples;

import ch.idsia.crema.factor.bayesian.BayesianDefaultFactor;
import ch.idsia.crema.factor.bayesian.BayesianFactor;
import ch.idsia.crema.model.graphical.BayesianNetwork;


public class BNdefinition {
public static void main(String[] args) {

BayesianNetwork model = new BayesianNetwork();
BayesianFactor[] f = new BayesianFactor[5];

// Winter?
int A = model.addVariable(2);
f[A] = new BayesianFactor(model.getDomain(A), new double[]{.6, .4}, false);

// Sprinkler?
int B = model.addVariable(2);
model.addParent(B, A);
f[B] = new BayesianFactor(model.getDomain(A, B), false);
f[B].setData(new int[]{B, A}, new double[]{.2, .8, .75, .25});

// Rain?
int C = model.addVariable(2);
model.addParent(C, A);
f[C] = new BayesianFactor(model.getDomain(A, C), false);
f[C].setData(new int[]{C, A}, new double[]{.8, .2, .1, .9});

// Wet Grass?
int D = model.addVariable(2);
model.addParent(D, B);
model.addParent(D, C);
f[D] = new BayesianFactor(model.getDomain(B, C, D), false);
f[D].setData(new int[]{D, B, C}, new double[]{.95, .05, .9, .1, .8, .2, 0, 1});

// Slippery Road?
int E = model.addVariable(2);
model.addParent(E, C);
f[E] = new BayesianFactor(model.getDomain(C, E), false);
f[E].setData(new int[]{E, C}, new double[]{.7, .3, 0, 1});

model.setFactors(f);

System.out.println(model);

for(int x: model.getVariables()){
for(int y: model.getChildren(x)){
System.out.print("("+x+"-->"+y+")");
}
}

}
public static void main(String[] args) {

BayesianNetwork model = new BayesianNetwork();
BayesianFactor[] f = new BayesianFactor[5];

// Winter?
int A = model.addVariable(2);
f[A] = new BayesianDefaultFactor(model.getDomain(A), new double[]{.6, .4});

// Sprinkler?
int B = model.addVariable(2);
model.addParent(B, A);
f[B] = new BayesianDefaultFactor(model.getDomain(A, B), new int[]{B, A}, new double[]{.2, .8, .75, .25});

// Rain?
int C = model.addVariable(2);
model.addParent(C, A);
f[C] = new BayesianDefaultFactor(model.getDomain(A, C), new int[]{C, A}, new double[]{.8, .2, .1, .9});

// Wet Grass?
int D = model.addVariable(2);
model.addParent(D, B);
model.addParent(D, C);
f[D] = new BayesianDefaultFactor(model.getDomain(B, C, D), new int[]{D, B, C}, new double[]{.95, .05, .9, .1, .8, .2, 0, 1});

// Slippery Road?
int E = model.addVariable(2);
model.addParent(E, C);
f[E] = new BayesianDefaultFactor(model.getDomain(C, E), new int[]{E, C}, new double[]{.7, .3, 0, 1});

model.setFactors(f);

System.out.println(model);

for (int x : model.getVariables()) {
for (int y : model.getChildren(x)) {
System.out.print("(" + x + "-->" + y + ")");
}
}

}
}
//54
66 changes: 37 additions & 29 deletions examples/BayesianFactors.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
package examples;

import ch.idsia.crema.core.Domain;
import ch.idsia.crema.core.DomainBuilder;
import ch.idsia.crema.factor.bayesian.BayesianDefaultFactor;
import ch.idsia.crema.factor.bayesian.BayesianFactor;
import ch.idsia.crema.factor.bayesian.BayesianFactorFactory;
import ch.idsia.crema.model.graphical.BayesianNetwork;


public class BayesianFactors {
public static void main(String[] args) {

// Create domain of 2 variables of sizes 2,3
Domain d1 = DomainBuilder.var(0, 1).size(2, 3);
public static void main(String[] args) {

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

// Some operations over a domain
d1.getVariables(); // vector of variables (int[])
d1.getSize(); // number of variables
d1.getCardinality(0); // size of a specific variable
d1.getSizeAt(0);

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

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

// Set the data (i.e., values) of the factor
// x1 x2
f.setData(new double[]{0.2, 0.8, //y1
0.5, 0.5, //y2
0.1, 0.9 //y3
});
// or using the dedicated Factory class:
f = BayesianFactorFactory.factory()
.domain(d1)
.data(new double[]{0.2, 0.8, 0.5, 0.5, 0.1, 0.9})
.get();

f.marginalize(0).getData(); // = double[3] { 1.0, 1.0, 1.0 }
f.marginalize(0).getData(); // = double[3] { 1.0, 1.0, 1.0 }


////// A factor can also be defined in the context of a specific model
////// A factor can also be defined in the context of a specific model


// Define the BN
BayesianNetwork model = new BayesianNetwork();
model.addVariables(2, 3);
model.addParent(0, 1);
// Define the BN
BayesianNetwork model = new BayesianNetwork();
model.addVariables(2, 3);
model.addParent(0, 1);

// extract a domain with varaiables 0 and 1 in the BN
Domain d2 = model.getDomain(0, 1);
// extract a domain with varaiables 0 and 1 in the BN
Domain d2 = model.getDomain(0, 1);

// Create an empty factor with that domain
f = new BayesianFactor(model.getDomain(0, 1));
// Create an empty factor with that domain
f = new BayesianDefaultFactor(model.getDomain(0, 1), null);


}
}
}
//53

Loading

0 comments on commit 540d11f

Please sign in to comment.