Exemplo n.º 1
0
        /* some functions for invoking the RecognizeChooseApplyCycle above. That function is
         * protected so we invoke it through one of these four functions. This functions will
         * not work, however, if one has not yet initiated the generation process to establish
         * the fields of the Generation object. */
        public candidate generateOneCandidate()
        {
            int[] numOfCalls = new int[numOfRuleSets];

            /* this copy set is needed because array are reference types and the RCA cycle
             * will modify the numOfCalls inside of it. */
            maxNumOfCalls.CopyTo(numOfCalls, 0);
            candidate newCand = seed.copy();

            RecognizeChooseApplyCycle(newCand, seed.activeRuleSetIndex, numOfCalls);
            return(newCand);
        }
Exemplo n.º 2
0
        /// <summary>
        ///   Calls  one rule on candidate - the rule is determined by the choose function.
        /// </summary>
        /// <param name = "cand">The cand.</param>
        /// <param name = "startingRuleSet">The starting rule set.</param>
        /// <returns></returns>
        public virtual candidate CallOneRuleOnCandidate(candidate cand = null, int startingRuleSet = -1)
        {
            if (cand == null)
            {
                cand = Host;
            }
            if (startingRuleSet == -1)
            {
                startingRuleSet = cand.activeRuleSetIndex;
            }

            /* the RecognizeChooseApplyCycle requires an array of ruleSet limits,
             * since we only intend to make one call on the activeRuleSet we make
             * an array (it should initialize to all zeros) of the proper length
             * and set its one value at the activeRuleSetIndex to 1. */
            var numOfCalls = new int[NumOfRuleSets];

            numOfCalls[cand.activeRuleSetIndex] = 1;

            var newCand = cand.copy();

            /* here the main cycle is invoked. First, we must pass a copy of the candidate
             * to the RCA cycle since the apply set will modify it, and then move the prevoius
             * state onto the prevStates under the candidate. It is not incorrect to state
             * merely the candidate here, but the prevStates will not be stored correctly.*/
            RecognizeChooseApplyCycle(newCand, startingRuleSet, numOfCalls);
            return(newCand);
        }
Exemplo n.º 3
0
        private candidate GenerateCand()
        {
            candidate cand = null;

            while (cand == null)
            {
                cand = Seed.copy();
                while (true)
                {
                    cand = agent.ChooseAndApplyAnyOption(cand);
                    if (cand == null)
                    {
                        Console.WriteLine("Fail, Rebuild");
                        break;
                    }
                    if (agent.IsTerminalCandidate(cand))
                    {
                        //Console.WriteLine("Choose terminal rule.");
                        break;
                    }
                    //Console.WriteLine("Choose non-terminal rule.");
                }
            }
            return(cand);
        }
Exemplo n.º 4
0
        /// <summary>
        ///   Generates all neighbors of the current.
        /// </summary>
        /// <param name = "current">The current.</param>
        /// <param name = "IncludingParent">if set to <c>true</c> [including parent].</param>
        /// <param name = "MaxNumber">The max number.</param>
        /// <returns></returns>
        public virtual List <candidate> GenerateAllNeighbors(candidate current = null, Boolean IncludingParent = false,
                                                             int MaxNumber     = int.MaxValue)
        {
            if (current == null)
            {
                current = Host;
            }
            var rand      = new Random();
            var neighbors = new List <candidate>();
            var options   = Rulesets[current.activeRuleSetIndex].recognize(current.graph, InParallel);

            while (MaxNumber < options.Count)
            {
                var i = rand.Next(options.Count);
                options.RemoveAt(i);
            }
            if (IncludingParent)
            {
                var parent = current.copy();
                parent.undoLastRule();
                neighbors.Add(parent);
            }
            if (InParallel)
            {
                Parallel.ForEach(options, opt =>
                {
                    var child = current.copy();
                    SearchProcess.transferLmappingToChild(child.graph, current.graph, opt);
                    opt.apply(child.graph, null);
                    child.addToRecipe(opt);
                    neighbors.Add(child);
                });
            }
            else
            {
                foreach (var opt in options)
                {
                    var child = current.copy();
                    SearchProcess.transferLmappingToChild(child.graph, current.graph, opt);
                    opt.apply(child.graph, null);
                    child.addToRecipe(opt);
                    neighbors.Add(child);
                }
            }
            return(neighbors);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Copies the candidate graph, transfers the L-mapping, and returns the resultant candidate.
        /// </summary>
        public candidate CopyAndApplyOption(option opt, candidate cand, bool doMinimize)
        {
            var newCand = cand.copy();
            var newOpt  = opt.copy();

            SearchProcess.transferLmappingToChild(newCand.graph, cand.graph, newOpt);
            ApplyOption(newOpt, newCand, doMinimize);
            return(newCand);
        }
Exemplo n.º 6
0
        /// <summary>
        ///   Generates one candidate. A simple function for invoking the RecognizeChooseApplyCycle.
        ///   That function is protected so we invoke it through a function like this.
        /// </summary>
        /// <param name = "cand">The cand to build upon (if null, then the seed will be used).</param>
        /// <param name = "startingRuleSet">The starting rule set (if unspecified then the candidate's active ruleset will be used.</param>
        /// <returns></returns>
        public virtual candidate GenerateOneCandidate(candidate cand = null, int startingRuleSet = -1)
        {
            if (cand == null)
            {
                cand = Host;
            }
            if (startingRuleSet == -1)
            {
                startingRuleSet = cand.activeRuleSetIndex;
            }
            var numOfCalls = (int[])MaxNumOfCalls.Clone();

            /* this copy set is needed because array are reference types and the RCA cycle
             * will modify the numOfCalls inside of it. */

            var newCand = cand.copy();

            RecognizeChooseApplyCycle(newCand, startingRuleSet, numOfCalls);
            return(newCand);
        }
Exemplo n.º 7
0
        protected override void Run()
        {
            Stack <candidate> candidates = new Stack <candidate>();
            candidate         current    = null;
            Boolean           found      = false;

            candidates.Push(seedCandidate);

            while (!found && (candidates.Count != 0) && !SearchIO.terminateRequest)
            {
                current             = candidates.Pop();
                SearchIO.iteration  = current.recipe.Count;
                SearchIO.miscObject = candidates.Count;

                if (isCurrentTheGoal(current))
                {
                    found = true;
                }
                else
                {
                    int           rsIndex     = current.activeRuleSetIndex;
                    List <option> ruleChoices = rulesets[rsIndex].recognize(current.graph);

                    foreach (option opt in ruleChoices)
                    {
                        candidate child = current.copy();
                        transferLmappingToChild(child.graph, current.graph, opt.location);
                        opt.apply(child.graph, null);
                        child.addToRecipe(opt);
                        candidates.Push(child);
                    }
                }
            }
            SearchIO.addAndShowGraphWindow(current.graph, "cand1");
            Save(settings.outputDirectory + "\\DFScandidate.xml", current);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Returns whether the graph violates angle or carboxyl-blocking constraints.
        /// </summary>
        private static bool IsValidChild(candidate cand, option opt)
        {
            var newCand = cand.copy();
            var newOpt  = opt.copy();

            SearchProcess.transferLmappingToChild(newCand.graph, cand.graph, newOpt);
            newOpt.apply(newCand.graph, null);

            var mol     = OBFunctions.designgraphtomol(newCand.graph);
            var mapping = OBFunctions.findcarboxylates(mol);

            if (mapping.Count < 2)
            {
                return(true);
            }

            var carbA  = mol.GetAtom(mapping[0][1]); // carbon in carboxylate
            var aA     = mol.GetAtom(mapping[0][3]); // atom that the carbon connects to
            var carbB  = mol.GetAtom(mapping[1][1]);
            var aB     = mol.GetAtom(mapping[1][3]);
            var pAngle = OBFunctions.pairwiseangle(carbA, aA, carbB, aB); // angle between carboxylates

            return(!OBFunctions.carboxylatesBlocked(mol, aA, carbA, aB, carbB) && pAngle >= AngleFloor);
        }
        private void Generate()
        {
            Console.WriteLine("ddddd!");
            var agent     = new Algorithms.Random(settings);
            var linkerSet = new HashSet <string>();

            for (var e = 0; e < NUM_EPOCH; e++)
            {
                Console.WriteLine("Epoch: {0}", e);
                for (var t = 0; t < NUM_TRAIL; t++)
                {
                    Console.WriteLine("Trail: {0}", t);
                    for (var total_rule = TOTAL_RULE_MIN; total_rule < TOTAL_RULE_MAX + 1; total_rule++)
                    {
                        candidate cand = null;
                        while (cand == null)
                        {
                            cand = Seed.copy();
                            Console.WriteLine("Total Intermediate Rules: {0}", total_rule);
                            for (var step = 0; step < total_rule; step++)
                            {
                                cand = agent.ChooseAndApplyOption(cand);
                                if (cand == null)
                                {
                                    Console.WriteLine("Fail on step {0}", step + 1);
                                    break;
                                }
                            }
                            if (cand == null)
                            {
                                continue;
                            }
                            //var carboxOpt = agent.ChooseAndApplyOption(cand);
                            cand = agent.ChooseAndApplyCarboxOptionBestAngle(cand);
                            //cand = agent.ChooseAndApplyCarboxOptionUsingEstimator(cand, computation, client, _runDirectory);
                            if (cand == null)
                            {
                                Console.WriteLine("Fail on finding final carbox");
                            }
                        }

                        var candSmile  = OBFunctions.moltoSMILES(OBFunctions.designgraphtomol(cand.graph));
                        var linkerName = AbstractAlgorithm.GetLinkerName(cand);
                        //Console.WriteLine(candSmile);
                        Console.WriteLine(linkerName);
                        if (linkerSet.Contains(linkerName))
                        {
                            total_rule--;
                            continue;
                        }
                        linkerSet.Add(linkerName);
                        var coeff  = Path.Combine(_runDirectory, "data", "linker" + linkerName + ".coeff");
                        var lmpdat = Path.Combine(_runDirectory, "data", "linker" + linkerName + ".lmpdat");
                        agent.Converter.moltoUFF(OBFunctions.designgraphtomol(cand.graph), coeff, lmpdat, false, 100);

                        double piority = 0;
                        if (CARBOXTYPE == "estimator")
                        {
                            piority = -Convert.ToDouble(client.SendMessage("[Predict]" + " " + linkerName));
                        }
                        else
                        {
                            piority = AbstractAlgorithm.Rand.NextDouble();
                            computation.CalculateFeature(linkerName);
                        }

                        //mutex.WaitOne();
                        jobBuffer.Add(linkerName, piority, e);
                        //mutex.ReleaseMutex();
                    }
                }
                if (CARBOXTYPE == "estimator")
                {
                    while (true)
                    {
                        var on_simulation = jobBuffer.Num_simulating();
                        if (on_simulation == 0)
                        {
                            break;
                        }
                        Console.WriteLine("Wait for current {0} linkers to finish simulation....", on_simulation);
                        Thread.Sleep(10000);
                    }
                    client.SendMessage("[FitModel]");
                }
            }
            allSubmitFlag = true;
        }
Exemplo n.º 10
0
        /// <summary>
        ///   Generates all neighbors of the current.
        /// </summary>
        /// <param name = "current">The current.</param>
        /// <param name = "IncludingParent">if set to <c>true</c> [including parent].</param>
        /// <param name = "MaxNumber">The max number.</param>
        /// <returns></returns>
        public static List <candidate> GenerateAllNeighbors(candidate current, IList <ruleSet> Rulesets, Boolean IncludingParent = false,
                                                            bool InParallel = false, bool includeStopCondition = false, int MaxNumber = int.MaxValue)
        {
            var rand      = new Random();
            var neighbors = new List <candidate>();
            var ruleset   = Rulesets[current.activeRuleSetIndex];
            var options   = ruleset.recognize(current.graph, InParallel);

            if (options.Count == 0)
            {
                var noRuleCandidate = current.copy();
                noRuleCandidate.activeRuleSetIndex = ruleset.nextRuleSet(GenerationStatuses.NoRules);
                neighbors.Add(noRuleCandidate);
                return(neighbors);
            }
            while (MaxNumber < options.Count)
            {
                var i = rand.Next(options.Count);
                options.RemoveAt(i);
            }
            if (IncludingParent)
            {
                var parent = current.copy();
                parent.undoLastRule();
                neighbors.Add(parent);
            }
            if (InParallel)
            {
                Parallel.ForEach(options, opt =>
                {
                    var child = current.copy();
                    SearchProcess.transferLmappingToChild(child.graph, current.graph, opt);
                    opt.apply(child.graph, null);
                    child.addToRecipe(opt);
                    neighbors.Add(child);
                });
            }
            else
            {
                foreach (var opt in options)
                {
                    var child = current.copy();
                    SearchProcess.transferLmappingToChild(child.graph, current.graph, opt);
                    opt.apply(child.graph, null);
                    child.addToRecipe(opt);
                    neighbors.Add(child);
                    if (opt.ruleNumber == ruleset.TriggerRuleNum)
                    {
                        child.activeRuleSetIndex = ruleset.nextRuleSet(GenerationStatuses.TriggerRule);
                    }
                    else
                    {
                        child.activeRuleSetIndex = ruleset.nextRuleSet(GenerationStatuses.Normal);
                    }
                }
            }
            var stopCandidate = current.copy();

            stopCandidate.activeRuleSetIndex = ruleset.nextRuleSet(GenerationStatuses.Choice);
            neighbors.Add(stopCandidate);
            return(neighbors);
        }
Exemplo n.º 11
0
        public static void runSearchProcess()
        {
            userDefinedGoals udg      = new userDefinedGoals();
            Form2            inputBox = new Form2(udg);

            //inputBox.ShowDialog();

            ruleSet.loadAndCompileSourceFiles(rulesets, Program.settings.recompileRules, Program.settings.compiledparamRules, Program.settings.execDir);

            List <candidate> candidates    = new List <candidate>();
            candidate        current       = null;
            candidate        seedCandidate = new candidate(seed, Program.settings.numOfRuleSets);
            Boolean          found         = false;

            candidates.Add(seedCandidate);
            GearEvaluator ge = new GearEvaluator(udg);

            Guidance.PNormProportionalPopulationSelection GuidanceApproach
                = new Guidance.PNormProportionalPopulationSelection(0, Guidance.optimize.minimize, true, true, 1);
            int maxPop = 10000;

            while (!found && (candidates.Count != 0))
            {
                current = candidates[0];
                candidates.RemoveAt(0);
                SearchIO.iteration = current.recipe.Count;
                //RECOGNIZE
                List <option> ruleChoices = rulesets[0].recognize(current.graph);
                SearchIO.miscObject = candidates.Count;
                //
                if (current.recipe.Count >= 2)
                {
                    found = isCurrentTheGoal(current, udg);
                    if (found == true)
                    {
                        ge.evalGT(current);
                        break;
                    }
                }
                //else current.f0 = double.PositiveInfinity;

                for (int i = 0; i != ruleChoices.Count; i++)
                {
                    candidate child = current.copy();

                    ruleChoices = rulesets[0].recognize(child.graph);
                    ruleChoices[i].apply(child.graph, null);

                    child.addToRecipe(ruleChoices[i]);
                    child.f0 = double.NaN;
                    child.f1 = double.NaN;
                    child.f2 = double.NaN;
                    ge.evalGT(child);
                    child.f3 = (child.f0) / 100 + child.f1;
                    //1 efficiency
                    //2 mass
                    //3 combination
                    addChildToSortedCandList(candidates, child, 3);
                    //candidates.Add(child);
                }
            }
            Program.addAndShowGraphDisplay(current.graph, "Here is your gear train!!!");
            candidate.saveToXml(current, "testTuned", settings.outputDirectory);
        }