Esempio n. 1
0
        public override void Execute(Interpreter inI)
        {
            IntStack    istack = inI.IntStack();
            ObjectStack estack = inI.ExecStack();

            if (_stack.Size() > 0 && istack.Size() > 0)
            {
                if (istack.Top() > 0)
                {
                    int    stop    = istack.Pop() - 1;
                    object bodyObj = _stack.Pop();
                    try
                    {
                        Program doRangeMacroProgram = new Program();
                        doRangeMacroProgram.Push(0);
                        doRangeMacroProgram.Push(stop);
                        doRangeMacroProgram.Push("exec.do*range");
                        doRangeMacroProgram.Push(bodyObj);
                        estack.Push(doRangeMacroProgram);
                    }
                    catch (Exception)
                    {
                        Console.Error.WriteLine("Error while initializing a program.");
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Reflect all atoms (indexes) int he provided stack around the line formed
        /// of the beg and end atoms.
        /// </summary>
        /// <param name="stack">atom indexes to reflect</param>
        /// <param name="beg">beg atom of a bond</param>
        /// <param name="end">end atom of a bond</param>
        private void Reflect(IntStack stack, IAtom beg, IAtom end)
        {
            var begP = beg.Point2D.Value;
            var endP = end.Point2D.Value;

            Reflect(stack, begP, endP);
        }
Esempio n. 3
0
        // Begin exec iteration functions
        public override void Execute(Interpreter inI)
        {
            IntStack    istack = inI.IntStack();
            ObjectStack estack = inI.ExecStack();

            if (_stack.Size() > 0 && istack.Size() > 1)
            {
                int    stop  = istack.Pop();
                int    start = istack.Pop();
                object code  = _stack.Pop();
                if (start == stop)
                {
                    istack.Push(start);
                    estack.Push(code);
                }
                else
                {
                    istack.Push(start);
                    start = (start < stop) ? (start + 1) : (start - 1);
                    // trh//Made changes to correct errors with code.do*range
                    try {
                        Program recursiveCallProgram = new Program();
                        recursiveCallProgram.Push(start);
                        recursiveCallProgram.Push(stop);
                        recursiveCallProgram.Push("exec.do*range");
                        recursiveCallProgram.Push(code);
                        estack.Push(recursiveCallProgram);
                    } catch (Exception) {
                        Console.Error.WriteLine("Error while initializing a program.");
                    }
                    estack.Push(code);
                }
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Restore the coordinates of atoms (indexes) in the stack to the provided
 /// source.
 /// </summary>
 /// <param name="stack">atom indexes to backup</param>
 /// <param name="src">source of coordinates</param>
 private void RestoreCoords(IntStack stack, Vector2[] src)
 {
     for (int i = 0; i < stack.len; i++)
     {
         var v = stack.xs[i];
         atoms[v].Point2D = new Vector2(src[v].X, src[v].Y);
     }
 }
Esempio n. 5
0
 /// <summary>
 /// Backup the coordinates of atoms (indexes) in the stack to the provided
 /// destination.
 /// </summary>
 /// <param name="dest">destination</param>
 /// <param name="stack">atom indexes to backup</param>
 private void BackupCoords(Vector2[] dest, IntStack stack)
 {
     for (int i = 0; i < stack.len; i++)
     {
         var v = stack.xs[i];
         dest[v] = new Vector2(atoms[v].Point2D.Value.X, atoms[v].Point2D.Value.Y);
     }
 }
Esempio n. 6
0
        public void Test1()
        {
            IntStack x = new IntStack(3);

            x.Push(1);
            x.Push(2);
            Assert.Equal(false, x.IsEmpty());
            Assert.Equal(2, x.Pop());
        }
Esempio n. 7
0
        public override void Execute(Interpreter inI)
        {
            ObjectStack codeStack = inI.CodeStack();
            IntStack    iStack    = inI.IntStack();

            if (iStack.Size() > 0)
            {
                codeStack.Push(iStack.Pop());
            }
        }
Esempio n. 8
0
        protected void setUp()
        {
            interpreter = new Interpreter();
            Program instructionList = new Program("( )");

            interpreter.randProgram.SetInstructions(interpreter, instructionList);
            istack = new IntStack();
            fstack = new FloatStack();
            bstack = new BooleanStack();
        }
Esempio n. 9
0
        private void Reflect(IntStack stack, Vector2 begP, Vector2 endP)
        {
            double dx = endP.X - begP.X;
            double dy = endP.Y - begP.Y;

            double a = (dx * dx - dy * dy) / (dx * dx + dy * dy);
            double b = 2 * dx * dy / (dx * dx + dy * dy);

            for (int i = 0; i < stack.len; i++)
            {
                Reflect(atoms[stack.xs[i]], begP, a, b);
            }
        }
Esempio n. 10
0
        public override void Execute(Interpreter inI)
        {
            IntStack iStack = inI.IntStack();

            if (iStack.Size() > 0)
            {
                int index = iStack.Pop();
                if (_stack.Size() > 0)
                {
                    _stack.YankDup(index);
                }
                else
                {
                    iStack.Push(index);
                }
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Create a new layout refiner for the provided molecule.
        /// </summary>
        /// <param name="mol">molecule to refine</param>
        internal LayoutRefiner(IAtomContainer mol, ISet <IAtom> afix, ISet <IBond> bfix)
        {
            this.mol     = mol;
            this.afix    = afix;
            this.bfix    = bfix;
            this.bondMap = EdgeToBondMap.WithSpaceFor(mol);
            this.adjList = GraphUtil.ToAdjList(mol, bondMap);
            this.idxs    = new Dictionary <IAtom, int>();
            foreach (var atom in mol.Atoms)
            {
                idxs[atom] = idxs.Count;
            }
            this.atoms = mol.Atoms.ToArray();

            // buffers for storing coordinates
            this.buffer1 = new Vector2[atoms.Length];
            this.buffer2 = new Vector2[atoms.Length];
            this.backup  = new Vector2[atoms.Length];
            for (int i = 0; i < buffer1.Length; i++)
            {
                buffer1[i] = new Vector2();
                buffer2[i] = new Vector2();
                backup[i]  = new Vector2();
            }
            this.stackBackup = new IntStack(atoms.Length);
            this.visited     = new bool[atoms.Length];

            this.congestion = new Congestion(mol, adjList);

            // note, this is lazy so only does the shortest path when needed
            // and does |V| search at maximum
            this.apsp = new AllPairsShortestPaths(mol);

            // index ring systems, idx -> ring system number (rnum)
            int rnum = 1;

            this.ringsystems = new int[atoms.Length];
            for (int i = 0; i < atoms.Length; i++)
            {
                if (atoms[i].IsInRing && ringsystems[i] == 0)
                {
                    TraverseRing(ringsystems, i, rnum++);
                }
            }
        }
Esempio n. 12
0
    static void Main(string[] args)
    {
        // create a new IntStack
        IntStack stack = new IntStack();

        stack.Push(2);
        stack.Push(4);
        stack.Push(8);

        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Pop value: {0}", stack.Pop());
        }

        // wait for input before exiting
        Console.WriteLine("Press enter to finish");
        Console.ReadLine();
    }
Esempio n. 13
0
        public override void Execute(Interpreter inI)
        {
            IntStack istack = inI.IntStack();

            if (istack.Size() > 0 && _stack.Size() > 0)
            {
                int index = istack.Pop();
                if (index < 0)
                {
                    index = 0;
                }
                if (index >= _stack.Size())
                {
                    index = _stack.Size() - 1;
                }
                inI.GetInputPusher().PushInput(inI, index);
            }
        }
Esempio n. 14
0
        public override void Execute(Interpreter inI)
        {
            IntStack    istack = inI.IntStack();
            ObjectStack estack = inI.ExecStack();

            if (_stack.Size() > 0 && istack.Size() > 0)
            {
                if (istack.Top() > 0)
                {
                    object bodyObj = _stack.Pop();
                    if (bodyObj is Program)
                    {
                        // insert integer.pop in front of program
                        ((Program)bodyObj).Shove("integer.pop", ((Program)bodyObj)._size);
                    }
                    else
                    {
                        // create a new program with integer.pop in front of
                        // the popped object
                        Program newProgram = new Program();
                        newProgram.Push("integer.pop");
                        newProgram.Push(bodyObj);
                        bodyObj = newProgram;
                    }
                    int stop = istack.Pop() - 1;
                    try
                    {
                        Program doRangeMacroProgram = new Program();
                        doRangeMacroProgram.Push(0);
                        doRangeMacroProgram.Push(stop);
                        doRangeMacroProgram.Push("code.quote");
                        doRangeMacroProgram.Push(bodyObj);
                        doRangeMacroProgram.Push("code.do*range");
                        estack.Push(doRangeMacroProgram);
                    }
                    catch (Exception)
                    {
                        Console.Error.WriteLine("Error while initializing a program.");
                    }
                }
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Resolves conflicts either by bending bonds or stretching bonds in the
        /// shortest path between an overlapping pair. Bending and stretch are tried
        /// for each pair and the best resolution is used.
        /// </summary>
        /// <param name="pairs">pairs</param>
        private void BendOrStretch(IEnumerable <AtomPair> pairs)
        {
            // without checking which bonds have been bent/stretch already we
            // could end up repeating a lot of repeated work to no avail
            var bendVisit    = new Dictionary <IBond, AtomPair>();
            var stretchVisit = new Dictionary <IBond, AtomPair>();

            var bendStack    = new IntStack(atoms.Length);
            var stretchStack = new IntStack(atoms.Length);

            foreach (var pair in pairs)
            {
                double score = congestion.Score();

                // each attempt will be more aggressive/distorting
                for (pair.attempt = 1; pair.attempt <= 3; pair.attempt++)
                {
                    bendStack.Clear();
                    stretchStack.Clear();

                    // attempt both bending and stretching storing the
                    // best result in the provided buffer
                    var bendScore    = Bend(pair, bendStack, buffer1, bendVisit);
                    var stretchScore = Stretch(pair, stretchStack, buffer2, stretchVisit);

                    // bending is better than stretching
                    if (bendScore < stretchScore && bendScore < score)
                    {
                        RestoreCoords(bendStack, buffer1);
                        congestion.Update(bendStack.xs, bendStack.len);
                        break;
                    }
                    // stretching is better than bending
                    else if (bendScore > stretchScore && stretchScore < score)
                    {
                        RestoreCoords(stretchStack, buffer2);
                        congestion.Update(stretchStack.xs, stretchStack.len);
                        break;
                    }
                }
            }
        }
Esempio n. 16
0
        public override float EvaluateTestCase(GAIndividual inIndividual, object inInput, object inOutput)
        {
            _interpreter.ClearStacks();
            int      currentInput = (int)inInput;
            IntStack stack        = _interpreter.IntStack();

            stack.Push(currentInput);
            // Must be included in order to use the input stack.
            _interpreter.InputStack().Push(currentInput);
            _interpreter.Execute(((PushGPIndividual)inIndividual)._program, _executionLimit);
            int result = stack.Top();

            // System.out.println( _interpreter + " " + result );
            // Penalize individual if there is no result on the stack.
            if (stack.Size() == 0)
            {
                return(_noResultPenalty);
            }
            return(result - ((int)inOutput));
        }
Esempio n. 17
0
        public void PushInput(Interpreter inI, int n)
        {
            ObjectStack _stack = inI.InputStack();

            if (_stack.Size() > n)
            {
                object inObject = _stack.DeepPeek(n);
                if (inObject is int)
                {
                    IntStack istack = inI.IntStack();
                    istack.Push((int)inObject);
                }
                else
                {
                    // if (inObject is Number)
                    // {
                    //   FloatStack fstack = inI.FloatStack();
                    //   fstack.Push(((Number)inObject).FloatValue());
                    // }
                    //else
                    if (inObject is float)
                    {
                        FloatStack fstack = inI.FloatStack();
                        fstack.Push((float)inObject);
                    }
                    else
                    {
                        if (inObject is bool)
                        {
                            BooleanStack bstack = inI.BoolStack();
                            bstack.Push((bool)inObject);
                        }
                        else
                        {
                            Console.Error.WriteLine("Error during input.index - object " + inObject.GetType() +
                                                    " is not a legal object according to " + this.GetType() + ".");
                        }
                    }
                }
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Stretch the distance between beg and end, moving all atoms provided in
        /// the stack.
        /// </summary>
        /// <param name="stack">atoms to be moved</param>
        /// <param name="beg">begin atom of a bond</param>
        /// <param name="end">end atom of a bond</param>
        /// <param name="amount">amount to try stretching by (absolute)</param>
        private void Stretch(IntStack stack, IAtom beg, IAtom end, double amount)
        {
            var begPoint = beg.Point2D.Value;
            var endPoint = end.Point2D.Value;

            if (Vector2.Distance(begPoint, endPoint) + amount > MaxBondLength)
            {
                return;
            }

            var vector = new Vector2(endPoint.X - begPoint.X, endPoint.Y - begPoint.Y);

            vector  = Vector2.Normalize(vector);
            vector *= amount;

            for (int i = 0; i < stack.len; i++)
            {
                var atom = atoms[stack.xs[i]];
                atom.Point2D = atom.Point2D.Value + vector;
            }
        }
Esempio n. 19
0
    static void Main(string[] args)
    {
        // create an instance from the derived type
        IntStack intStack = new IntStack();

        // upcast to the base type
        GenericStack <int> gStack = intStack;

        // push some data into the stack
        intStack.Push(1);
        intStack.Push(2);
        intStack.Push(3);

        // pop the data back out of the stack
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Popped Value: {0}", intStack.Pop());
        }

        // wait for input before exiting
        Console.WriteLine("Press enter to finish");
        Console.ReadLine();
    }
Esempio n. 20
0
        public override void Execute(Interpreter inI)
        {
            IntStack stack = inI.IntStack();

            stack.Push(_stack.Size());
        }
Esempio n. 21
0
        /// <summary>
        /// Stretch all bonds in the shortest path between a pair of atoms in an
        /// attempt to resolve the overlap. The stretch that produces the minimum
        /// congestion is stored in the provided stack and coordinates with the congestion
        /// score returned.
        /// </summary>
        /// <param name="pair">congested atom pair</param>
        /// <param name="stack">best result vertices</param>
        /// <param name="coords">best result coordinates</param>
        /// <param name="firstVisit">visit map to avoid repeating work</param>
        /// <returns>congestion score of best result</returns>
        private double Stretch(AtomPair pair, IntStack stack, Vector2[] coords, Dictionary <IBond, AtomPair> firstVisit)
        {
            stackBackup.Clear();

            var score = congestion.Score();
            var min   = score;

            foreach (var bond in pair.bndAt)
            {
                // don't stretch ring bonds
                if (bond.IsInRing)
                {
                    continue;
                }
                if (bfix.Contains(bond))
                {
                    continue;
                }

                // has this bond already been tested as part of another pair
                if (!firstVisit.TryGetValue(bond, out AtomPair first))
                {
                    firstVisit[bond] = first = pair;
                }
                if (first != pair)
                {
                    continue;
                }

                var beg         = bond.Begin;
                var end         = bond.End;
                var begIdx      = idxs[beg];
                var endIdx      = idxs[end];
                var begPriority = beg.GetProperty <int>(AtomPlacer.Priority);
                var endPriority = end.GetProperty <int>(AtomPlacer.Priority);

                Arrays.Fill(visited, false);
                if (begPriority < endPriority)
                {
                    stack.len = Visit(visited, stack.xs, endIdx, begIdx, 0);
                }
                else
                {
                    stack.len = Visit(visited, stack.xs, begIdx, endIdx, 0);
                }

                BackupCoords(backup, stack);
                if (begPriority < endPriority)
                {
                    Stretch(stack, end, beg, pair.attempt * StrechStep);
                }
                else
                {
                    Stretch(stack, beg, end, pair.attempt * StrechStep);
                }

                congestion.Update(visited, stack.xs, stack.len);

                if (PercDiff(score, congestion.Score()) >= ImprovementPrecThreshold && congestion.Score() < min)
                {
                    BackupCoords(coords, stack);
                    min = congestion.Score();
                    stackBackup.CopyFrom(stack);
                }

                RestoreCoords(stack, backup);
                congestion.Update(visited, stack.xs, stack.len);
                congestion.score = score;
            }

            stack.CopyFrom(stackBackup);

            return(min);
        }
Esempio n. 22
0
        /// <summary>
        /// Bend all bonds in the shortest path between a pair of atoms in an attempt
        /// to resolve the overlap. The bend that produces the minimum congestion is
        /// stored in the provided stack and coords with the congestion score
        /// returned.
        /// </summary>
        /// <param name="pair">congested atom pair</param>
        /// <param name="stack">best result vertices</param>
        /// <param name="coords">best result coords</param>
        /// <param name="firstVisit">visit map to avoid repeating work</param>
        /// <returns>congestion score of best result</returns>
        private double Bend(AtomPair pair, IntStack stack, Vector2[] coords, Dictionary <IBond, AtomPair> firstVisit)
        {
            stackBackup.Clear();

            Trace.Assert(stack.len == 0);
            double score = congestion.Score();
            double min   = score;

            // special case: if we have an even length path where the two
            // most central bonds are cyclic but the next two aren't we bend away
            // from each other
            if (pair.bndAt.Count > 4 && (pair.bndAtCode & 0x1F) == 0x6)
            {
                var bndA = pair.bndAt[2];
                var bndB = pair.bndAt[3];

                if (bfix.Contains(bndA) || bfix.Contains(bndB))
                {
                    return(int.MaxValue);
                }

                var pivotA = GetCommon(bndA, pair.bndAt[1]);
                var pivotB = GetCommon(bndB, pair.bndAt[0]);

                if (pivotA == null || pivotB == null)
                {
                    return(int.MaxValue);
                }

                Arrays.Fill(visited, false);
                int split = Visit(visited, stack.xs, idxs[pivotA], idxs[bndA.GetOther(pivotA)], 0);
                stack.len = Visit(visited, stack.xs, idxs[pivotB], idxs[bndB.GetOther(pivotB)], split);

                // perform bend one way
                BackupCoords(backup, stack);
                Bend(stack.xs, 0, split, pivotA, BendStep);
                Bend(stack.xs, split, stack.len, pivotB, -BendStep);

                congestion.Update(stack.xs, stack.len);

                if (PercDiff(score, congestion.Score()) >= ImprovementPrecThreshold)
                {
                    BackupCoords(coords, stack);
                    stackBackup.CopyFrom(stack);
                    min = congestion.Score();
                }

                // now bend the other way
                RestoreCoords(stack, backup);
                Bend(stack.xs, 0, split, pivotA, -BendStep);
                Bend(stack.xs, split, stack.len, pivotB, BendStep);
                congestion.Update(stack.xs, stack.len);
                if (PercDiff(score, congestion.Score()) >= ImprovementPrecThreshold && congestion.Score() < min)
                {
                    BackupCoords(coords, stack);
                    stackBackup.CopyFrom(stack);
                    min = congestion.Score();
                }

                // restore original coordinates and reset score
                RestoreCoords(stack, backup);
                congestion.Update(stack.xs, stack.len);
                congestion.score = score;
            }
            // general case: try bending acyclic bonds in the shortest
            // path from inside out
            else
            {
                // try bending all bonds and accept the best one
                foreach (var bond in pair.bndAt)
                {
                    if (bond.IsInRing)
                    {
                        continue;
                    }
                    if (bfix.Contains(bond))
                    {
                        continue;
                    }

                    // has this bond already been tested as part of another pair
                    if (!firstVisit.TryGetValue(bond, out AtomPair first))
                    {
                        firstVisit[bond] = first = pair;
                    }
                    if (first != pair)
                    {
                        continue;
                    }

                    var beg         = bond.Begin;
                    var end         = bond.End;
                    var begPriority = beg.GetProperty <int>(AtomPlacer.Priority);
                    var endPriority = end.GetProperty <int>(AtomPlacer.Priority);

                    Arrays.Fill(visited, false);
                    if (begPriority < endPriority)
                    {
                        stack.len = Visit(visited, stack.xs, idxs[beg], idxs[end], 0);
                    }
                    else
                    {
                        stack.len = Visit(visited, stack.xs, idxs[end], idxs[beg], 0);
                    }

                    BackupCoords(backup, stack);

                    // bend one way
                    if (begPriority < endPriority)
                    {
                        Bend(stack.xs, 0, stack.len, beg, pair.attempt * BendStep);
                    }
                    else
                    {
                        Bend(stack.xs, 0, stack.len, end, pair.attempt * BendStep);
                    }
                    congestion.Update(visited, stack.xs, stack.len);

                    if (PercDiff(score, congestion.Score()) >= ImprovementPrecThreshold &&
                        congestion.Score() < min)
                    {
                        BackupCoords(coords, stack);
                        stackBackup.CopyFrom(stack);
                        min = congestion.Score();
                    }

                    // bend other way
                    if (begPriority < endPriority)
                    {
                        Bend(stack.xs, 0, stack.len, beg, pair.attempt * -BendStep);
                    }
                    else
                    {
                        Bend(stack.xs, 0, stack.len, end, pair.attempt * -BendStep);
                    }
                    congestion.Update(visited, stack.xs, stack.len);

                    if (PercDiff(score, congestion.Score()) >= ImprovementPrecThreshold && congestion.Score() < min)
                    {
                        BackupCoords(coords, stack);
                        stackBackup.CopyFrom(stack);
                        min = congestion.Score();
                    }

                    RestoreCoords(stack, backup);
                    congestion.Update(visited, stack.xs, stack.len);
                    congestion.score = score;
                }
            }

            stack.CopyFrom(stackBackup);

            return(min);
        }
Esempio n. 23
0
 public void CopyFrom(IntStack stack)
 {
     Array.Copy(stack.xs, 0, xs, 0, stack.len);
     this.len = stack.len;
 }
Esempio n. 24
0
 public BehaviourIterator(BehaviourTree tree, int levelOffset)
 {
     _tree        = tree;
     _traversal   = new IntStack(_tree.Height);
     _levelOffset = levelOffset;
 }