-
Notifications
You must be signed in to change notification settings - Fork 41
/
BasicCognitiveModelLiteExample.java
186 lines (157 loc) · 8.33 KB
/
BasicCognitiveModelLiteExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* File: BasicCognitiveModelLiteExample.java
* Authors: Justin Basilico
* Company: Sandia National Laboratories
* Project: Cognitive Framework Lite
*
* Copyright May 5, 2006, Sandia Corporation. Under the terms of Contract
* DE-AC04-94AL85000, there is a non-exclusive license for use of this work by
* or on behalf of the U.S. Government. Export of this program may require a
* license from the United States Government. See CopyrightHistory.txt for
* complete details.
*/
package examples;
import gov.sandia.cognition.framework.*;
import gov.sandia.cognition.framework.lite.*;
/**
* This file contains example code for how to build and use a simple Cognitive
* Model Lite programmatically. We use the simple example of driving a car where
* we use brake, left blinker, and right blinker as inputs to determine if the
* car is stopping or about to turn.
*
* @author Justin Basilico
* @since 1.0
*/
public class BasicCognitiveModelLiteExample
{
/**
* This is the main method where execution begins.
*
* @param arguments The command-line arguments.
*/
public static void main(
String[] arguments)
{
// Step 1: We need to create a semantic network that describes how our
// labeled semantic entities are connected.
// Step 1.1: Create a DefaultSemanticNetwork, which is a default
// implementation of the SemanticNetwork interface that lets you build
// up the network by adding labels to it.
DefaultSemanticNetwork semanticNetwork = new DefaultSemanticNetwork();
// Step 1.2: Create DefaultSemanticLabels for the semantic entities that
// we want to put in our semantic network.
SemanticLabel brake = new DefaultSemanticLabel("Brake");
SemanticLabel leftBlinker = new DefaultSemanticLabel("Left Blinker");
SemanticLabel rightBlinker = new DefaultSemanticLabel("Right Blinker");
SemanticLabel turning = new DefaultSemanticLabel("Turning");
SemanticLabel stopping = new DefaultSemanticLabel("Stopping");
// Step 1.3: Add them to the network.
semanticNetwork.addNode(brake);
semanticNetwork.addNode(leftBlinker);
semanticNetwork.addNode(rightBlinker);
semanticNetwork.addNode(turning);
semanticNetwork.addNode(stopping);
// Step 1.4: Now connect the semantic labels together in the network
// to show how they are associated.
semanticNetwork.setAssociation(brake, turning, 0.2);
semanticNetwork.setAssociation(brake, stopping, 1.0);
semanticNetwork.setAssociation(leftBlinker, turning, 1.0);
semanticNetwork.setAssociation(rightBlinker, turning, 1.0);
// Step 2: With the SemanticNetwork created, we now need to build a
// CognitiveModelFactory to create our CognitiveModel from. To do this
// we need to build CognitiveModuleFactory objects for each of the
// modules we want to install in the model.
// Step 2.1: Create the CognitiveModelLiteFactory that we will add the
// modules to.
CognitiveModelLiteFactory modelFactory = new CognitiveModelLiteFactory();
// Step 2.2: The first module installed in a model should be a
// perception module, which handles the conversion of an input into
// activations that the other modules can deal with. For this example
// we use a simple ArrayBasedPerceptionModule since it allows us to
// directly give the model the inputs we want. However, a custom
// perception module can be written that takes any sort of external
// perception data and converts it into activations that the model can
// use.
modelFactory.addModuleFactory(new ArrayBasedPerceptionModuleFactory());
// Step 2.3: Now that we have a perception module we can instantiate
// our SemanticNetwork in a SemanticMemory module. In order to do this
// we first need to create a PatternRecognizerLite for our network,
// which implements the core pattern recognition algorithms for the
// SharedSemanticMemoryLite. We use the SimplePatternRecognizer
// implementation as a stub. Once it is finished, a psychologically
// plausible implementation should be used instead.
PatternRecognizerLite recognizer =
new SimplePatternRecognizer(semanticNetwork);
modelFactory.addModuleFactory(
new SharedSemanticMemoryLiteFactory(recognizer));
// Step 3: Create the model from the factory.
CognitiveModelLite model = modelFactory.createModel();
// Step 4: Giving an input to the model.
// Step 4.1: Since we are using the ArrayBasedPerceptionModule, in
// order to drive the model we need to provide it with an
// ArrayBasedCognitiveModelInput, which in turn requires us to
// convert our SemanticLabels into SemanticIdentifiers.
// SemanticIdentifiers provide the model with a faster way of
// dealing with SemanticLables by assigning each one a unique
// identifier. We convert SemanticLabels to SemanticIdentifiers using
// the SemanticIdentifierMap that is on the model.
SemanticIdentifierMap idMap = model.getSemanticIdentifierMap();
SemanticIdentifier brakeID = idMap.addLabel(brake);
SemanticIdentifier leftBlinkerID = idMap.addLabel(leftBlinker);
SemanticIdentifier rightBlinkerID = idMap.addLabel(rightBlinker);
SemanticIdentifier turningID = idMap.addLabel(turning);
SemanticIdentifier stoppingID = idMap.addLabel(stopping);
// Step 4.2: Now that we have the SemanticIdentifiers, we need to
// create a ArrayBasedCognitiveModelInput with the activations that
// we are going to give.
// Here we are giving the brake a slight activation and the left
// blinker an activation of 1.0 and the right blinker an activation
// of 0.0. Note that we could leave out the right blinker and it would
// get no activation.
SemanticIdentifier[] identifiers =
new SemanticIdentifier[] { brakeID, leftBlinkerID, rightBlinkerID };
double[] inputValues = new double[] { 0.2, 1.0, 0.0 };
ArrayBasedCognitiveModelInput input =
new ArrayBasedCognitiveModelInput(identifiers, inputValues);
// Step 4.3: With the input created we now pass it into the update
// method on the model in order to update the state of the model.
// Note that we need to update twice - the first time initializes
// the model, while the second time performs the first actual state
// update. Note that the number of updates to propogate the input
// all the way through the model depends on the number of layers
// in the model.
model.update(input);
model.update(input);
// Step 5: Getting output from the model.
// Step 5.1: To get output from the model we look at the state of
// the model.
CognitiveModelState state = model.getCurrentState();
// Step 5.2: The activations of the semantic entities in the state
// are called Cogxels and are stored in the CogxelState in side the
// CognitiveModelState.
CogxelState cogxels = state.getCogxels();
// Step 5.3: The activations for the semantic entities in the
// CogxelState are stored in Cogxels. Cogxels can be retrieved directly
// to get their activations (and any other information that you want
// to associate with them through a CogxelFactory). However, we will
// make use of a convenience method for getting the activation of a
// Cogxel through the state.
double turningActivation = cogxels.getCogxelActivation(turningID);
double stoppingActivation = cogxels.getCogxelActivation(stoppingID);
// Here we look at the activations to determine if we are turning or
// stopping.
if ( turningActivation <= 0.0 && stoppingActivation <= 0.0 )
{
System.out.println("We are neither turning nor stopping.");
}
else if ( turningActivation >= stoppingActivation )
{
System.out.println("We are turning.");
}
else
{
System.out.println("We are stopping.");
}
// That's all folks!
}
}