00001 package commandline.Encoder.dynamicProg;
00002
00003 import java.util.ArrayList;
00004 import java.util.LinkedList;
00005 import java.util.PriorityQueue;
00006 import java.util.Queue;
00007
00008 import junit.framework.Assert;
00009
00010 import commandline.ImageStuff.BreathFirstVisitor;
00011 import commandline.ImageStuff.LeavesCollector;
00012 import commandline.ImageStuff.GeometricObjects.GenericEdge;
00013 import commandline.ImageStuff.GeometricObjects.GeometricObject;
00014 import commandline.debuggingCode.Debug;
00015
00024 public class Tree implements ITree{
00025 protected Node rootNode;
00026
00027 protected PriorityQueue<Node> queue;
00028
00029 float Lagrangian;
00030 protected Tree(Node rootNode) {
00031 this.rootNode = rootNode;
00032 ArrayList<Node> leaves =LeavesCollector.getLeavesFromNode(rootNode);
00033 queue = new PriorityQueue<Node>(leaves.size(), new NodeLevelComparator());
00034 for (Node n : leaves) {
00035 queue.offer(n);
00036 }
00037 }
00038
00045 public void prune(float Lagrangian,int WHICH_NORM) {
00046
00047
00048 setLagrangian(Lagrangian);
00049
00050 while (queue.peek().getLevel() > 0) {
00051
00052 Node n = next();
00053
00054 Node parent = n.getParent();
00055
00056 queue.offer(parent);
00057
00058
00059 float delta_error =parent.getDeltaSubtreeError(WHICH_NORM);
00060 float delta_rate= parent.getCostOfSubTree();
00061 float current_slope=delta_error/delta_rate;
00062 if(Debug.DEBUG && Debug.DEBUG_PRUNE) Debug.print ("delta_error"+delta_error);
00063 if(Debug.DEBUG && Debug.DEBUG_PRUNE) Debug.print ("delta_rate"+delta_rate);
00064 if(Debug.DEBUG && Debug.DEBUG_PRUNE) Debug.print ("current slope:= "+current_slope);
00065
00066 if (current_slope <= Lagrangian ) {
00070 parent.setPruned(true);
00071 } else {
00075 }
00076
00077 if(parent.hasChildren()){
00078 if(Debug.DEBUG && Debug.DEBUG_PRUNE) Debug.print(" ->remove children from queue;");
00079
00080 BreathFirstVisitor<Node> breathFirstVisitor=new BreathFirstVisitor<Node>(parent,new ArrayList<Node>());
00081
00082
00083 if(breathFirstVisitor.hasNext())breathFirstVisitor.next();
00084
00085 while (breathFirstVisitor.hasNext()) {
00086 Node offspring =breathFirstVisitor.next();
00087 Assert.assertFalse(offspring==parent);
00088 queue.remove(offspring);
00089 }
00090
00091
00092 }
00093
00094 }
00095
00096 }
00097
00098
00099
00100
00101 protected Node next() {
00102
00103 return queue.poll();
00104 }
00105
00106 public Node getRootNode() {
00107 return rootNode;
00108 }
00109
00114 public void print() {
00115 Queue<Node> nodeList = new LinkedList<Node>();
00116 nodeList.add(this.getRootNode());
00117 Node node;
00118 while (!nodeList.isEmpty()) {
00119 node = nodeList.poll();
00120 if (node.getLevel() >= 0)
00121 System.out.println("level: " + node.getLevel() + " cost: ");
00122 if (node.hasChildren()) {
00123 nodeList.addAll(node.getChildren());
00124 }
00125 }
00126 }
00127
00128
00132 public void reset(){
00133 Queue<Node> nodeList = new LinkedList<Node>();
00134 nodeList.add(this.getRootNode());
00135 Node node;
00136 while (!nodeList.isEmpty()) {
00137 node = nodeList.poll();
00138 node.reset();
00139 if(node.hasChildren()){
00140 nodeList.addAll(node.getChildren());
00141 }
00142 }
00143 }
00144
00145
00146
00150 public void applyPruningInRealTree(){
00151 Queue<Node> nodeList = new LinkedList<Node>();
00152 nodeList.add(this.getRootNode());
00153 Node node;
00154 while (!nodeList.isEmpty()) {
00155 node = nodeList.poll();
00156 if (node.hasChildren()) {
00157 nodeList.addAll(node.getChildren());
00158 }
00159
00160 if(node.isPruned()){
00161 if(node.getGeometricObject().isAnEdge()){
00162 ((GenericEdge)node.getGeometricObject()).prune(this);
00163 }
00164 else{
00165 node.getGeometricObject().setChildren(new ArrayList<GeometricObject>());
00166 }
00167 Assert.assertFalse(node.getGeometricObject().hasChildren());
00168 }
00169
00170
00171
00172 }
00173 }
00174
00179 private float getLagrangian() {
00180 return Lagrangian;
00181 }
00182
00183 private void setLagrangian(float lagrangian) {
00184 Lagrangian = lagrangian;
00185 }
00186
00187 }