1 / 28

Programo Coding

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.

lew
Download Presentation

Programo Coding

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Code • Potential package • Class Diagram • Potential Values • Potential Table: basic operations • Assignation package • Class Diagram • Factory Pattern Index

  3. Code Potential Package: Class Diagram

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

  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. 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: Code Potential Package: Potential Values: Bridge Pattern

  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). Code Potential Package: Potential Values: Inner Classes

  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(); } } Code Potential Package: Potential Values: Potential 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; } } Code Potential Package: Potential Values: PotentialTable Class Code

  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; } } Code Potential Package: Potential Values: Creating a new representation

  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 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: posx(next(y) x) = posx(y x) + accOffsetxy(varToIncr(y)) Code Potential package: Potential Table operations

  13. Configurations For simplicity in the algorithms, in a configuration we increment the variables from left to right: Code Potential package: Potential Table operations

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. Code Assignation Package: Class Diagram

  21. 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

  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); } Code Assignation Package: Interfaces

  23. Code Assignation Package: Two kinds of Assignations

  24. 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

  25. Code Assignation Package: Efficience of Concrete implementations

  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); Code Assignation Package: Factory Pattern

  27. 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

  28. Thanks! DECSAI ~ UGR

More Related