def optLogLike(self, verbose=1, newtAndBrentPowell=1, allBrentPowell=0):
        """Calculate the likelihood of the tree, with optimization.

        There are two optimization methods-- choose one.  I've made
        'newtAndBrentPowell' the default, as it is fast and seems to be
        working.  The 'allBrentPowell' optimizer used to be the default,
        as it seems to be the most robust, although it is slow.  It would
        be good for checking important calculations.  
        """

        if verbose:
            theStartTime = time.clock()
        self._commonCStuff()

        # We want only one opt method.
        if newtAndBrentPowell:
            newtAndBrentPowell = 1
        if allBrentPowell:
            allBrentPowell = 1
        if (newtAndBrentPowell + allBrentPowell) != 1:
            gm = ['Tree.optLogLike()']
            gm.append("Choose 1 opt method.")
            raise P4Error(gm)

        # Do the opt.
        if allBrentPowell:
            pf.p4_allBrentPowellOptimize(self.cTree)
        else:
            pf.p4_newtSetup(self.cTree)
            pf.p4_newtAndBrentPowellOpt(self.cTree)

        # second arg is getSiteLikes
        self.logLike = pf.p4_treeLogLike(self.cTree, 0)

        # get the brLens
        brLens = pf.p4_getBrLens(self.cTree)
        for n in self.iterNodesNoRoot():
            n.br.len = brLens[n.nodeNum]

        # get the other free prams
        prams = pf.p4_getFreePrams(self.cTree)
        self.model.restoreFreePrams(prams)

        if verbose:
            print("optLogLike = %f" % self.logLike)
            theEndTime = time.clock()
            print("cpu time %s seconds." % (theEndTime - theStartTime))
    def optTest(self):
        self._commonCStuff()
        theStartTime = time.clock()
        doXfer = 0
        for i in range(1):
            if doXfer:
                self.model.setCStuff()
                self.setCStuff()
            pf.p4_setPrams(self.cTree, -1)
            self.logLike = pf.p4_treeLogLike(self.cTree, 0)

            if doXfer:
                # get the brLens
                brLens = pf.p4_getBrLens(self.cTree)
                for i in range(len(self.nodes)):
                    n = self.nodes[i]
                    if n != self.root:
                        n.br.len = brLens[i]

                # get the other free prams
                prams = pf.p4_getFreePrams(self.cTree)
                self.model.restoreFreePrams(prams)

        print("time %s seconds." % (time.clock() - theStartTime))
    def optLogLike(self, verbose=1, method="BOBYQA", optBrLens=True):
        """Calculate the likelihood of the tree, with optimization.

        There are different optimization methods-- choose one.  I've
        made 'BOBYQA' the default, as it is very fast and seems to be
        working.  It is from the nlopt library.

        Other opt methods include ---

        newtAndBrentPowell -- fairly fast, and works well.  It was the
        default.  Perhaps use this in combination with BOBYQA, eg

        t.optLogLike(method="BOBYQA")
        t.optLogLike(method="newtAndBrentPowell")

        The 'allBrentPowell' optimizer was the default several years
        ago, as it seems to be the most robust, although it is slow.
        It might be good for checking important calculations.

        'newtAndBOBYQA' --- fast and seems to work well.

        As suggested above, for difficult optimizations it may help to
        repeat the call to optLogLike(), perhaps with a different
        method.

        Arg optBrLens (default True), can be turned off.  This week, 
        this only works with method="BOBYQA".
        """

        gm = ["Tree.optLogLike()"]
        if verbose:
            theStartTime = time.time()

        if 0:
            for n in self.iterNodesNoRoot():
                if n.br.len < var.BRLEN_MIN:
                    gm.append(
                        "All branch lengths should be greater than or equal to var.BRLEN_MIN,"
                    )
                    gm.append(f"    which at the moment is {var.BRLEN_MIN}")
                    gm.append(
                        f"Got a branch length of {n.br.len:.8f} {n.br.len:g}")
                    gm.append(
                        "Either make the branch length bigger, or lower var.BRLEN_MIN."
                    )
                    gm.append(
                        "You could, for example, t.stripBrLens() which makes all br lens default 0.1"
                    )
                    raise P4Error(gm)

        if not optBrLens:
            if method != "BOBYQA":
                gm.append("Turning arg optBrLens off only works with BOBYQA")
                raise P4Error(gm)

        self._commonCStuff()

        if method == "newtAndBrentPowell":
            pf.p4_newtSetup(self.cTree)
            pf.p4_newtAndBrentPowellOpt(self.cTree)
        elif method == "allBrentPowell":
            pf.p4_allBrentPowellOptimize(self.cTree)
        elif method == "newtAndBOBYQA":
            pf.p4_newtSetup(self.cTree)
            pf.p4_newtAndBOBYQAOpt(self.cTree)
        elif method == "BOBYQA":
            if optBrLens:
                pf.p4_allBOBYQAOptimize(self.cTree, 1)
            else:
                pf.p4_allBOBYQAOptimize(self.cTree, 0)
        else:
            gm.append(
                'method should be one of "newtAndBrentPowell", "allBrentPowell", "newtAndBOBYQA", or "BOBYQA"'
            )
            raise P4Error(gm)

        # Do a final like calc.  (second arg is getSiteLikes)
        self.logLike = pf.p4_treeLogLike(self.cTree, 0)

        # get the brLens
        brLens = pf.p4_getBrLens(self.cTree)
        for n in self.iterNodesNoRoot():
            n.br.len = brLens[n.nodeNum]

        # get the other free prams
        prams = pf.p4_getFreePrams(self.cTree)
        self.model.restoreFreePrams(prams)

        if verbose:
            print("optLogLike = %f" % self.logLike)
            theEndTime = time.time()
            print("cpu time %s seconds." % (theEndTime - theStartTime))