View
216
Download
0
Category
Preview:
Citation preview
Programo CodingProgramo Coding
Cora Pérez Ariza ~ DECSAI ~ UGR
Granada, January 28th & 29th, 2009
IndexIndex
Code
◦Potential package Class Diagram Potential Values Potential Table: basic operations
◦Assignation package Class Diagram Factory Pattern
2
CodePotential Package: Class Diagram
3
CodePotential package design
4
Benefit of this designAllows us to implement generic propagation algorithms
How to do it?Implement them in Potential class
CodePotential Package: Potential Values: Bridge Pattern
5
General IdeaDecouple an abstraction from its implementation.
How to do it?Define an interface with the operations the abstraction needs.
Keep other methods private.
CodePotential Package: Potential Values: Bridge Pattern
6
Potential ValuesDifferent representations of potentials differ only in
the structure their values are stored
First approximation:
CodePotential 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).
CodePotential 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(); }}
CodePotential 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; }
}
CodePotential 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; }
}
CodePotential package: Potential Table operations
11
Reference articleOperation with potentials of discrete variables
M.Arias, F.J. DíezInternational Journal of Approximate Reasoning
46(2007) 166-187
General IdeaIn case of large potentials, the cost of retrieving
their elements is significantly higher than the cost of multiplying, maximizing or summing them
CodePotential 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))
CodePotential package: Potential Table operations
13
Configurations
For simplicity in the algorithms, in a configuration we increment the variables from left to right:
A B C
0 0 0
1 0 0
0 1 0
1 1 0
0 0 1
1 0 1
0 1 1
1 1 1
CodePotential package: Potential Table operations: Marginalization
14
Marginalization over a set of variables
We have a potential ψX(x) where X=Xe U Xk
and we want a marginalized potential ψm
Xk(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
CodePotential package: Potential Table operations: Marginalization
15
Pseudocode:
Pos = 0For(|Xk
*|){ For(|Xe
*|){ sum = value in ψX in Pos Pos = next position in ψX reordering variables } ψm
Xk= sum
calculate next position in ψmXk
}
CodePotential 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 Xo and we want to calculate ψXo
Xu
To do so, we should work with a reordered potential that has the observed variables at the end, lets call it ψy
CodePotential 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
}
CodePotential package: Potential Table operations: Combination
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)
CodePotential 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
}
CodeAssignation Package: Class Diagram
20
CodeAssignation Package: Interfaces
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();
}
CodeAssignation 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);
}
CodeAssignation Package: Two kinds of Assignations
23
CategoricalAssignation MixedAssignation
Only Categorical Variables Both Categorical and Continuous Variables
Fixed set of variables Allows addition/removal of variables
CodeAssignation Package: Concrete Implementations
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
…}
CodeAssignation Package: Efficience of Concrete implementations
25
IndexedCategoricalAssignation
IndexedMixedAssignation
getValue(Var) O(1) O(1)
getValue(Index) O(1) O(1)
getVariable(Index) O(1) O(1)
setValue(Var, Val) O(1) O(1)
setValue(Index, Val) O(1) O(1)
indexOf(Var) O(1) O(n)
CodeAssignation 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);
CodeAssignation Package: Factory Pattern: Factory Code
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){…}
}
Thanks!Thanks!DECSAI ~ UGR
Recommended