codesimian
Class SimpleBayesNet

java.lang.Object
  extended by codesimian.CS<CSGeneric>
      extended by codesimian.DefaultCS
          extended by codesimian.SimpleBayesNet
All Implemented Interfaces:
CodeSimian, java.util.Comparator

public class SimpleBayesNet
extends DefaultCS
implements java.util.Comparator

        simpleBayes(input0 input1 input2 input3...)
        There are 2 phases... TRAINING-MODE and CALCULATE-MODE.

        During training, data is recorded, but nothing calculated.
        When training mode ends, an OUTPUT NODE is selected by DELETING
        any one of the params. You should use the last index
        because I'm going to remove the ability to put it anywhere else.
        An OUTPUT NODE is put in the last index.
        Now its in CALCULATE-MODE. Replace any of the params (with new chances, range 0 to 1),
        and SimpleBayesNet.DForProxy() will return the PREDICTED output value
        based on those new input chances. You can ignore the OUTPUT NODE.

        Quantity of input nodes can increase only during TRAINING-MODE.
        Training datas can have different sizes,
        but will be enlarged to the size of the biggest by filling the ends with 0.5.

        When CALCULATE-MODE starts, the last param is replaced by the one OUTPUT NODE.
        TRAINING-MODE ends when an output node is selected by deleting its index.
        That index is moved to the last index, and contains the OUTPUT NODE after every DForProxy().

        From the OUTPUT NODE, you can find any node in this bayesian-network.
        If you execute the OUTPUT NODE, it should return the same as this SimpleBayesNet's D().

        TODO: REFACTOR THIS:
        In either mode, D() always returns the value of the last param.
        If you replace the OUTPUT NODE, the new value is ignored,
        and D() will put the OUTPUT NODE back. END REFACTOR.

        In CALCULATE-MODE, you can try to optimize the network by adding or modifying bayes-nodes.
        You can find all the bayes-nodes by traversing the directed-acyclic-network starting
        at the last param (the 1 output node).

        To add new bayes-nodes, call setP(k, the new bayes-node),
        where k equals countP()-1 or countP().
        This SimpleBayesNet judges its accuracy as a bayes-node.
        If it is a good OUTPUT NODE (judged by the CS returned by getNodeJudger()),
        then it BECOMES THE NEW OUTPUT NODE.
        It might also be put somewhere in the middle of the network if it looks useful for that,
        but this default implementation does not check for that (Do it in a subclass!).

        INPUT NODES are the only nodes with 0 childs.
        For any input node x, x.P(0).countP()==0 (0 childs)
        and x.P(1).countP()==2 (2 possible values of this node, this number doubles with every child added).

        SimpleBayesNet implements Comparator to compare bayes-nodes' values,
        and choose which nodes to use or modify, to evolve the data in bayes-nodes.


Nested Class Summary
static class SimpleBayesNet.BayesNodeIndependentChildsDependentParent
          param0 is a bayes-node.
static class SimpleBayesNet.Test
          Tests SimpleBayesNet to see if it works.
 
Field Summary
static int MINP
          this is a "small" number, but not guaranteed to be any specific number
protected  int nameNumber
           
protected  CS nodeJudger
           
protected  java.util.List<CS> nodes
          Nodes in this bayesian network.
static java.util.Random rand
           
protected  java.util.List trainingData
          contains double[] arrays, probably with different sizes.
protected  boolean trainingMode
          if false, calculateMode
 
Fields inherited from class codesimian.CS
DESCRIPTION, END, EXECPROXY, HEAP, JAVACODE, MYFUEL, NAME, NEWINSTANCE, NULL, PARENT, PARSEPRIORITY, PREV, TESTER, THIS
 
Constructor Summary
SimpleBayesNet()
           
 
Method Summary
 void addOneRandomlySelectedChildToEveryNonInputNode()
          randomly selected child always has lower index than parent its put in.
 int compare(java.lang.Object bayesNodeA, java.lang.Object bayesNodeB)
          Comparator interface.
 double cost()
          cost() should be changed to return a float, and should be renamed to costToExecute()

cost of EXECUTING this CS, not including any CSs it executes recursively.
 java.util.List<CS> createNetwork(int networkSize)
          Creates an untrained network with no experience.
 boolean deleteP(int index)
          WARNING: if returns false, could have deleted but not moved other params down 1 index.
WARNING: if duplicates not allowed, can not slide params down 1 index the standard way, returns false.
static double dependence(double[] condProb, int childIndexA, int childIndexB, boolean believeCondProbSumsTo1)
          condProb[] is the param1 of some bayes-node.
 java.lang.String description()
          a short description of this CS, shorter than the javadoc, but long enough to tell what the params are for.
 double DForProxy()
          Execute this CS and cast to double.
static boolean exaggerate(double[] condProb, double amount)
          make big values bigger and small values smaller.
static boolean exaggerateNode(CS bayesNode, double amount)
           
 CS getNodeJudger()
          returns the CS that judges bayes-nodes.
 java.lang.String keyword()
          For the CodeSimian language as a String.
CodeSimian language keyword, like "+" "*" "max" ">" etc.

Override this function if you want to specify a keyword other than how I derive them from the class name, like + for Add.

Some CSs might never be intended to be used in the language by their keyword.
The best example (4/05) is Num, because it is used in the language like "3.4" instead of "num()".
 int maxP()
          unlimited, if enough memory
 int minP()
          for hard-coded evolving reasons, need approximately at least 6 inputs + 1 output.
 CS newBayesNode(CS[] childs, CS historyOfNewNode)
          the type of bayes-node that this class prefers: BayesNode (and fills its param0 and param1).
 CS newRandomNode(CS replaceMe, int childCountOfNewNode, int maxChildIndexInNet)
          Makes a new partially random bayes-node with the same HISTORY (every BayesNode's param2) values as replaceMe.param2's values.
 boolean offerBayesNodeToNetwork(CS newBayesNode)
          This bayesian network looks for a place to use the bayes-node without changing its data.
 void randomizeNode(CS bayesNode)
          randomize the history, then update the conditional-probabilities to match it
 void randomlyEnlargeNetwork(int newNetSize)
           
static java.util.List<CS> removeChildsThatDependMoreOnAnyOneSiblingThanParent(CS parentBayesNode)
          Removes some childs and returns them in a List.
 java.util.List<CS> removeNodesThatDoNotDescendFromOutputNode(java.util.List<CS> net)
          Removes unused nodes, except input nodes (first) and the output node (last).
 boolean replaceOutputNodeWithBetterOutputNode(int tryThisManyNewNodes)
           
 boolean shrinkAllNodesToHaveMaxChilds(int maxChildQuantity)
           
 void testEnlargeNetwork()
           
 boolean trainNetwork()
          Done once between TRAINING-MODE and CALCULATE-MODE to set the conditional-probabilities of all the nodes and increase network size.
 boolean unknownCost()
           
 boolean updateNetwork()
           
 double valueOf(CS bayesNode)
          the highest valued bayes node is the root
 
Methods inherited from class codesimian.DefaultCS
B, C, countP, decrementMyFuel, F, fuel, getExec, getObject, heap, I, indexP, indexPName, insertB, insertC, insertD, insertF, insertI, insertJ, insertL, insertL, insertL1, insertP, insertS, insertZ, J, javaCode, LForProxy, LForProxy, myFuel, name, newInstance, objectToCS, objectToCSArray, objectToCSArray, P, prevD, prevL, PType, S, setB, setC, setCountP, setD, setD, setExec, setF, setFuel, setI, setJ, setL, setL, setL, setL1, setMyFuel, setName, setObject, setP, setPrevExec, setPType, setS, setZ, start, toString, V, Z
 
Methods inherited from class codesimian.CS
addB, addC, addD, addF, addI, addJ, addL, addP, addP, addP, addP, addP, addS, addZ, BForProxy, CForProxy, clone, D, deleteP, FForProxy, GETB, GETC, GETD, GETF, GETI, GETJ, GETL, GETS, GETZ, IForProxy, isIllusion, JForProxy, L, L, L, L, L, maxD, minD, overwrites, parent, parsePriority, PB, PC, PD, PF, PI, PJ, PL, prevB, prevC, prevF, prevI, prevJ, prevS, prevZ, proxyOf, PS, PZ, reflect, reflect, reflect6, setB, SETB, setC, SETC, setCost, SETD, setDescription, setF, SETF, setHeap, setI, SETI, setJ, SETJ, SETL, setL, setL, setParent, setParsePriority, setProxyOf, setS, SETS, setTester, setZ, SETZ, SForProxy, tester, toJavaCode, VForProxy, voidReflect, ZForProxy
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Field Detail

nodes

protected java.util.List<CS> nodes
Nodes in this bayesian network. The INPUT NODES have the lowest indexs, and the OUPUT NODE has the highest index. There are probably nodes in the middle. Nodes may only have lower-indexed nodes as params. nodes.size() may never be less than countP(). Nodes must be a "bayes-node" as defined in BayesNode.java.


nameNumber

protected int nameNumber
See Also:
newBayesNode(CS[])

rand

public static java.util.Random rand

trainingData

protected java.util.List trainingData
contains double[] arrays, probably with different sizes. Use the biggest size, and assume all missing data is .5


trainingMode

protected boolean trainingMode
if false, calculateMode


MINP

public static final int MINP
this is a "small" number, but not guaranteed to be any specific number

See Also:
Constant Field Values

nodeJudger

protected CS nodeJudger
Constructor Detail

SimpleBayesNet

public SimpleBayesNet()
Method Detail

DForProxy

public double DForProxy()
Description copied from class: CS
Execute this CS and cast to double.

D() and DForProxy() are the 2 most important functions in CS. They execute this CS. All other execute functions, by default, return DForProxy cast to their own type.

For example, J() calls the proxy which calls JForProxy() which calls DForProxy(). D() calls the proxy which calls DForProxy().

By default, all other primitive EXECUTE functions defer to D.
Functions that EXECUTE this CS: L(Class) L(int,Class,int) Z() B() C() S() I() J() F() D() V()

Specified by:
DForProxy in class DefaultCS

deleteP

public boolean deleteP(int index)
Description copied from class: DefaultCS
WARNING: if returns false, could have deleted but not moved other params down 1 index.
WARNING: if duplicates not allowed, can not slide params down 1 index the standard way, returns false.

Overrides:
deleteP in class DefaultCS

valueOf

public double valueOf(CS bayesNode)
the highest valued bayes node is the root


compare

public int compare(java.lang.Object bayesNodeA,
                   java.lang.Object bayesNodeB)
Comparator interface. Compare 2 bayes-nodes. Non-CS objects are always LESS THAN any CS.

Specified by:
compare in interface java.util.Comparator

removeChildsThatDependMoreOnAnyOneSiblingThanParent

public static java.util.List<CS> removeChildsThatDependMoreOnAnyOneSiblingThanParent(CS parentBayesNode)
Removes some childs and returns them in a List. Child should depend on parent more than child depends on any 1 sibling. 1 child has no siblings, so the last child can not be removed. Tries to remove childs with low depencence to parent and high dependence to childs that it hasnt removed yet (or will not be removed).

WARNING: All parents (and ancestors recursively) of parentBayesNode should be BayesNodeUpdated (in the right order) if any childs are removed. The easiest way to do this is to the whole network, from index 0 to index nodes.size()-1. You can wait to BayesNodeUpdate until you call this on many nodes.

See Also:
dependence(double[],int,int,boolean)

shrinkAllNodesToHaveMaxChilds

public boolean shrinkAllNodesToHaveMaxChilds(int maxChildQuantity)

addOneRandomlySelectedChildToEveryNonInputNode

public void addOneRandomlySelectedChildToEveryNonInputNode()
randomly selected child always has lower index than parent its put in. WARNING: Does not BayesNodeUpdate any nodes. Must do that before exec network again.


updateNetwork

public boolean updateNetwork()

replaceOutputNodeWithBetterOutputNode

public boolean replaceOutputNodeWithBetterOutputNode(int tryThisManyNewNodes)

trainNetwork

public boolean trainNetwork()
Done once between TRAINING-MODE and CALCULATE-MODE to set the conditional-probabilities of all the nodes and increase network size.

Assumes (List of double[]) trainingData is all the same size, and the output data is the last index in all the arrays.


newRandomNode

public CS newRandomNode(CS replaceMe,
                        int childCountOfNewNode,
                        int maxChildIndexInNet)
Makes a new partially random bayes-node with the same HISTORY (every BayesNode's param2) values as replaceMe.param2's values. A different combination of childs from SimpleBayesNet.nodes (CS[] array) is used, but their purpose is still to calculate and predict the same HISTORY (replaceMe.P(2)).


randomizeNode

public void randomizeNode(CS bayesNode)
randomize the history, then update the conditional-probabilities to match it


randomlyEnlargeNetwork

public void randomlyEnlargeNetwork(int newNetSize)

testEnlargeNetwork

public void testEnlargeNetwork()

newBayesNode

public CS newBayesNode(CS[] childs,
                       CS historyOfNewNode)
the type of bayes-node that this class prefers: BayesNode (and fills its param0 and param1). Puts HISTORY in param2 of new node. Returns the new node. If historyOfNewNode is null, uses empty history.


minP

public int minP()
for hard-coded evolving reasons, need approximately at least 6 inputs + 1 output. Returns 7.

Overrides:
minP in class DefaultCS

maxP

public int maxP()
unlimited, if enough memory

Overrides:
maxP in class CS

keyword

public java.lang.String keyword()
Description copied from class: DefaultCS
For the CodeSimian language as a String.
CodeSimian language keyword, like "+" "*" "max" ">" etc.

Override this function if you want to specify a keyword other than how I derive them from the class name, like + for Add.

Some CSs might never be intended to be used in the language by their keyword.
The best example (4/05) is Num, because it is used in the language like "3.4" instead of "num()".
Default: Returns class name, minus package name (and its dots), and change the first letter to lowercase.

For example, CS.MaxParams does not override keyword(), which returns "maxP".

Overrides:
keyword in class DefaultCS
See Also:
CS.parent(), CS.newInstance(), CS.name()

description

public java.lang.String description()
Description copied from class: CS
a short description of this CS, shorter than the javadoc, but long enough to tell what the params are for. Example use: in automatically generated webpages for CodeSimian. Example: "returns sum of all params" for Add.

Overrides:
description in class DefaultCS

cost

public double cost()
Description copied from class: CS
cost() should be changed to return a float, and should be renamed to costToExecute()

cost of EXECUTING this CS, not including any CSs it executes recursively. The INTERNAL cost.

One unit of myFuel() costs cost() units of fuel(). Beware of CSs that counterfeit fuel.

Returns 1 by default.

Overrides:
cost in class CS

unknownCost

public boolean unknownCost()

createNetwork

public java.util.List<CS> createNetwork(int networkSize)
Creates an untrained network with no experience. Input nodes are first, then the 1 output node.


getNodeJudger

public CS getNodeJudger()
returns the CS that judges bayes-nodes. Higher numbers are better. Good bayes-nodes predict their own chance well. Old chances are in each bayes-node's param2. Put a bayes-node in it and execute the judge. Judge does things to it, executes it, and changes things back, to test it. Judge returns a number that is higher when the node predicts better.


offerBayesNodeToNetwork

public boolean offerBayesNodeToNetwork(CS newBayesNode)
This bayesian network looks for a place to use the bayes-node without changing its data. It must have a list of child nodes, list of conditional probabilities, and list of history values. Returns true if I add it to my network.


removeNodesThatDoNotDescendFromOutputNode

public java.util.List<CS> removeNodesThatDoNotDescendFromOutputNode(java.util.List<CS> net)
Removes unused nodes, except input nodes (first) and the output node (last). This should make the network smaller but not change its behavior. Traverses the bayes-nodes, but not their conditional-probabilities or histories. Will not traverse past any CS thats not in net, even if its a valid child. Returns net if could not remove any nodes, else returns a replacement for net.


dependence

public static double dependence(double[] condProb,
                                int childIndexA,
                                int childIndexB,
                                boolean believeCondProbSumsTo1)
condProb[] is the param1 of some bayes-node. size should be a power of 2.


exaggerate

public static boolean exaggerate(double[] condProb,
                                 double amount)
make big values bigger and small values smaller. Amount 0.0 changes nothing. Negative makes the values closer to 0, and approximately less than 1/condProb.length. Positive exaggerates more. condProb[] must sum to 1 except for roundoff error. -1 < amount < infinity.


exaggerateNode

public static boolean exaggerateNode(CS bayesNode,
                                     double amount)