Ejemplo n.º 1
0
        public void TestThreadedPublisher_SpatialPoolerNetwork()
        {
            Net.Network.Network network = CreateAndRunTestSpatialPoolerNetwork(0, 6);
            ILayer      l  = network.Lookup("r1").Lookup("1");
            Connections cn = l.GetConnections();

            SerialConfig    config = new SerialConfig("testThreadedPublisher_SpatialPoolerNetwork", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(cn);
            //Serialize above Connections for comparison with same run but unserialized below...
            Connections serializedConnections = api.Read <Connections>(bytes);

            Net.Network.Network network2 = CreateAndRunTestSpatialPoolerNetwork(0, 6);
            ILayer      l2      = network2.Lookup("r1").Lookup("1");
            Connections newCons = l2.GetConnections();

            //Compare the two Connections (both serialized and regular runs) - should be equal
            bool b = newCons.IsDeepEqual(serializedConnections);

            DeepCompare(newCons, serializedConnections);
            Assert.IsTrue(b);
        }
Ejemplo n.º 2
0
        public void TestThreadedPublisher_TemporalMemoryNetwork()
        {
            Net.Network.Network network = CreateAndRunTestTemporalMemoryNetwork();
            ILayer      l  = network.Lookup("r1").Lookup("1");
            Connections cn = l.GetConnections();

            SerialConfig    config = new SerialConfig("testThreadedPublisher_TemporalMemoryNetwork", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[]      bytes = api.Write(cn);
            Connections serializedConnections = api.Read <Connections>(bytes);

            Net.Network.Network network2 = CreateAndRunTestTemporalMemoryNetwork();
            ILayer      l2      = network2.Lookup("r1").Lookup("1");
            Connections newCons = l2.GetConnections();

            newCons.ShouldDeepEqual(serializedConnections);

            bool b = newCons.IsDeepEqual(serializedConnections);

            DeepCompare(newCons, serializedConnections);
            Assert.IsTrue(b);
        }
Ejemplo n.º 3
0
        private Net.Network.Network CreateAndRunTestTemporalMemoryNetwork()
        {
            Publisher manual = Publisher.GetBuilder()
                               .AddHeader("dayOfWeek")
                               .AddHeader("darr")
                               .AddHeader("B").Build();

            Sensor <ObservableSensor <String[]> > sensor = Sensor <ObservableSensor <string[]> > .Create(
                ObservableSensor <string[]> .Create, SensorParams.Create(SensorParams.Keys.Obs, new Object[] { "name", manual }));

            Parameters p = GetParameters();

            Map <String, Map <String, Object> > settings = NetworkTestHarness.SetupMap(
                null,                     // map
                20,                       // n
                0,                        // w
                0,                        // min
                0,                        // max
                0,                        // radius
                0,                        // resolution
                null,                     // periodic
                null,                     // clip
                true,                     // forced
                "dayOfWeek",              // fieldName
                "darr",                   // fieldType (dense array as opposed to sparse array or "sarr")
                "SDRPassThroughEncoder"); // encoderType

            p.SetParameterByKey(Parameters.KEY.FIELD_ENCODING_MAP, settings);

            Net.Network.Network network = Net.Network.Network.Create("test network", p)
                                          .Add(Net.Network.Network.CreateRegion("r1")
                                               .Add(Net.Network.Network.CreateLayer("1", p)
                                                    .Add(new TemporalMemory())
                                                    .Add(sensor)));

            network.Start();

            network.Observe().Subscribe(i => { }, e => Console.WriteLine(e));
            //            new Subscriber<Inference>() {
            //        @Override public void onCompleted() { }
            //    @Override public void onError(Throwable e) { e.printStackTrace(); }
            //    @Override public void onNext(Inference i) { }
            //});

            int[]   input1 = new int[] { 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0 };
            int[]   input2 = new int[] { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
            int[]   input3 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
            int[]   input4 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 };
            int[]   input5 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 };
            int[]   input6 = new int[] { 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 };
            int[]   input7 = new int[] { 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 };
            int[][] inputs = { input1, input2, input3, input4, input5, input6, input7 };

            // Run until TemporalMemory is "warmed up".
            int timeUntilStable = 602;

            for (int j = 0; j < timeUntilStable; j++)
            {
                for (int i = 0; i < inputs.Length; i++)
                {
                    manual.OnNext(Arrays.ToString(inputs[i]));
                }
            }

            manual.OnComplete();

            ILayer l = network.Lookup("r1").Lookup("1");

            try
            {
                l.GetLayerThread().Wait();

                Console.WriteLine(Arrays.ToString(SDR.AsCellIndices(l.GetConnections().GetActiveCells())));
            }
            catch (Exception e)
            {
                Assert.AreEqual(typeof(ThreadInterruptedException), e.GetType());
            }

            return(network);
        }