Exemple #1
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();
 }
        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 string getSMILE()
 {
     return(OBFunctions.moltoSMILES(OBFunctions.designgraphtomol(Cand.graph)));
 }
        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;
        }
        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);
        }