Skip to content

Commit

Permalink
Added support for NAL 8
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyLo1 committed Feb 16, 2019
1 parent d6da425 commit cc79786
Show file tree
Hide file tree
Showing 34 changed files with 569 additions and 328 deletions.
5 changes: 4 additions & 1 deletion ALANNStreams/ALANNStreams.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@
<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<None Include="App.config" />
<Compile Include="TypesPlus\Actions.fs" />
<Compile Include="TypesPlus\AsyncUtils.fs" />
<Compile Include="TypesPlus\Events.fs" />
<Compile Include="TypesPlus\Params.fs" />
<Compile Include="TypesPlus\Events.fs" />
<Compile Include="TypesPlus\Types.fs" />
<Compile Include="TypesPlus\Evidence.fs" />
<Compile Include="TypesPlus\ActivePatterns.fs" />
Expand All @@ -73,6 +74,7 @@
<Compile Include="IO\Loggers.fs" />
<Compile Include="IO\Reporting.fs" />
<Compile Include="IO\FileIO.fs" />
<Compile Include="IO\ActionExecution.fs" />
<Compile Include="Containers\SubStore.fs" />
<Compile Include="Containers\Store.fs" />
<Compile Include="Containers\EventStore.fs" />
Expand Down Expand Up @@ -101,6 +103,7 @@
<Compile Include="System\Controller.fs" />
<Compile Include="Tests\TermFormatters\TermFormattersTests.fs" />
<Compile Include="Tests\Unify\UniifyTests.fs" />
<Compile Include="Tests\Unify\SubstitutionTest.fs" />
<Compile Include="Tests\Inference\FirstOrderInfernceTests.fs" />
<Compile Include="Tests\Inference\HigherOrderInferenceTests.fs" />
<Compile Include="Tests\Evidence\EvidenceTests.fs" />
Expand Down
12 changes: 6 additions & 6 deletions ALANNStreams/Commands/CommandProcessor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ open TermFormatters
open CommandUtils
open SystemState

let showGeneralBeliefs term =
let showSimpleBeliefs term =
match getNodeFromTerm term with
| (true, node) ->
printCommandWithString "SHOW_GENERAL_BELIEFS FOR TERM" (ft term)
Expand All @@ -48,14 +48,14 @@ let showTemporalBeliefs term =
showBeliefs (List.sortBy (fun b -> -exp(b.TV)) [for b in node.Beliefs.GetTemporalBeliefs() -> b])
| _ -> printCommand "ERROR *** TERM DOES NOT EXIST ***"

let showSuperBeliefs term =
let showHypothesisBeliefs term =
match getNodeFromTerm term with
| (true, node) ->
printCommandWithString "SHOW_PRE/POST_BELIEFS FOR TERM" (ft term)
showBeliefs (List.sortBy (fun b -> -exp(b.TV)) [for b in node.Beliefs.GetSuperBeliefs() -> b])
| _ -> printCommand "ERROR *** TERM DOES NOT EXIST ***"

let showVariableBeliefs term =
let showGeneralisedBeliefs term =
match getNodeFromTerm term with
| (true, node) ->
printCommandWithString "SHOW_VARIABLE_BELIEFS FOR TERM" (ft term)
Expand Down Expand Up @@ -145,10 +145,10 @@ let reset() =
let processCommand (cmd : string) =
let cmd' = cmd.TrimStart([|'#'|])
match commandParser cmd' with
| Show_General_Beliefs(term) -> showGeneralBeliefs term
| Show_Simple_Beliefs(term) -> showSimpleBeliefs term
| Show_Temporal_Beliefs(term) -> showTemporalBeliefs term
| Show_Super_Beliefs(term) -> showSuperBeliefs term
| Show_Variable_Beliefs(term) -> showVariableBeliefs term
| Show_Hypothesis_Beliefs(term) -> showHypothesisBeliefs term
| Show_Generalised_Beliefs(term) -> showGeneralisedBeliefs term
| Show_Node(term) -> showNode(term)
| Node_Count -> nodeCount()
| Enable_Trace(term) -> enableTrace term
Expand Down
3 changes: 1 addition & 2 deletions ALANNStreams/Commands/CommandUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@

module CommandUtils

open Types
open ALANNSystem
open TermFormatters
open SystemState

let getNodeFromTerm term =
let numStreams = Params.NUM_TERM_STREAMS
let hashRoute numStreams t = abs(t.GetHashCode() % numStreams )
let hashRoute numStreams t = abs(t.GetHashCode()) % numStreams
let i = hashRoute numStreams term

systemState.stores.[i].TryGetValue(term)
Expand Down
52 changes: 28 additions & 24 deletions ALANNStreams/Containers/Store.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,75 +29,79 @@ open TermUtils
open SubStore
open System

type Store(hostTerm, generalCapacity, temporalCapacity, superCapacity ) =
let temporalStore = new SubStore(temporalCapacity) :> ISubStore
let GeneralStore = new SubStore(generalCapacity) :> ISubStore
let superStore = new SubStore(superCapacity) :> ISubStore
let variableStore = new SubStore(superCapacity) :> ISubStore

type Store(hostTerm, generalCapacity, temporalCapacity, hypothesisCapacity ) =
let simpleBeliefRanking (belief : Belief) = exp belief.TV / float32(System.Math.Pow(float(belief.Stamp.SC), Params.BELIEF_RANK_POW))
let complexBeliefRanking (belief : Belief) = exp belief.TV

let simpleStore = new SubStore(generalCapacity, simpleBeliefRanking) :> ISubStore
let temporalStore = new SubStore(temporalCapacity, complexBeliefRanking) :> ISubStore
let hypothesisStore = new SubStore(hypothesisCapacity, complexBeliefRanking) :> ISubStore
let variableStore = new SubStore(hypothesisCapacity, complexBeliefRanking) :> ISubStore

interface IStore with
member x.Contains(key) =
if key |> isSuperTerm hostTerm then
superStore.Contains key
if key |> isHypothesis then
hypothesisStore.Contains key
else if key |> containsVars then
variableStore.Contains key
else if key |> isTemporal then
temporalStore.Contains key
else
GeneralStore.Contains key
simpleStore.Contains key

member x.Insert(key, belief) =
if key |> isSuperTerm hostTerm then
superStore.Insert(key, belief)
if key |> isHypothesis then
hypothesisStore.Insert(key, belief)
else if key |> containsVars then
variableStore.Insert(key, belief)
else if key |> isTemporal then
temporalStore.Insert(key, belief)
else
GeneralStore.Insert(key, belief)
simpleStore.Insert(key, belief)

member x.Update(key, belief) =
if key |> isSuperTerm hostTerm then
superStore.Update(key, belief)
if key |> isHypothesis then
hypothesisStore.Update(key, belief)
else if key |> containsVars then
variableStore.Update(key, belief)
else if key |> isTemporal then
temporalStore.Update(key, belief)
else
GeneralStore.Update(key, belief)
simpleStore.Update(key, belief)

member x.TryGetValue key =
if key |> isSuperTerm hostTerm then
superStore.TryGetValue key
if key |> isHypothesis then
hypothesisStore.TryGetValue key
else if key |> containsVars then
variableStore.TryGetValue key
else if key |> isTemporal then
temporalStore.TryGetValue key
else
GeneralStore.TryGetValue key
simpleStore.TryGetValue key

member x.Clear() =
superStore.Clear()
hypothesisStore.Clear()
temporalStore.Clear()
GeneralStore.Clear()
simpleStore.Clear()
variableStore.Clear()

member x.Count = temporalStore.Count + GeneralStore.Count + superStore.Count + variableStore.Count
member x.Count = temporalStore.Count + simpleStore.Count + hypothesisStore.Count + variableStore.Count

member x.GetBeliefs() =
Seq.append
(superStore.GetBeliefs())
(hypothesisStore.GetBeliefs())
(temporalStore.GetBeliefs())
|> Seq.append
(GeneralStore.GetBeliefs())
(simpleStore.GetBeliefs())
|> Seq.append
(variableStore.GetBeliefs())

member x.GetSuperBeliefs() = superStore.GetBeliefs()
member x.GetSuperBeliefs() = hypothesisStore.GetBeliefs()

member x.GetTemporalBeliefs() = temporalStore.GetBeliefs()

member x.GetGeneralBeliefs() = GeneralStore.GetBeliefs()
member x.GetGeneralBeliefs() = simpleStore.GetBeliefs()

member x.GetVariableBeliefs() = variableStore.GetBeliefs()

6 changes: 2 additions & 4 deletions ALANNStreams/Containers/SubStore.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ISubStore =
abstract Count : int
abstract GetBeliefs : unit -> seq<Belief>

type SubStore(n : int) =
type SubStore(n : int, ranking : Belief -> float32) =
let q = IntervalHeap<Belief>(n) :> IPriorityQueue<Belief>
let d = HashDictionary<Key, IPriorityQueueHandle<Belief>>() :>IDictionary<Key, IPriorityQueueHandle<Belief>>

Expand All @@ -55,14 +55,12 @@ type SubStore(n : int) =
| false -> failwith "ConceptStore.Insert() : failed to remove on maxSize"
!h

let rank (belief : Belief) = exp belief.TV / float32(System.Math.Pow(float(belief.Stamp.SC), Params.BELIEF_RANK_POW))

interface ISubStore with
member x.Contains(key) = d.Contains key

member x.Insert(key, belief) =
if d.Count >= maxSize then
if rank(belief) >= rank(q.FindMin()) then
if ranking(belief) >= ranking(q.FindMin()) then
ref <| deleteMinBelief()
|> addBelief key belief
else
Expand Down
36 changes: 36 additions & 0 deletions ALANNStreams/IO/ActionExecution.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(*
* The MIT License
*
* Copyright 2018 The ALANN2018 authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*)

module ActionExecution
open Actions
open Network

let executeAction action =
match action with
| MoveLeft ->
printfn "Moving left"
sendMessageToPong("left")
| MoveRight ->
printfn "Moving right"
sendMessageToPong("right")
3 changes: 3 additions & 0 deletions ALANNStreams/IO/Reporting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ let displayAnswer (answer : string) =
| _ -> ()
| _ -> ()

let displaySolution solution =
printActor <! PrintMessage (sprintf "?%s" solution)

let updateStatus() =
myprintfn (sprintf "%sSystemTime [%d]" Params.STATUS_PREFIX (SystemTime()))
myprintfn (sprintf "%sStatus: ALANN Server Running" Params.STATUS_PREFIX)
Expand Down
2 changes: 1 addition & 1 deletion ALANNStreams/IO/TermFormatters.fs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ let rec ft t =

let formatEvent e = sprintf "%A %s %s %s %s" e.EventType (av e.AV) (match e.TV with | Some tv -> truth tv | _ -> "None") (ft e.Term) (Trail e.Stamp.Evidence)

let formatBelief (b : Belief) = sprintf "%s %s %s @%d" (truth b.TV) (ft b.Term) (Trail b.Stamp.Evidence) (b.Stamp.Created)
let formatBelief (b : Belief) = sprintf "@%d %s %s %s" (b.Stamp.Created) (truth b.TV) (ft b.Term) (Trail b.Stamp.Evidence)
Loading

0 comments on commit cc79786

Please sign in to comment.