Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PolygonFactory"/>
        /// class. The environment provided is used for default values.
        /// Optionally a custom random generator can be provided.
        /// </summary>
        /// <param name="env">The configuration.</param>
        /// <param name="r">Custom random number generator.</param>
        public PolygonFactory(EvolutionaryAlgorithm env, Random r = null)
        {
            this.environment = env;

            // Assign given generator to r or default if null
            this.r = r ?? new Random();
        }
Example #2
0
        /// <summary>
        /// Converts a little endian byte array of [ ( 4 + 8 x PolygonEdgeCount ) * PolygonCount ] bytes
        /// to a <see cref="ICanvas"/> object using settings in the given <seealso cref="EvolutionaryAlgorithm"/>.
        /// </summary>
        /// <param name="bytes">Bytes to convert</param>
        /// <param name="ea">Algorithm to get PolygonCount and PolygonEdgeCount from</param>
        /// <returns>A canvas for the given EvolutionaryAlgorithm</returns>
        public static ICanvas AsCanvas(this byte[] bytes, EvolutionaryAlgorithm ea)
        {
            // per polygon: 4B+8B*coords
            // per canvas: (4B+8B*C)*N where N=polygoncount
            var expectedPerPolygon = 4 + (8 * ea.PolygonEdgeCount);
            var expectedPerCanvas  = expectedPerPolygon * ea.PolygonCount;

            if (bytes.Length != expectedPerCanvas)
            {
                throw new ArgumentException(string.Format("expected {0} bytes, got {1}.", expectedPerCanvas, bytes.Length));
            }

            var canvas = new Canvas(ea);

            canvas.Elements = new List <IShape>();
            for (int i = 0; i < bytes.Length; i += expectedPerPolygon)
            {
                canvas.Elements.Add(bytes.Skip(i).Take(expectedPerPolygon).ToArray().AsPolygon());
            }

            return(canvas);
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Canvas"/> class. It
 /// used the given algorithm as environment to run in. The created
 /// canvas is still empty (i.e. has no shapes in it yet).
 /// </summary>
 /// <param name="environment">The context for this individual.</param>
 public Canvas(EvolutionaryAlgorithm environment)
 {
     this.Environment = environment;
 }
Example #4
0
 /// <summary>
 /// Converts a little endian byte array of [ ( 32 + 64 x PolygonEdgeCount ) * PolygonCount ] bits
 /// to a <see cref="ICanvas"/> object using settings in the given <seealso cref="EvolutionaryAlgorithm"/>.
 /// </summary>
 /// <param name="bits">Bits to convert</param>
 /// <param name="ea">Algorithm to get PolygonCount and PolygonEdgeCount from</param>
 /// <returns>A canvas for the given EvolutionaryAlgorithm</returns>
 public static ICanvas AsCanvas(this bool[] bits, EvolutionaryAlgorithm ea)
 {
     return(bits.AsByteArray().AsCanvas(ea));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="AlgorithmEvent"/>
 /// class. Contains the algorithm itself by default.
 /// </summary>
 /// <param name="current">The algorithm for this event</param>
 public AlgorithmEvent(EvolutionaryAlgorithm current)
 {
     this.Current = current;
 }