2424import network .aika .neuron .INeuron ;
2525import network .aika .neuron .Synapse ;
2626import network .aika .neuron .activation .Activation ;
27- import network .aika .neuron .activation .Activation .Option ;
2827import network .aika .neuron .activation .Activation .OscillatingActivationsException ;
29- import network .aika .neuron .activation .Candidate ;
3028import network .aika .neuron .activation .Position ;
31- import network .aika .neuron .activation .SearchNode ;
32- import network .aika .neuron .activation .SearchNode .TimeoutException ;
29+ import network .aika .neuron .activation .link .Linker ;
30+ import network .aika .neuron .activation .search .Option ;
31+ import network .aika .neuron .activation .search .SearchNode ;
32+ import network .aika .neuron .activation .search .SearchNode .TimeoutException ;
3333import network .aika .neuron .activation .*;
3434import org .slf4j .Logger ;
3535import org .slf4j .LoggerFactory ;
3636
3737import java .util .*;
3838import java .util .stream .Collectors ;
3939
40- import static network .aika .neuron .activation .SearchNode .Decision .SELECTED ;
41- import static network .aika .neuron .activation .SearchNode .Decision .UNKNOWN ;
40+ import static network .aika .neuron .INeuron .Type .*;
41+ import static network .aika .neuron .activation .Activation .CANDIDATE_COMP ;
42+ import static network .aika .neuron .activation .search .Decision .SELECTED ;
43+ import static network .aika .neuron .activation .search .Decision .UNKNOWN ;
4244
4345
4446/**
@@ -57,11 +59,6 @@ public class Document implements Comparable<Document> {
5759 public static int MAX_ROUND = 20 ;
5860 public static int ROUND_LIMIT = -1 ;
5961
60- /**
61- * Experimental code: not working yet!
62- */
63- public static boolean INCREMENTAL_MODE = false ;
64-
6562 private final int id ;
6663 private final StringBuilder content ;
6764
@@ -109,8 +106,6 @@ public class Document implements Comparable<Document> {
109106
110107 private TreeMap <Integer , Activation > activationsById = new TreeMap <>();
111108
112- private int lastProcessedActivationId = -1 ;
113-
114109 private static class ActKey {
115110 int slot ;
116111 Position pos ;
@@ -130,7 +125,7 @@ public ActKey(int slot, Position pos, INeuron neuron, int actId) {
130125
131126
132127 public SearchNode selectedSearchNode ;
133- public ArrayList <Candidate > candidates = new ArrayList <>();
128+ public ArrayList <Activation > candidates = new ArrayList <>();
134129
135130 public long createV ;
136131
@@ -369,36 +364,30 @@ public void propagate() {
369364
370365
371366 public void generateCandidates () throws CyclicDependencyException {
372- TreeSet <Candidate > tmp = new TreeSet <>();
367+ TreeSet <Activation > tmp = new TreeSet <>(CANDIDATE_COMP );
373368 int i = 0 ;
374369
375- if (!INCREMENTAL_MODE ) {
376- candidates .clear ();
377- }
378-
379- for (Activation act : activationsById .subMap (INCREMENTAL_MODE ? lastProcessedActivationId : -1 , false , Integer .MAX_VALUE , true ).values ()) {
380- if (act .getDecision () == UNKNOWN && act .getUpperBound () > 0.0 ) {
381- SearchNode .invalidateCachedDecision (act );
382- tmp .add (new Candidate (act , i ++));
383-
384- lastProcessedActivationId = Math .max (lastProcessedActivationId , act .getId ());
370+ for (Activation act : activationsById .values ()) {
371+ if (act .getType () == EXCITATORY && act .getDecision () == UNKNOWN && act .getUpperBound () > 0.0 ) {
372+ act .setCandidateId (i ++);
373+ tmp .add (act );
385374 }
386375 }
387376
388377 long v = visitedCounter ++;
389378 for (Activation act : inputNeuronActivations ) {
390- act .markedHasCandidate = v ;
379+ act .markHasCandidate ( v ) ;
391380 }
392381
393382 while (!tmp .isEmpty ()) {
394383 int oldSize = tmp .size ();
395- for (Candidate c : tmp ) {
396- if (c .checkDependenciesSatisfied (v )) {
397- tmp .remove (c );
398- c . setId (candidates .size ());
399- candidates .add (c );
384+ for (Activation act : tmp ) {
385+ if (act .checkDependenciesSatisfied (v )) {
386+ tmp .remove (act );
387+ act . setCandidateId (candidates .size ());
388+ candidates .add (act );
400389
401- c . getActivation (). markedHasCandidate = v ;
390+ act . markHasCandidate ( v ) ;
402391 break ;
403392 }
404393 }
@@ -423,15 +412,17 @@ public void process() throws TimeoutException, CyclicDependencyException, Oscill
423412 public void process (Long timeoutInMilliSeconds ) throws TimeoutException , CyclicDependencyException , OscillatingActivationsException {
424413 linker .lateLinking ();
425414
426- inputNeuronActivations .forEach (act -> valueQueue .propagateActivationValue (0 , act ));
415+ inputNeuronActivations .forEach (act -> {
416+ valueQueue .propagateActivationValue (act , null , true , true );
417+ });
427418
428419 generateCandidates ();
429420
430- SearchNode rootNode = null ;
431- if ( selectedSearchNode == null || ! INCREMENTAL_MODE ) {
432- selectedSearchNode = new SearchNode ( this , null , null , 0 );
433- rootNode = selectedSearchNode ;
434- }
421+ selectedSearchNode = new SearchNode ( this , null , null , 0 ) ;
422+ selectedSearchNode . updateActivations ( this );
423+ storeFinalState ( );
424+
425+ SearchNode rootNode = selectedSearchNode ;
435426
436427 SearchNode .search (this , selectedSearchNode , visitedCounter ++, timeoutInMilliSeconds );
437428
@@ -448,30 +439,23 @@ public void process(Long timeoutInMilliSeconds) throws TimeoutException, CyclicD
448439 }
449440
450441
451- private void computeSoftMax () {
452- for (Activation act : activationsById .values ()) {
453- double offset = Double .MAX_VALUE ;
454- for (Option option : act .getOptions ()) {
455- offset = Math .min (offset , Math .log (option .cacheFactor ) + option .weight );
456- }
442+ public void storeFinalState () {
443+ for (Activation act : activationsById .values ()) {
444+ act .finalOption = act .currentOption ;
445+ }
446+ }
457447
458- double norm = 0.0 ;
459- for (Option option : act .getOptions ()) {
460- norm += Math .exp (Math .log (option .cacheFactor ) + option .weight - offset );
461- }
462448
463- for (Option option : act .getOptions ()) {
464- if (option .decision == SELECTED ) {
465- option .p = Math .exp (Math .log (option .cacheFactor ) + option .weight - offset ) / norm ;
466- }
467- }
449+ private void computeSoftMax () {
450+ for (Activation act : activationsById .values ()) {
451+ act .computeSoftMax ();
468452 }
469453 }
470454
471455
472456 public void dumpDebugCandidateStatistics () {
473- for (Candidate c : candidates ) {
474- log .info (c . toString ());
457+ for (Activation act : candidates ) {
458+ log .info (act . searchStateToString ());
475459 }
476460 }
477461
@@ -605,20 +589,6 @@ public String activationsToString() {
605589 }
606590
607591
608- public String dumpOscillatingActivations () {
609- StringBuilder sb = new StringBuilder ();
610- activatedNeurons .stream ()
611- .flatMap (n -> n .getActivations (this , false ))
612- .filter (act -> act .isOscillating ())
613- .forEach (act -> {
614- sb .append (act .getId () + " " + act .slotsToString () + " " + act .getDecision () + "\n " );
615- sb .append (act .linksToString () + "\n " );
616- sb .append ("\n " );
617- });
618- return sb .toString ();
619- }
620-
621-
622592 public static class CyclicDependencyException extends RuntimeException {
623593
624594 public CyclicDependencyException () {
0 commit comments