Skip to content

Commit 4bb5dd8

Browse files
Merge pull request #14 from aika-algorithm/conflict-refactoring
Conflict refactoring
2 parents 55c791f + 6036a29 commit 4bb5dd8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2658
-2361
lines changed

src/main/java/network/aika/ActivationFunction.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package network.aika;
22

3-
3+
/**
4+
*
5+
* @author Lukas Molzberger
6+
*/
47
public enum ActivationFunction {
58

69
NULL_FUNCTION(x -> 0.0),

src/main/java/network/aika/Document.java

Lines changed: 39 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,23 @@
2424
import network.aika.neuron.INeuron;
2525
import network.aika.neuron.Synapse;
2626
import network.aika.neuron.activation.Activation;
27-
import network.aika.neuron.activation.Activation.Option;
2827
import network.aika.neuron.activation.Activation.OscillatingActivationsException;
29-
import network.aika.neuron.activation.Candidate;
3028
import 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;
3333
import network.aika.neuron.activation.*;
3434
import org.slf4j.Logger;
3535
import org.slf4j.LoggerFactory;
3636

3737
import java.util.*;
3838
import 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() {

src/main/java/network/aika/Model.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
import network.aika.neuron.Neuron;
2424
import network.aika.Provider.SuspensionMode;
2525
import network.aika.neuron.Synapse;
26-
import network.aika.neuron.activation.Linker;
27-
import network.aika.neuron.activation.SearchNode;
26+
import network.aika.neuron.activation.search.SearchNode;
2827

2928
import java.io.DataInput;
3029
import java.io.DataOutput;
@@ -34,8 +33,6 @@
3433
import java.util.concurrent.atomic.AtomicInteger;
3534
import java.util.concurrent.atomic.AtomicLong;
3635

37-
import static network.aika.neuron.INeuron.Type.INPUT;
38-
3936

4037
/**
4138
* The model consists of two layers. The first layer is the actual neural network consisting of neurons and synapses.
@@ -57,8 +54,6 @@ public class Model {
5754

5855
public SuspensionHook suspensionHook;
5956

60-
public SearchNode.SkipSelectStep skipSelectStep = (act) -> false;
61-
6257
public AtomicInteger docIdCounter = new AtomicInteger(0);
6358
public AtomicInteger currentId = new AtomicInteger(0);
6459

@@ -99,15 +94,6 @@ public void setSuspensionHook(SuspensionHook suspensionHook) {
9994
}
10095

10196

102-
public SearchNode.SkipSelectStep getSkipSelectStep() {
103-
return skipSelectStep;
104-
}
105-
106-
public void setSkipSelectStep(SearchNode.SkipSelectStep skipSelectStep) {
107-
this.skipSelectStep = skipSelectStep;
108-
}
109-
110-
11197
public Neuron createNeuron(Type type) {
11298
return createNeuron(null, type);
11399
}

src/main/java/network/aika/PassiveInputFunction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import network.aika.neuron.Synapse;
44
import network.aika.neuron.activation.Activation;
55

6+
/**
7+
*
8+
* @author Lukas Molzberger
9+
*/
610
public interface PassiveInputFunction {
711

812
double getActivationValue(Synapse s, Activation outputActivation);

src/main/java/network/aika/Provider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import java.util.zip.GZIPInputStream;
77
import java.util.zip.GZIPOutputStream;
88

9-
9+
/**
10+
*
11+
* @author Lukas Molzberger
12+
*/
1013
public class Provider<T extends AbstractNode> implements Comparable<Provider<?>> {
1114

1215
protected Model model;

src/main/java/network/aika/Utils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,22 @@ public static String collapseText(String txt, int length) {
3737
}
3838
}
3939

40+
41+
public static String addPadding(String s, int targetSize) {
42+
StringBuilder sb = new StringBuilder();
43+
sb.append(s);
44+
for(int i = s.length(); i < targetSize; i++) {
45+
sb.append(' ');
46+
}
47+
48+
return sb.toString();
49+
}
50+
51+
52+
public static Integer max(Integer a, Integer b) {
53+
if(a == null) return b;
54+
if(b == null) return a;
55+
56+
return Math.max(a, b);
57+
}
4058
}

0 commit comments

Comments
 (0)