280 likes | 385 Views
Programo Coding. Cora Pérez Ariza ~ DECSAI ~ UGR Granada, January 28 th & 29 th , 2009. Code Potential package Class Diagram Potential Values Potential Table: basic operations Assignation package Class Diagram Factory Pattern. Index. Code. Potential Package: Class Diagram.
E N D
Programo Coding Cora PérezAriza ~ DECSAI ~ UGR Granada, January 28th & 29th, 2009
Code • Potential package • Class Diagram • Potential Values • Potential Table: basic operations • Assignation package • Class Diagram • Factory Pattern Index
Code Potential Package: Class Diagram
Benefit of this design Allows us to implement generic propagation algorithms How to do it? Implement them in Potential class Code Potential package design
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. Code Potential Package: Potential Values: Bridge Pattern
Potential Values Different representations of potentials differ only in the structure their values are stored First approximation: Code Potential Package: Potential Values: Bridge Pattern
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). Code Potential Package: Potential Values: Inner Classes
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(); } } Code Potential Package: Potential Values: Potential Class Code
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; } } Code Potential Package: Potential Values: PotentialTable Class Code
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; } } Code Potential Package: Potential Values: Creating a new representation
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 Code Potential package: Potential Table operations
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: posx(next(y) x) = posx(y x) + accOffsetxy(varToIncr(y)) Code Potential package: Potential Table operations
Configurations For simplicity in the algorithms, in a configuration we increment the variables from left to right: Code Potential package: Potential Table operations
Marginalization over a set of variables • We have a potential ψX(x) where X=Xe U Xk • and we want a marginalized potential • ψmXk(xk) = ∑Xe ψX(xe,xk) • Instead of creating a new potential with the variables to eliminate at the beginning, we retrieve the elements of ψX in the correct order Code Potential package: Potential Table operations: Marginalization
Pseudocode: • Pos = 0 • For(|Xk*|){ • For(|Xe*|){ • sum = value in ψX in Pos • Pos = next position in ψX reordering variables • } • ψmXk= sum • calculate next position in ψmXk • } Code Potential package: Potential Table operations: Marginalization
Restrict a potential to a configuration of variables • We have a potential ψX and a set of observed variables Xo and we want to calculate ψXoXu • To do so, we should work with a reordered potential that has the observed variables at the end, lets call it ψy Code Potential package: Potential Table operations: Projection
Pseudocode: • pos = conf [Xu = 0, Xo] in ψX • For(|Xu*|){ • ψXoXu = value in ψX for pos • calculate next position in ψXoXu • pos = calculate next position in ψx according to ψy • } Code Potential package: Potential Table operations: Projection
Combine two potentials • We have two potentials ψX1 and ψX2 and we want to compute ψY where Y = X1 U X2 • ψY (y) = ψX1 (y X1) x ψX2 (y X2) Code Potential package: Potential Table operations: Combination
Pseudocode: • PosX1 = 0; • PosX2 = 0; • For(|Y*|){ • ψY= value of ψX1 in PosX1 * value of ψX2 in PosX2 • PosX1 = next position in ψX1 according to ψy • PosX2 = next position in ψX2 according to ψy • calculate next position in ψY • } Code Potential package: Potential Table operations: Combination
Code Assignation Package: Class Diagram
public interface Assignation<V extends Variable> { 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<V> getVariables(); public String toString(); } Code Assignation Package: Interfaces
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); } Code Assignation Package: Interfaces
Code Assignation Package: Two kinds of Assignations
class IndexedCategoricalAssignation implements CategoricalAssignation{ private Map<CategoricalVariable,Integer> mapVariableIndex; // For each variable return its position private Map<Integer,CategoricalVariable> mapIndexVariable; // For each index return its variable private int[] values; //Store the values … } class IndexedMixedAssignation implements MixedAssignation{ private Map<Variable,Double> mapVariableValue; // For each variable return its value private ArrayList<Variable > variables; //Store the order of the variables … } Code Assignation Package: Concrete Implementations
Code Assignation Package: Efficience of Concrete implementations
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); Code Assignation Package: Factory Pattern
public class AssignationFactory{ • public static MixedAssignation createMixedAssignation(VariableSet<Variable> varset, List<Double> initialValues){…} • public static CategoricalAssignation createCategoricalAssignation(VariableSet<CategoricalVariable> varset, List<Double> initialValues){…} • public static MixedAssignation createMixedFromCategoricalAssignation(CategoricalAssignation oldassignation){…} • public static CategoricalAssignation createCategoricalFromMixedAssignation(MixedAssignation oldassignation){…} • } Code Assignation Package: Factory Pattern: Factory Code
Thanks! DECSAI ~ UGR