Exemplo n.º 1
0
 public Spawner(float distInterval, int expectCount, int fullCount, SpawnFunction function)
 {
     this.distInterval = distInterval;
     this.expectCount  = expectCount;
     this.fullCount    = fullCount;
     this.function     = function;
     actualCount       = 0;
     intervalCount     = 0;
     dist = 0f;
 }
Exemplo n.º 2
0
        public GeneticSimulation(FitnessFunction <TGenome> fitnessFunction, SpawnFunction <TGenome> spawnFunction, int populationSize)
        {
            _population = new List <Organism <TGenome> >();
            _rand       = new Random();

            _fitnessFunction = fitnessFunction;

            for (int i = 0; i < populationSize; i++)
            {
                _population.Add(spawnFunction(i));
            }
        }
Exemplo n.º 3
0
        public void SpawnSimpleProcess()
        {
            AutoResetEvent handle = new AutoResetEvent(false);
            int            count  = 0;
            IFunction      func   = new SpawnFunction();
            IFunction      lambda = new LambdaFunction(() => { count = 1; return(handle.Set()); });

            var result = func.Apply(null, new object[] { lambda });

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Process));

            handle.WaitOne();

            Assert.AreEqual(1, count);
        }
Exemplo n.º 4
0
        public void Sin()
        {
            var rand = new Random();
            var net  = NeuralNetBuilder.From(new NeuralNetConfig()
            {
                InputLabels            = new string[] { "x" },
                OutputLabels           = new string[] { "y" },
                HiddenLayerCount       = 3,
                HiddenLayerNeuronCount = 3,
                //PreActivationFunction = (i, d) =>
                ActivationFunction = d => d > .5 ? 1 : 0
                                     //ActivationFunction = d => 1 / (1 + Math.Pow(Math.E, -d))
            });
            var trainingData = new List <Tuple <double, double> >();

            for (double i = 0; i < Math.PI * 2; i += .1)
            {
                var input  = i;
                var output = Math.Sin(i);
                //var normalizedOutput = output / Math.PI * 2;
                var normalizedOutput = .5 * (output + 1);
                var normalizedInput  = input / (Math.PI * 2);
                trainingData.Add(new Tuple <double, double>(normalizedInput, normalizedOutput));
            }

            var weightCount      = net.WeightCount;
            var biasCount        = net.BiasCount;
            var weightOrgSpawner = new SpawnFunction <double[]>(index =>
            {
                var data = new double[weightCount + biasCount];
                for (int i = 0; i < weightCount + biasCount; i++)
                {
                    var scale = i >= weightCount ? 1 : 1;
                    data[i]   = (rand.NextDouble()) * scale;
                }

                return(new Org(data));
            });

            var fitnessFunction = new FitnessFunction <double[]>(org =>
            {
                net.InitializeWeights(org.Genome.Take(weightCount).ToArray());

                net.InitializeBiases(org.Genome.Skip(weightCount).ToArray());

                var errorSum = trainingData.Select(trainingElement =>
                {
                    var input          = trainingElement.Item1;
                    var expectedOutput = trainingElement.Item2;

                    var actualOutput = net.Compute(new NamedValue[] { new NamedValue()
                                                                      {
                                                                          Name = "x", Value = input
                                                                      } });

                    var error          = expectedOutput - actualOutput[0].Value;
                    var convertedError = Math.Pow((Math.Abs(error) + 1), 2);

                    return(convertedError);
                }).Sum();
                //Trace.WriteLine(errorSum);
                return(1 / errorSum);
            });

            var geneticSim = new GeneticSimulation <double[]>(fitnessFunction, weightOrgSpawner, 50);

            var bestSet = geneticSim.Evolve(20000, false);
            var theBest = geneticSim.GetBest();

            net.InitializeWeights(theBest.Genome.Take(weightCount).ToArray());
            net.InitializeBiases(theBest.Genome.Skip(weightCount).ToArray());

            Trace.WriteLine("OUTPUT");
            trainingData.ForEach(t =>
            {
                var input          = t.Item1;
                var expectedOutput = t.Item2;

                var actualOutput = net.Compute(new NamedValue[] { new NamedValue()
                                                                  {
                                                                      Name = "x", Value = input
                                                                  } });

                var error = expectedOutput - actualOutput[0].Value;

                //Trace.WriteLine($"INPUT:{input} EXPECT:{expectedOutput} REAL:{actualOutput[0].Value} ERR:{error}");
                //Trace.WriteLine($"{input},{expectedOutput},{actualOutput[0].Value}");
                Trace.WriteLine($"{actualOutput[0].Value}");
            });
        }
Exemplo n.º 5
0
 public void RegisterSpawnable(EventType type, SpawnFunction spawner)
 {
     spawnableEvents.Add(type, spawner);
 }
Exemplo n.º 6
0
    public static void SpawnObjects(int amount, float radius, float clusterDist, Vector3 position, GameObject[] objs, SpawnFunction create, LayerMask mask)
    {
        if (amount <= 0)
        {
            return;
        }

        float clusterDistSqrd = clusterDist * clusterDist;
        int   created         = 0;

        do
        {
            Vector3 spawnPoint = new Vector3(UnityEngine.Random.Range(-radius, radius) + position.x, 0f, UnityEngine.Random.Range(-radius, radius) + position.z);
            spawnPoint.x = Mathf.Clamp(spawnPoint.x, -GenerateLevel.xx, GenerateLevel.xx);
            spawnPoint.z = Mathf.Clamp(spawnPoint.z, -GenerateLevel.zz, GenerateLevel.zz);
            Vector3 nearestLoc;

            if (!Physics.CheckSphere(spawnPoint, 2, mask))
            {
                if (objs[0] == null)
                {
                    objs[created] = create(spawnPoint);
                    NetworkServer.Spawn(objs[created]);
                    created++;
                }
                else
                {
                    nearestLoc = NearestTarget(objs, spawnPoint);
                    if ((nearestLoc - spawnPoint).sqrMagnitude > clusterDistSqrd)
                    {
                        objs[created] = create(spawnPoint);
                        NetworkServer.Spawn(objs[created]);
                        created++;
                    }
                }
            }
        }while(created < amount);
    }