Example #1
0
        public void MultiObjectiveFitnessWriteAndRead()
        {
            var rand = new MersenneTwisterFast(0);
            var f    = new MultiObjectiveFitness(2);
            var f2   = new MultiObjectiveFitness(2);     // We'll use this when we read the other one back in

            f2.SetObjectives(null, new [] { 0.0, 0.0 }); // setting these to zero allows us to check if they change

            // Default is to Maximize!

            // These need to be set up manually here because we are not running 'Setup'.
            // Every instance would thus normally share the same min and max values.
            f.MaxObjective  = new [] { 1.0, 1.0 };
            f.MinObjective  = new [] { 0.0, 0.0 };
            f2.MaxObjective = new[] { 1.0, 1.0 };
            f2.MinObjective = new[] { 0.0, 0.0 };

            // Set two objective (fitness) values, worst and best respectively
            f.SetObjectives(null, new [] { 0.0, 1.0 });

            // Set up some random Trials just to check that they get transmitted
            const int n = 10;

            f.Trials = new List <double>(n);
            for (var i = 0; i < n; i++)
            {
                f.Trials.Add(rand.NextDouble() * double.MaxValue); // in the half-open interval [0.0, double.MaxValue)
            }

            using (var ms = new MemoryStream())
            {
                var writer = new BinaryWriter(ms);
                f.WriteFitness(null, writer); // Write
                ms.Position = 0;
                var reader = new BinaryReader(ms);
                f2.ReadFitness(null, reader); // Read

                // Compare
                Assert.AreEqual(f.Value, f2.Value); // Value is same. This is the MAX objective value in the array

                // Just for kicks, lets make sure BOTH objectives are equal to the original values
                Assert.AreEqual(f.GetObjective(0), 0.0);
                Assert.AreEqual(f.GetObjective(0), f2.GetObjective(0));
                Assert.AreEqual(f.GetObjective(1), 1.0);
                Assert.AreEqual(f.GetObjective(1), f2.GetObjective(1));

                // And let's make sure our MAX and MIN are the same (these wouldn't normally change once established in 'Setup')
                Assert.AreEqual(f.MinObjective[0], f2.MinObjective[0]);
                Assert.AreEqual(f.MinObjective[1], f2.MinObjective[1]);
                Assert.AreEqual(f.MaxObjective[0], f2.MaxObjective[0]);
                Assert.AreEqual(f.MaxObjective[1], f2.MaxObjective[1]);

                Assert.IsTrue(f.Value <= 1.0f && f2.Value <= 1.0f); // This is the MAX objective value in the array
                Assert.IsFalse(f2.IsIdeal);                         // Fitness is not ideal by default (ideal must be defined in subclass)

                Assert.AreEqual(f2.Trials.Count, f.Trials.Count);   // Number of trials is the same
                for (var i = 0; i < f.Trials.Count; i++)
                {
                    Assert.AreEqual((double)f.Trials[i], (double)f2.Trials[i]); // Trial values are all the same
                }
            }
        }