Esempio n. 1
0
    //c indicates where the child's parent is in relation to it
    public static ShipChromosomeNode generateRandomShip(int d, ChildNode c)
    {
        ShipChromosomeNode root = new ShipChromosomeNode();

        root.depth     = d;
        root.parentPos = c;

        root.isEngine         = Random.Range(0, 2) == 1;
        root.startEngageAngle = Random.Range(0, 360);
        root.rangeEngageAngle = Random.Range(0, 360);
        root.relativeRotation = Random.Range(-Config.MAX_CHILD_ROTATION, Config.MAX_CHILD_ROTATION);


        float f;

        if (root.depth < Config.MAX_SHIP_DEPTH)
        {
            //top
            if (c != ChildNode.TOP)
            {
                f = Random.Range(0.0f, 1.0f);
                if (f <= Config.CHANCE_OF_CHILD_NODE)
                {
                    root.top = ShipChromosomeNode.generateRandomShip(root.depth + 1, ChildNode.BOTTOM);
                }
            }

            //bottom
            if (c != ChildNode.BOTTOM)
            {
                f = Random.Range(0.0f, 1.0f);
                if (f <= Config.CHANCE_OF_CHILD_NODE)
                {
                    root.bottom = ShipChromosomeNode.generateRandomShip(root.depth + 1, ChildNode.TOP);
                }
            }

            //left
            if (c != ChildNode.LEFT)
            {
                f = Random.Range(0.0f, 1.0f);
                if (f <= Config.CHANCE_OF_CHILD_NODE)
                {
                    root.left = ShipChromosomeNode.generateRandomShip(root.depth + 1, ChildNode.RIGHT);
                }
            }

            //right
            if (c != ChildNode.RIGHT)
            {
                f = Random.Range(0.0f, 1.0f);
                if (f <= Config.CHANCE_OF_CHILD_NODE)
                {
                    root.right = ShipChromosomeNode.generateRandomShip(root.depth + 1, ChildNode.LEFT);
                }
            }
        }

        return(root);
    }
Esempio n. 2
0
    public static void TreeMutate(List <ShipChromosomeNode> crossoverList)
    {
        foreach (ShipChromosomeNode s in crossoverList)
        {
            if (rnd.NextDouble() < Config.MUTATION_PROBABILITY)
            {
                List <ShipChromosomeNode> subNodes   = s.getListOfNodes();
                ShipChromosomeNode        mutateNode = selectRandomElement(subNodes, false);

                ChildNode          parentPos = mutateNode.parentPos;
                ShipChromosomeNode parent    = null;
                switch (parentPos)
                {
                case ChildNode.BOTTOM:
                    parent = mutateNode.bottom;
                    break;

                case ChildNode.LEFT:
                    parent = mutateNode.left;
                    break;

                case ChildNode.RIGHT:
                    parent = mutateNode.right;
                    break;

                case ChildNode.TOP:
                    parent = mutateNode.top;
                    break;
                }

                mutateNode = ShipChromosomeNode.generateRandomShip(mutateNode.depth, parentPos);

                switch (parentPos)
                {
                case ChildNode.BOTTOM:
                    mutateNode.bottom = parent;
                    parent.top        = mutateNode;
                    break;

                case ChildNode.LEFT:
                    mutateNode.left = parent;
                    parent.right    = mutateNode;
                    break;

                case ChildNode.RIGHT:
                    mutateNode.right = parent;
                    parent.left      = mutateNode;
                    break;

                case ChildNode.TOP:
                    mutateNode.top = parent;
                    parent.bottom  = mutateNode;
                    break;
                }
            }
        }
    }
Esempio n. 3
0
    public void generateRandomShip()
    {
        ShipChromosomeNode n = ShipChromosomeNode.generateRandomShip();

        generatePhysicalShip(n);
    }