public AutomatsDesign(MainForm mainW)
 {
     InitializeComponent();
     tbox = new ToolboxAutomats(this);
     graph = new GraphAutomats();
     PICTURE_HEIGHT = this.pictureBox1.Height;
     PICTURE_WIDTH = this.pictureBox1.Width;
     IMAGE_DIMENSION = (int)((40.0f / DEFAULT_HEIGHT) * LOGICAL_SIZE.Y);
     mainAutomate = new FinitestateAutomation();
     mainWindow = mainW;
 }
        private bool simulateAnnealing(Rectangle border)
        {
            double bestEnergyExplicit = 0;
            trialsCount = trialsNumber;
            currentTemperature = startTemperature;
            graphNew = new GraphAutomats();
            currentRadius = startRadius;

            this.generateRandomStartedPoint(border);
            graphNew.copyNodesAndEdges(this);
            bestGraph = new GraphAutomats();
            bestGraph.copyNodesAndEdges(this);

            double oldStateEnergy = this.calculateEnergy();
            bestEnergy = oldStateEnergy;
            double tempEn = graphNew.calculateEnergy();
            double newStateEnergy = 0;
            bestNodes = this.nodes;
            while (trialsCount > 0 && oldStateEnergy > energyMax)
            {

                graphNew.transformIntoNeighbor();
                newStateEnergy = graphNew.calculateEnergy();
                if (newStateEnergy < bestEnergy)
                {
                    bestGraph.copyNodesAndEdges(graphNew);
                    bestEnergy = newStateEnergy;
                    bestEnergyExplicit = bestGraph.calculateEnergy();
                }
                double randomNum = randomGenerator.NextDouble();
                double boltzmanNum = Math.Exp((oldStateEnergy - newStateEnergy) / currentTemperature);
                if (randomNum < boltzmanNum)
                {
                    oldStateEnergy = newStateEnergy;
                    this.copyNodesAndEdges(graphNew);
                }
                trialsCount--;
                currentTemperature *= temperatureFactor;
                currentRadius *= (float)temperatureFactor;
            }
            double finalEnergy = bestGraph.calculateEnergy();
            bestGraph.actualizeNodesScreenPositions();
            this.nodes = bestGraph.nodes;
            this.edges = bestGraph.edges;
            return true;
        }
        private void copyNodesAndEdges(GraphAutomats patternGraph)
        {
            this.nodes.Clear();
               this.edges.Clear();
               int k = 0;
            int ind1=0,ind2=0;

            for (int i = 0; i < patternGraph.nodes.Count; i++)
            {
                Node newNode = new Node(patternGraph.nodes[i]);
                this.nodes.Add(newNode);
            }
            for (int i = 0; i < patternGraph.edges.Count; i++)
            {
                ind1 = patternGraph.nodes.IndexOf(patternGraph.edges[i].BeginNode);
                ind2 = patternGraph.nodes.IndexOf(patternGraph.edges[i].EndNode);
                Edge newEdge = new Edge(nodes[ind1], nodes[ind2], patternGraph.edges[i].EdgeChars);
                this.edges.Add(newEdge);
            }
        }