IDSIA / crema

Crema: Credal Models Algorithms
https://crema-toolbox.readthedocs.io/
GNU Lesser General Public License v3.0
10 stars 4 forks source link

ApproxLP non stable results. #60

Open rcabanasdepaz opened 3 years ago

rcabanasdepaz commented 3 years ago

ApproxLP occasionally produces NaN results for queries that are solvable.

Consider this code:

        double eps = 0.000000001; // eps > 0. If set to 0.0, the inference will not work.

        DAGModel model = new DAGModel();
        int x = model.addVariable(2);
        int u = model.addVariable(3);
        model.addParent(x,u);

        BayesianFactor ifx = new BayesianFactor(model.getDomain(x,u));
        ifx.setData(new double[] {
                1., 0.,
                1., 0.,
                0., 1.,
        });
        model.setFactor(x, ifx);

        IntervalFactor ifu = new IntervalFactor(model.getDomain(u), model.getDomain());
        ifu.set(new double[] { 0, 0, 0.8-eps}, new double[] { 0.2, 0.2, 0.8 });
        model.setFactor(u, ifu);

        for(int i=0;i<100;i++) {
            ApproxLP2 inference = new ApproxLP2();
            double[] upper = inference.query(model, x).getUpper();
            System.out.println(Arrays.toString(upper));
        }

The out would be:


[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[NaN, NaN]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
[0.20000000099999993, 0.8]
...
``
rcabanasdepaz commented 3 years ago

With changes at a3beffb35cca85e7cefc206fcb386015899ee3d3, now the random seed can be specified having always the same behaviour. So no this is reproducible:

        double eps = 0.000000001; // eps > 0. If set to 0.0, the inference will not work.

        DAGModel model = new DAGModel();
        int x = model.addVariable(2);
        int u = model.addVariable(3);
        model.addParent(x,u);

        BayesianFactor ifx = new BayesianFactor(model.getDomain(x,u));
        ifx.setData(new double[] {
                1., 0.,
                1., 0.,
                0., 1.,
        });
        model.setFactor(x, ifx);

        IntervalFactor ifu = new IntervalFactor(model.getDomain(u), model.getDomain());
        ifu.set(new double[] { 0, 0, 0.8-eps}, new double[] { 0.2, 0.2, 0.8 });
        model.setFactor(u, ifu);

        ApproxLP2 inference;
        RandomUtil.setRandomSeed(0);
        inference = new ApproxLP2();
        double[] upper = inference.query(model, x).getUpper();  // works: [0.20000000099999993, 0.8]
        System.out.println(Arrays.toString(upper));

        RandomUtil.setRandomSeed(22);
        inference = new ApproxLP2();
        upper = inference.query(model, x).getUpper();   //NaN, NaN
        System.out.println(Arrays.toString(upper));

Any idea? @davidhuber

cbonesana commented 3 years ago

I noted something similar myself; with the same linear problem but different time and space, the solver return different results. No idea how to control that. Maybe is a know issue of the org.apache.commons.math3 library?