Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programo Coding Cora Pérez Ariza ~ DECSAI ~ UGR Granada, January 28 th & 29 th, 2009.

Similar presentations


Presentation on theme: "Programo Coding Cora Pérez Ariza ~ DECSAI ~ UGR Granada, January 28 th & 29 th, 2009."— Presentation transcript:

1 Programo Coding Cora Pérez Ariza ~ DECSAI ~ UGR Granada, January 28 th & 29 th, 2009

2 Index Code ◦Potential package  Class Diagram  Potential Values  Potential Table: basic operations ◦Assignation package  Class Diagram  Factory Pattern 2

3 Code Potential Package: Class Diagram 3

4 Code Potential package design 4 Benefit of this design Allows us to implement generic propagation algorithms How to do it? Implement them in Potential class

5 Code Potential Package: Potential Values: Bridge Pattern 5 General Idea Decouple an abstraction from its implementation. How to do it? Define an interface with the operations the abstraction needs. Keep other methods private.

6 Code Potential Package: Potential Values: Bridge Pattern 6 Potential Values Different representations of potentials differ only in the structure their values are stored First approximation:

7 Code Potential Package: Potential Values: Inner Classes 7 Inner Class vs Nested Class Inner Class: an object of the inner class has a link to the enclosing object that made it. Nested Class (static inner class): there is no connection between the inner class object and the outerclass object. (similar to nested classes in C++) Benefits of Inner Classes Encloses Values concrete structure within its Potential representation. Provides full bidirectional communication between class and its inner class. Hides details about implementation (private inner class).

8 Code Potential Package: Potential Values: Potential Class Code 8 abstract public class Potential { protected VariableSet domainVariables; abstract public void normalizePotential(); abstract public Potential sumMarginalize(VariableSet vars); abstract public Potential combinePotential(Potential pot); abstract public Potential projectVariables(Assignation conf); abstract public double getValue(Assignation conf); abstract public void setValue(Assignation conf,double value); abstract public int valueDomainSize(); … public interface PotentialValues { public int size(); public void normalizePotentialValues(); public double getValue(Assignation conf); public void setValueByConfiguration(Assignation conf, double value); public Potential sumMarginalizeValues(VariableSet vars); public Potential combineValues(Potential pot); public Potential projectValues(Assignation conf); public String toString(); }

9 Code Potential Package: Potential Values: PotentialTable Class Code 9 public abstract class CategoricalPotential extends Potential{ public boolean equalValues(CategoricalPotential pot){…} } public class PotentialTable extends CategoricalPotential { private PotentialValues domainValues; … private final class PotentialValues implements Potential.PotentialValues { private double arrayValues[]; public methods of interface Potential.PotentialValues; private methods for this implementation; }

10 Code Potential Package: Potential Values: Creating a new representation 10 public class PotentialTree extends CategoricalPotential { private PotentialValues domainValues; … private final class PotentialValues implements Potential.PotentialValues { private nodeTree tree; public methods of interface Potential.PotentialValues; private methods for this implementation; }

11 Code Potential package: Potential Table operations 11 Reference article Operation with potentials of discrete variables M.Arias, F.J. Díez International Journal of Approximate Reasoning 46(2007) 166-187 General Idea In case of large potentials, the cost of retrieving their elements is significantly higher than the cost of multiplying, maximizing or summing them

12 Code Potential package: Potential Table operations 12 Keystone: A method for retrieving the elements of a potential in an arbitrary order but instead of multiplying the coordinates by the offsets, the position corresponding to each configuration is determined by adding an accumulated offset to the position of the previous configuration: pos x (next(y) x ) = pos x (y x ) + accOffset xy (varToIncr(y))

13 Code Potential package: Potential Table operations 13 Configurations For simplicity in the algorithms, in a configuration we increment the variables from left to right: ABC 000 100 010 110 001 101 011 111

14 Code Potential package: Potential Table operations: Marginalization 14 Marginalization over a set of variables We have a potential ψ X (x) where X=X e U X k and we want a marginalized potential ψ m X k (x k ) = ∑ X e ψ X (x e,x k ) Instead of creating a new potential with the variables to eliminate at the beginning, we retrieve the elements of ψ X in the correct order

15 Code Potential package: Potential Table operations: Marginalization 15 Pseudocode: Pos = 0 For(|X k * |){ For(|X e * |){ sum = value in ψ X in Pos Pos = next position in ψ X reordering variables } ψ m X k = sum calculate next position in ψ m X k }

16 Code Potential package: Potential Table operations: Projection 16 Restrict a potential to a configuration of variables We have a potential ψ X and a set of observed variables X o and we want to calculate ψ Xo X u To do so, we should work with a reordered potential that has the observed variables at the end, lets call it ψ y

17 Code Potential package: Potential Table operations: Projection 17 Pseudocode: pos = conf [X u = 0, X o ] in ψ X For(|X u * |){ ψ Xo X u = value in ψ X for pos calculate next position in ψ Xo X u pos = calculate next position in ψ x according to ψ y }

18 Code Potential package: Potential Table operations: Combination 18 Combine two potentials We have two potentials ψ X1 and ψ X2 and we want to compute ψ Y where Y = X 1 U X 2 ψ Y (y) = ψ X1 (y X1 ) x ψ X2 (y X2 )

19 Code Potential package: Potential Table operations: Combination 19 Pseudocode: PosX 1 = 0; PosX 2 = 0; For(|Y * |){ ψ Y = value of ψ X1 in PosX 1 * value of ψ X2 in PosX 2 PosX 1 = next position in ψ X1 according to ψ y PosX 2 = next position in ψ X2 according to ψ y calculate next position in ψ Y }

20 Code Assignation Package: Class Diagram 20

21 Code Assignation Package: Interfaces 21 public interface Assignation { public double getValue(Variable var); public double getValue(int indexvar); public Variable getVariable(int index); public void setValue(Variable var, double value); public void setValue(int indexvar, double value); public int indexOfVariable(Variable var); public int getSize(); public VariableSet getVariables(); public String toString(); }

22 Code Assignation Package: Interfaces 22 public interface CategoricalAssignation extends Assignation { public void nextConfiguration(); } public interface MixedAssignation extends Assignation { public void addVariable (Variable var, double value); public void removeVariable(Variable var); }

23 Code Assignation Package: Two kinds of Assignations 23 CategoricalAssignationMixedAssignation Only Categorical VariablesBoth Categorical and Continuous Variables Fixed set of variablesAllows addition/removal of variables

24 Code Assignation Package: Concrete Implementations 24 class IndexedCategoricalAssignation implements CategoricalAssignation { private Map mapVariableIndex; // For each variable return its position private Map mapIndexVariable; // For each index return its variable private int[] values; //Store the values … } class IndexedMixedAssignation implements MixedAssignation { private Map mapVariableValue; // For each variable return its value private ArrayList variables; //Store the order of the variables … }

25 Code Assignation Package: Efficience of Concrete implementations 25 Indexed CategoricalAssignation Indexed MixedAssignation getValue(Var)O(1) getValue(Index)O(1) getVariable(Index)O(1) setValue(Var, Val)O(1) setValue(Index, Val)O(1) indexOf(Var)O(1) O(n)

26 Code Assignation Package: Factory Pattern 26 Why a Factory? Concrete implementations should be hidden, that includes instantiating and transforming from one to another. How to use it? CategoricalAssignation assignation = AssignationFactory.createCategoricalAssignation(setOfVars, listOfValues);

27 Code Assignation Package: Factory Pattern: Factory Code 27 public class AssignationFactory{ public static MixedAssignation createMixedAssignation(VariableSet varset, List initialValues){…} public static CategoricalAssignation createCategoricalAssignation(VariableSet varset, List initialValues){…} public static MixedAssignation createMixedFromCategoricalAssignation(CategoricalAssignation oldassignation){…} public static CategoricalAssignation createCategoricalFromMixedAssignation(MixedAssignation oldassignation){…} }

28 Thanks! DECSAI ~ UGR


Download ppt "Programo Coding Cora Pérez Ariza ~ DECSAI ~ UGR Granada, January 28 th & 29 th, 2009."

Similar presentations


Ads by Google