Skip to content

Commit

Permalink
Added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasMolzberger committed Dec 20, 2024
1 parent 6e1797d commit 7b5a470
Show file tree
Hide file tree
Showing 8 changed files with 560 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package network.aika.activations;

import network.aika.Document;
import network.aika.Model;
import network.aika.type.TypeRegistry;
import network.aika.neurons.Neuron;
import network.aika.typedefs.NodeDefinition;
import org.junit.jupiter.api.BeforeEach;

import static network.aika.neurons.RefType.NEURON_EXTERNAL;
import static org.mockito.Mockito.mock;

public abstract class AbstractActivationTest {

TypeRegistry typeRegistry;
NodeDefinition nodeDef;
Model model;
Neuron neuron;
Document doc;

@BeforeEach
public void init() {
typeRegistry = mock(TypeRegistry.class);
nodeDef = new NodeDefinition(typeRegistry, "test")
.setClazz(ConjunctiveActivation.class, Neuron.class);

doc = mock(Document.class);
model = mock(Model.class);

neuron = nodeDef.neuron.instantiate(model);
}
}
135 changes: 135 additions & 0 deletions core/src/test/java/network/aika/activations/ActivationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package network.aika.activations;


import network.aika.Document;
import network.aika.bindingsignal.BindingSignal;
import network.aika.neurons.Neuron;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Map;

import static network.aika.activations.TestBSTypes.A;
import static network.aika.activations.TestBSTypes.B;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;


@ExtendWith(MockitoExtension.class)
public class ActivationTest extends AbstractActivationTest {


@Test
public void testHasConflictingBindingSignals() {
Document doc = mock(Document.class);
Neuron n = mock(Neuron.class);

BindingSignal bs0 = new BindingSignal(0, doc);
BindingSignal bs1 = new BindingSignal(1, doc);

Activation act = new ConjunctiveActivation(
null,
null,
1,
n,
doc,
Map.of(A, bs0)
);

assertFalse(
act.hasConflictingBindingSignals(
Map.of(A, bs0)
)
);

assertFalse(
act.hasConflictingBindingSignals(
Map.of(
A, bs0,
B, bs1
)
)
);

assertTrue(
act.hasConflictingBindingSignals(
Map.of(
A, bs1,
B, bs0
)
)
);
}

@Test
public void testHasNewBindingSignals() {
Document doc = mock(Document.class);
Neuron n = mock(Neuron.class);

BindingSignal bs0 = new BindingSignal(0, doc);
BindingSignal bs1 = new BindingSignal(1, doc);

Activation act = new ConjunctiveActivation(
null,
null,
1,
n,
doc,
Map.of(A, bs0)
);

assertTrue(
act.hasNewBindingSignals(
Map.of(
A, bs0,
B, bs1
)
)
);

assertFalse(
act.hasNewBindingSignals(
Map.of(A, bs0)
)
);
}

@Test
public void testBranch() {
BindingSignal bs0 = new BindingSignal(0, doc);
BindingSignal bs1 = new BindingSignal(1, doc);

Activation parentAct = new ConjunctiveActivation(
null,
null,
1,
neuron,
doc,
Map.of(A, bs0)
);

Activation childAct = parentAct.branch(
Map.of(B, bs1)
);

assertEquals(parentAct, childAct.getParent());
assertEquals(1, childAct.getBindingSignals().size());
assertEquals(bs1, childAct.getBindingSignal(B));
}

@Test
public void testCollectLinkingTargets() {

}

@Test
public void testLinkOutgoing() {

}

@Test
public void testPropagate() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package network.aika.activations;


import network.aika.bindingsignal.BindingSignal;
import network.aika.neurons.ConjunctiveSynapse;
import network.aika.neurons.Neuron;
import network.aika.typedefs.EdgeDefinition;
import network.aika.typedefs.NodeDefinition;
import network.aika.typedefs.SynapseDefinition;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Map;

import static network.aika.activations.TestBSTypes.A;
import static network.aika.activations.TestBSTypes.B;
import static network.aika.bindingsignal.Transition.of;
import static network.aika.neurons.RefType.NEURON_EXTERNAL;


@ExtendWith(MockitoExtension.class)
public class ConjunctiveActivationTest extends AbstractActivationTest {

Neuron inputNeuron;

ConjunctiveSynapse synapse;

@BeforeEach
@Override
public void init() {
super.init();

NodeDefinition inputNodeDef = new NodeDefinition(typeRegistry, "input")
.setClazz(ConjunctiveActivation.class, Neuron.class);

EdgeDefinition firstInputEdgeDef = new EdgeDefinition(typeRegistry, "test")
.setClazz(Link.class, ConjunctiveSynapse.class)
.setInput(inputNodeDef)
.setOutput(nodeDef);

SynapseDefinition synapseDefinition = firstInputEdgeDef.synapse
.setTransition(of(A, B));

inputNeuron = inputNodeDef.neuron.instantiate(model);
synapse = (ConjunctiveSynapse) synapseDefinition.instantiate(inputNeuron, neuron);
}

@Test
public void testLinkIncoming() {
BindingSignal bs0 = new BindingSignal(0, doc);

Activation iAct = inputNeuron.createActivation(null, doc, Map.of(A, bs0));
Activation oAct = neuron.createActivation(null, doc, Map.of(B, bs0));

Assertions.assertNull(oAct.getInputLink(iAct, 0));

bs0.addActivation(iAct);

Assertions.assertNull(oAct.getInputLink(iAct, 0));

oAct.linkIncoming(null);

Assertions.assertNotNull(oAct.getInputLink(iAct, 0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package network.aika.activations;

import network.aika.Config;
import network.aika.Document;
import network.aika.Model;
import network.aika.activations.model.TestTypeModel;
import network.aika.fields.defs.FieldDefinition;
import network.aika.neurons.Neuron;
import network.aika.neurons.Synapse;
import network.aika.typedefs.ActivationDefinition;
import network.aika.typedefs.NeuronDefinition;
import network.aika.typedefs.SynapseDefinition;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static network.aika.activations.TestBSTypes.A;
import static network.aika.neurons.RefType.NEURON_EXTERNAL;

/**
*
* @author Lukas Molzberger
*/
public class MinimalNetworkTest {


@Test
public void minNetworkTest() {
TestTypeModel typeModel = new TestTypeModel();
FieldDefinition<NeuronDefinition, Neuron> bias = typeModel.getNeuron().getBias();
FieldDefinition<SynapseDefinition, Synapse> weight = typeModel.getNeuron().getWeight();
FieldDefinition<ActivationDefinition, Activation> net = typeModel.getNeuron().getNet();

Model m = new Model(typeModel)
.setConfig(new Config());

System.out.println("Begin test\n");

Neuron inputNeuron = typeModel
.getNeuron()
.getNeuron()
.instantiate(m);

Neuron outputNeuron = typeModel
.getNeuron()
.getNeuron()
.instantiate(m)
.setFieldValue(bias, 1.0);

Synapse synapse = typeModel
.getNeuron()
.getSynapse()
.instantiate(inputNeuron, outputNeuron)
.setFieldValue(weight, 10.0)
.setPropagable(m, true);


Document doc = new Document(m, 4);
Activation iAct = doc.addToken(inputNeuron, A, 0)
.setFieldValue(net, 5.0);

Assertions.assertEquals(5.0, iAct.getFieldValue(net));

doc.process();

System.out.println("Dump results:");

Activation oAct = doc.getActivationByNeuron(outputNeuron);

Assertions.assertEquals(10.0, oAct.getFieldValue(net));

doc.disconnect();
}
}
8 changes: 8 additions & 0 deletions core/src/test/java/network/aika/activations/TestBSTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package network.aika.activations;

import network.aika.bindingsignal.BSType;

public enum TestBSTypes implements BSType {
A,
B;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package network.aika.activations.model;

import network.aika.fields.ActivationFunction;

/**
*
* @author Lukas Molzberger
*/
public enum TestActivationFunction implements ActivationFunction {

LIMITED_RECTIFIED_LINEAR_UNIT(
x -> Math.max(0.0, Math.min(1.0, x)),
x -> x >= 0.0 && x <= 1.0 ? 1.0 : 0.0
);

private final Function f;
private final Function outerGrad;

TestActivationFunction(Function f, Function outerGrad) {
this.f = f;
this.outerGrad = outerGrad;
}

public double f(double x) {
return f.f(x);
}

public double outerGrad(double x) {
return outerGrad.f(x);
}

interface Function {
double f(double x);
}
}
Loading

0 comments on commit 7b5a470

Please sign in to comment.