public candidate ChooseAndApplyCarboxOptionUsingEstimator(candidate cand, Computation cpt, MessageClient clt, string runDir, int epoch)
        {
            option bestOpt      = null;
            var    bestProperty = Double.NegativeInfinity;
            var    options      = GetCarboxylOptions(cand);

            foreach (var opt in options)
            {
                var evalcand   = CopyAndApplyOption(opt, cand, true);
                var mol        = OBFunctions.designgraphtomol(evalcand.graph);
                var linkerName = AbstractAlgorithm.GetLinkerName(evalcand) + "-E" + epoch.ToString();
                var coeff      = Path.Combine(runDir, "data", "linker" + linkerName + ".coeff");
                var lmpdat     = Path.Combine(runDir, "data", "linker" + linkerName + ".lmpdat");
                Converter.moltoUFF(OBFunctions.designgraphtomol(evalcand.graph), coeff, lmpdat, false, 100);
                cpt.CalculateFeature(linkerName);
                var properpty = Convert.ToDouble(clt.SendMessage("[Predict]" + " " + linkerName));

                if (properpty > bestProperty)
                {
                    bestProperty = properpty;
                    bestOpt      = opt;
                }
            }
            Console.WriteLine("Best {0}", bestProperty);
            return(bestOpt == null ? null : CopyAndApplyOption(bestOpt, cand, true));
        }
Exemple #2
0
 protected override void Run()
 {
     Console.WriteLine("Fall 2019 One Drive New Mac Matt....");
     for (var r = 0; r < NUM_RUNS; r++)
     {
         Dictionary <string, int> MolSet = new Dictionary <string, int>();
         for (var s = 0; s < GEN_SET_SIZE; s++)
         {
             var cand    = GenerateCand();
             var atomNum = Evaluation.CountAtoms(cand);
             var smi     = OBFunctions.moltoSMILES(OBFunctions.designgraphtomol(cand.graph));
             if (!MolSet.ContainsKey(smi))
             {
                 MolSet.Add(smi, atomNum);
             }
             Console.WriteLine("Candidate Found {0}", smi);
             Console.WriteLine("Candidate moment {0}", string.Join(", ", Evaluation.CalcMoment(cand)));
         }
         for (var e = 0; e < NUM_EPOCH; e++)
         {
             Console.WriteLine("Epoch: {0}", e);
             var cand    = GenerateCand();
             var atomNum = Evaluation.CountAtoms(cand);
             var smi     = OBFunctions.moltoSMILES(OBFunctions.designgraphtomol(cand.graph));
             Console.WriteLine(smi);
             if (!MolSet.ContainsKey(smi))
             {
                 MolSet.Add(smi, atomNum);
             }
             writer.Write("{0} ", MolSet.Values.ToList().Max());
         }
         writer.WriteLine();
     }
     writer.Close();
 }
Exemple #3
0
        /// <summary>
        /// Clean way to minimize a graph.
        /// </summary>
        private designGraph Minimize(designGraph graph)
        {
            var mol    = OBFunctions.designgraphtomol(graph);
            var newMol = OBFunctions.InterStepMinimize(mol);

            OBFunctions.updatepositions(graph, newMol);
            return(graph);
        }
Exemple #4
0
        /// <summary>
        /// Clean way to minimize a graph.
        /// </summary>
        private designGraph Minimize(designGraph graph)
        {
            var mol = QuickMinimization(OBFunctions.designgraphtomol(graph), IODir + "rank" + ".lmpdat",
                                        IODir + "rank" + ".coeff", false, 0);

            OBFunctions.updatepositions(graph, mol);
            return(OBFunctions.tagconvexhullpoints(graph));
        }
        public void designgraphtoCML(designGraph host, string filename)
        {
            OBConversion obconv = new OBConversion();

            obconv.SetOutFormat("cml");
            OBMol mol = new OBMol();

            OBFunctions.designgraphtomol(host, ref mol);
            obconv.WriteFile(mol, filename);
        }
        protected override void Run()
        {
            var     agent    = new Algorithms.Deterministic(settings);
            BFSNode seedNode = new BFSNode(Seed, 0);

            QueueBFS.Enqueue(seedNode);
            while (QueueBFS.Count != 0)
            {
                var popNode = QueueBFS.Dequeue();
                Console.WriteLine("{0},{1}", popNode.getSMILE(), popNode.getDepth());
//                Console.Write(".");
                if (popNode.getDepth() < MAX_DEPTH)
                {
                    var newNodes = popNode.getChildren(agent);
                    foreach (var node in newNodes)
                    {
                        var nodeSMILE = node.getSMILE();
                        if (!allNode.Contains(nodeSMILE))
                        {
                            nodeInfoWriter.WriteLine("{0},{1}", nodeSMILE, node.getDepth());
//                            var nodeDir = _runDirectory + "/interNode/depth" + node.getDepth() + "/node" + nodeCnt;
//                            Directory.CreateDirectory(nodeDir);
//                            Settings.filer.Save(nodeDir + "/node" + nodeCnt + ".xml", node.getCand());
                            allNode.Add(nodeSMILE);
                            QueueBFS.Enqueue(node);
                            nodeCnt++;

                            var terminalLinker = node.getFinalCand(agent);
                            foreach (var linker in terminalLinker)
                            {
                                var mol       = OBFunctions.designgraphtomol(linker.graph);
                                var candSMILE = OBFunctions.moltoSMILES(mol);
                                if (!allFinalCand.Contains(candSMILE))
                                {
//                                    var linkerDir = _runDirectory + "/finalNode/depth" + node.getDepth() + "/linker" + candCnt;
//                                    Directory.CreateDirectory(linkerDir);
//
//                                    var coeff = Path.Combine(linkerDir, "linker" + candCnt + ".coeff");
//                                    var lmpdat = Path.Combine(linkerDir, "linker" + candCnt + ".lmpdat");
                                    allFinalCand.Add(candSMILE);
//                                    // Set up UFF and run lammps
//                                    Converter.moltoUFF(mol, coeff, lmpdat, false, 100);
//                                    Settings.filer.Save(linkerDir + "/linker" + candCnt + ".xml", linker.graph);
                                    candInfoWriter.WriteLine("{0},{1}", candSMILE, node.getDepth());
                                    candCnt++;
                                }
                            }
                        }
                    }
                }
            }
            nodeInfoWriter.Close();
            candInfoWriter.Close();
        }
        public void designgraphtoUFF(designGraph host, string coefficientfilename, string datafilename, bool contingency)
        {
            OBMol mol = new OBMol();

            OBFunctions.designgraphtomol(host, ref mol);
            LAMMPSNow lammpssetup = new LAMMPSNow(parameters, atomtypes, periodictable);

            if (contingency)
            {
                //if lammps fails to minimize, try minimization using openbabel as a starting point
                OBForceField uff = OBForceField.FindForceField("UFF");
                mol = lammpssetup.perturbator(mol, 2); //lammps crashes when atoms are on top of each other
                OBConversion obconv = new OBConversion();
                obconv.SetOutFormat("cml");
                obconv.WriteFile(mol, "badrun.cml");
                uff.Setup(mol);
                uff.SteepestDescent(1000);
            }
            lammpssetup.setupUFF(mol, coefficientfilename, datafilename, 50);
            //lammpssetup.setupUFFtest(mol);
            //lammpssetup.reset();
        }
Exemple #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);
        }
        public candidate ChooseAndApplyCarboxOptionBestAngle(candidate cand)
        {
            var    options   = GetCarboxylOptions(cand);
            option bestOpt   = null;
            var    bestAngle = .0;

            foreach (var opt in options)
            {
                var evalcand = CopyAndApplyOption(opt, cand, true);
                var mol      = OBFunctions.designgraphtomol(evalcand.graph);
                var angle    = CalAngle(mol);
                if (angle > 180)
                {
                    Console.WriteLine(angle + " too large");
                    Environment.Exit(0);
                }
                if (angle > bestAngle)
                {
                    bestAngle = angle;
                    bestOpt   = opt;
                }
            }
            return(bestOpt == null ? null : CopyAndApplyOption(bestOpt, cand, true));
        }
 public string getSMILE()
 {
     return(OBFunctions.moltoSMILES(OBFunctions.designgraphtomol(Cand.graph)));
 }
        private void GenerateFixed()
        {
            var agent = new Algorithms.Random(settings);
            var linkerBeforeCarboxDict = new Dictionary <string, candidate>();

            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;
                    candidate finalCand = null;
                    while (cand == null || finalCand == 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;
                        }
                        finalCand = agent.ChooseAndApplyCarboxOption(cand);
                        if (finalCand == null)
                        {
                            Console.WriteLine("Fail on finding final carbox");
                        }
                    }
                    var linkerName = AbstractAlgorithm.GetLinkerName(cand);
                    Console.WriteLine(linkerName);
                    if (linkerBeforeCarboxDict.ContainsKey(linkerName))
                    {
                        total_rule--;
                        continue;
                    }
                    linkerBeforeCarboxDict[linkerName] = cand;
                }
            }
            Console.WriteLine(linkerBeforeCarboxDict.Count);
            for (var e = 0; e < NUM_EPOCH; e++)
            {
                Console.WriteLine("Epoch: {0}", e);
                foreach (var item in linkerBeforeCarboxDict)
                {
                    var submitCand = agent.ChooseAndApplyCarboxOptionUsingEstimator(item.Value, computation, client, _runDirectory, e);
                    //cand = agent.ChooseAndApplyCarboxOptionBestAngle(item.Value());
                    //cand = agent.ChooseAndApplyCarboxOptionUsingEstimator(item.Value(), computation, client, _runDirectory);
                    if (submitCand == null)
                    {
                        Console.WriteLine("Fail on finding final carbox, should never happen");
                        Environment.Exit(0);
                    }
                    var linkerName = AbstractAlgorithm.GetLinkerName(submitCand) + "-E" + e.ToString();
                    var coeff      = Path.Combine(_runDirectory, "data", "linker" + linkerName + ".coeff");
                    var lmpdat     = Path.Combine(_runDirectory, "data", "linker" + linkerName + ".lmpdat");
                    agent.Converter.moltoUFF(OBFunctions.designgraphtomol(submitCand.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;
        }
        private double RunTrial(BanditNode node, int depth)
        {
            if (depth == 0) // leaf node
            {
                return(_evaluation.Evaluate(node.Cand));
            }


            if (!node.Bandit.HasOption())
            {
                return(_evaluation.Evaluate(node.Cand));
            }

            var    optionIndex = node.Bandit.SelectPullArm();
            double totalReward;

//            Console.WriteLine("Querry opt idx: {0}, No.Children: {1}", optionIndex, node.Children.Length);
            // If we reach max child nodes, then select randomly among children according to how much we've visited
            if (node.Children[optionIndex].Count >= MaxWidth)
            {
//                Console.WriteLine("Should never be here1.");
                var successors     = node.Children[optionIndex].Keys.ToList();
                var selectedOption = successors[node.Multinomial(optionIndex)];;
                node.Children[optionIndex][selectedOption].Visits += 1;
                var successorNode = node.Children[optionIndex][selectedOption].Node;
                totalReward = successorNode.TransitionReward + RunTrial(successorNode, depth - 1);
            }
            else
            {
                // generate a new successor node
                var successorState = CopyAndApplyOption(node.Options[optionIndex], node.Cand, true);
//                var immediateReward = Evaluate(successorState) - node.AbsoluteReward; // how much better than last node?
                var immediateReward = 0 - node.AbsoluteReward; // how much better than last node?

                // If the successor state is already in node.Children
                if (node.Children[optionIndex].ContainsKey(successorState))
                {
//                    Console.WriteLine("Should never be here2.");
                    var successorNode = node.Children[optionIndex][successorState].Node;
                    node.Children[optionIndex][successorState].Visits += 1; // mark that we've sampled
                    totalReward = immediateReward + RunTrial(successorNode, depth - 1);
                }
                else
                {
                    var successorNode = new BanditNode(this, successorState, immediateReward);
                    node.Children[optionIndex][successorState] = new BanditNode.NodeCountTuple(successorNode);
                    totalReward = immediateReward + _evaluation.Evaluate(successorState);//this evalutation tells how this state is POTENTIALLY good
                    var fileDir = "_runDirectory" + "/intermediateLinkers/linker" + nodeCnt;
                    Directory.CreateDirectory(fileDir);
                    Settings.filer.Save("_runDirectory" + "/intermediateLinkers/linker" + nodeCnt + "/linker" + nodeCnt + ".xml", successorState);
                    Console.WriteLine("Node{0}: depth: {1}, reward: {2}, smi: {3}", nodeCnt, MaxDepth - depth + 1, totalReward,
                                      OBFunctions.moltoSMILES(OBFunctions.designgraphtomol(successorState.graph)));
                    sw.WriteLine("{0},{1},{2},{3}", nodeCnt, MaxDepth - depth + 1, totalReward,
                                 OBFunctions.moltoSMILES(OBFunctions.designgraphtomol(successorState.graph)));
                    nodeCnt++;
                }
            }

            node.Bandit.Update(optionIndex, totalReward);
            return(totalReward);
        }