public void DetectorBasicTest()
        {
            //Test data
            var x = new List <double> {
                1, 0.1, 100, 1.1, 0.5, 0.3
            };
            var y = new List <double> {
                1.1, 0.3, 1.1, 0.7, 10, 0.17
            };
            var z = new List <double> {
                -11, -0.7, 1.2, -1.7, 1.8, 0.2
            };

            var amplitudes = new List <double> {
                1, 1.1, 2, 0.7, 0.2, 1.8
            };
            var traveledDistances = new List <double> {
                2, 1, 2.3, 0.9, 0.1, 2.35
            };

            var detectorDistance = 1.0;
            var detectorWidth    = 0.5;

            //Expected Results
            var expectedAmplitude         = 1.7;
            var expectedTraveledDistances = new List <double> {
                2, 0.9
            };

            var detectorData = Photons.DetectorPhotons(x, y, z, traveledDistances, amplitudes, detectorDistance, detectorWidth);

            Assert.Equal(expectedAmplitude, detectorData.TotalAmplitude);
            Assert.Equal(expectedTraveledDistances, detectorData.TraveledDistances);
        }
예제 #2
0
 public void Stop()
 {
     CurrentTime = -1;
     board       = boardBackup;
     boardBackup = null;
     Photons.Clear();
 }
예제 #3
0
    public void Start(int seed)
    {
        // Bookkeeping
        CurrentTime  = 0;
        nextPhotonId = 0;
        Photons.Clear();

        // Get input/output from script
        System.Random random  = new System.Random(seed);
        DynValue      iovalue = luaEnvironment.Call(
            luaGetIO, (Func <int, int>)((max) => random.Next(max))
            );

        input  = iovalue.Tuple[0].ToObject <List <int> >();
        golden = iovalue.Tuple[1].ToObject <List <int> >();
        output = new List <int>();

        // Save the original state of board
        boardBackup = board.Clone() as Cell[, ];

        // Find out where input is
        FindCell((x, y, cell) => cell.type == CellType.INPUT,
                 (x, y, cell) =>
        {
            inputPosition  = new Vector2Int(x, y);
            inputDirection = DirectionIdToVector(cell.param);
        });
        //Debug.Log($"Input coordinate: ({inputX}, {inputY})");

        // Generate first input Photon
        GeneratePhoton(input[0], inputPosition, inputDirection);
    }
        public void AreWeDoneBasicTest()
        {
            int detector1 = 9999, detector2 = 9999;

            Assert.True(Photons.AreWeDone(detector1, detector2));

            detector1 = detector2 = 10000;

            Assert.False(Photons.AreWeDone(detector1, detector2));
        }
예제 #5
0
 private void GeneratePhoton(int value, Vector2Int position, Vector2Int dir)
 {
     Photons.Add(++nextPhotonId, new Photon
     {
         value     = value,
         position  = position,
         direction = dir
     });
     //Debug.Log($"Generated Photon id {nextPhotonId}: {Photons[nextPhotonId]}");
 }
        public void FilterPhotonOutOfSkinTest()
        {
            var x = new List <double> {
                1, 0.1, 100, 1.5, 0.5, 0.3
            };
            var y = new List <double> {
                1.5, 0.3, 1.1, 0.7, 10, 0.17
            };
            var z = new List <double> {
                11, -0.7, 1.2, 1.7, 1.8, 0.2
            };

            var xyBound = 1.0;
            var zBound  = 1.0;

            var results = Photons.FilterPhotons(x, y, z, xyBound, zBound);

            Assert.DoesNotContain(1, results);
            Assert.Contains(5, results);
            Assert.True(results.Count == 1);
        }
        public void DetectorNegativeDistanceOrWidthTest()
        {
            //Test data
            var x = new List <double> {
                1, 0.1, 100, 1.1, 0.5, 0.3
            };
            var y = new List <double> {
                1.1, 0.3, 1.1, 0.7, 10, 0.17
            };
            var z = new List <double> {
                -11, -0.7, 1.2, -1.7, 1.8, 0.2
            };

            var amplitudes = new List <double> {
                1, 1.1, 2, 0.7, 0.2, 1.8
            };
            var traveledDistances = new List <double> {
                2, 1, 2.3, 0.9, 0.1, 2.35
            };

            var detectorDistance = -1.0;
            var detectorWidth    = 0.5;

            Assert.Throws <ArgumentOutOfRangeException>(() => Photons.DetectorPhotons(x, y, z, traveledDistances,
                                                                                      amplitudes, detectorDistance, detectorWidth));

            detectorDistance = 1.0;
            detectorWidth    = -0.5;

            Assert.Throws <ArgumentOutOfRangeException>(() => Photons.DetectorPhotons(x, y, z, traveledDistances,
                                                                                      amplitudes, detectorDistance, detectorWidth));

            detectorDistance = -1.0;
            detectorWidth    = -0.5;

            Assert.Throws <ArgumentOutOfRangeException>(() => Photons.DetectorPhotons(x, y, z, traveledDistances,
                                                                                      amplitudes, detectorDistance, detectorWidth));
        }
        public void DetectorDifferentListLengthTest()
        {
            var x = new List <double> {
                1, 2, 3, 4
            };
            var y = new List <double> {
                1, 2, 3
            };
            var z = new List <double> {
                1, 2, 3
            };

            var amplitudes = new List <double> {
                1, 2, 3
            };
            var traveledDistances = new List <double> {
                1, 2, 3
            };

            Assert.Throws <ArgumentException>(() => Photons.DetectorPhotons(x, y, z, traveledDistances, amplitudes, 1,
                                                                            1));

            x = new List <double> {
                1, 2, 3
            };
            y = new List <double> {
                1, 2, 3, 4
            };
            z = new List <double> {
                1, 2, 3
            };

            amplitudes = new List <double> {
                1, 2, 3
            };
            traveledDistances = new List <double> {
                1, 2, 3
            };

            Assert.Throws <ArgumentException>(() => Photons.DetectorPhotons(x, y, z, traveledDistances, amplitudes, 1,
                                                                            1));

            x = new List <double> {
                1, 2, 3
            };
            y = new List <double> {
                1, 2, 3
            };
            z = new List <double> {
                1, 2, 3, 4
            };

            amplitudes = new List <double> {
                1, 2, 3
            };
            traveledDistances = new List <double> {
                1, 2, 3
            };

            Assert.Throws <ArgumentException>(() => Photons.DetectorPhotons(x, y, z, traveledDistances, amplitudes, 1,
                                                                            1));

            x = new List <double> {
                1, 2, 3
            };
            y = new List <double> {
                1, 2, 3
            };
            z = new List <double> {
                1, 2, 3
            };

            amplitudes = new List <double> {
                1, 2, 3, 4
            };
            traveledDistances = new List <double> {
                1, 2, 3
            };

            Assert.Throws <ArgumentException>(() => Photons.DetectorPhotons(x, y, z, traveledDistances, amplitudes, 1,
                                                                            1));

            x = new List <double> {
                1, 2, 3
            };
            y = new List <double> {
                1, 2, 3
            };
            z = new List <double> {
                1, 2, 3
            };

            amplitudes = new List <double> {
                1, 2, 3
            };
            traveledDistances = new List <double> {
                1, 2, 3, 4
            };

            Assert.Throws <ArgumentException>(() => Photons.DetectorPhotons(x, y, z, traveledDistances, amplitudes, 1,
                                                                            1));
        }
예제 #9
0
    public void Step()
    {
        if (CurrentTime < 0)
        {
            return;
        }
        ++CurrentTime;

        // Do emulation
        foreach (int id in Photons.Keys.ToList())
        {
            if (!Photons.ContainsKey(id))
            {
                continue;
            }
            Photon p = Photons[id];

            // update position
            p.Advance();

            if (!IsInBound(p.position))
            {
                Photons.Remove(id);
            }
            else
            {
                // Find out what is there
                Cell there = this[p.position];
                switch (there.type)
                {
                case CellType.EMPTY:
                    break;

                case CellType.MIRROR:
                    switch (there.param)
                    {
                    case 0:                                     // -
                        if (p.direction.y == 0)
                        {
                            Photons.Remove(id);
                        }
                        else
                        {
                            p.Reflect();
                        }
                        break;

                    case 1:                                     // /
                        if (p.direction.y == 0)
                        {
                            p.TurnLeft();
                        }
                        else
                        {
                            p.TurnRight();
                        }
                        break;

                    case 2:                                     // |
                        if (p.direction.x == 0)
                        {
                            Photons.Remove(id);
                        }
                        else
                        {
                            p.Reflect();
                        }
                        break;

                    case 3:                                     // \ 
                        if (p.direction.x == 0)
                        {
                            p.TurnLeft();
                        }
                        else
                        {
                            p.TurnRight();
                        }
                        break;
                    }
                    break;

                case CellType.GENERATOR:
                    GeneratePhoton(there.param, p.position, p.direction);
                    p.Reflect();
                    break;

                case CellType.SLUICE:
                    p.SetDirection(DirectionIdToVector(there.param));
                    break;

                case CellType.PROCESS:
                    GeneratePhoton(p.value, p.position, DirRotateCCW90(p.direction));
                    GeneratePhoton(p.value, p.position, DirRotateCW90(p.direction));
                    Photons.Remove(id);
                    break;

                case CellType.TARPIT:
                    // I am stuck in the pit
                    if (p.direction == Vector2Int.zero)
                    {
                        break;
                    }
                    // I come to the pit
                    if (there.tarpitId == 0)
                    {
                        // No others are in the pit
                        p.SetDirection(Vector2Int.zero);                                 // I'm stuck
                        this[p.position] = there.SetTarpitId(id);
                    }
                    else
                    {
                        // Someone is in the pit
                        int thereValue = Photons[there.tarpitId].value;
                        int newValue;
                        switch (there.param)
                        {
                        case 0: newValue = thereValue + p.value; break;

                        case 1: newValue = thereValue - p.value; break;

                        case 2: newValue = thereValue * p.value; break;

                        case 3: newValue = p.value == 0 ? 0 : thereValue / p.value; break;

                        case 4: newValue = p.value == 0 ? 0 : thereValue % p.value; break;

                        default: newValue = 0; break;
                        }
                        GeneratePhoton(newValue, p.position, p.direction);
                        Photons.Remove(id);
                        Photons.Remove(there.tarpitId);
                        this[p.position] = there.SetTarpitId(0);
                        //Debug.Log($"Photon id {there.tarpitId} in tarpit removed");
                    }
                    break;

                case CellType.INPUT:
                    Photons.Remove(id);
                    break;

                case CellType.OUTPUT:
                    output.Add(p.value);
                    Photons.Remove(id);
                    break;

                case CellType.WALL:
                    Photons.Remove(id);
                    break;
                }
            }

            if (Photons.ContainsKey(id))
            {
                Photons[id] = p;
                //Debug.Log($"Photon id {id} updated to {p}");
            }
            else
            {
                //Debug.Log($"Photon id {id} removed");
            }
        }

        // Generate photon from input
        if (CurrentTime % InputSpeed == 0)
        {
            int id = InputIndex;
            if (id < input.Count)
            {
                GeneratePhoton(input[id], inputPosition, inputDirection);
            }
        }
    }