Пример #1
0
    def alignData(i0, i1):
        """Returns two arrays of fevals aligned on function evaluations.
        """

        res = readalign.alignArrayData(
            readalign.HArrayMultiReader([i0.evals, i1.evals]))
        idx = 1 + i0.nbRuns()
        data0 = res[:, np.r_[0, 1:idx]]
        data1 = res[:, np.r_[0, idx:idx + i1.nbRuns()]]
        return data0, data1
def generateData(ds0, ds1):
    #Align ert arrays on targets
    array0 = numpy.vstack([ds0.target, ds0.ert]).transpose()
    array1 = numpy.vstack([ds1.target, ds1.ert]).transpose()
    data = readalign.alignArrayData(readalign.HArrayMultiReader([array0, array1]))

    #Downsample?
    adata = data[data[:, 0]<=10, :]
    try:
        adata = adata[adata[:, 0]>=1e-8, :]
    except IndexError:
        #empty data
        pass
        #set_trace()

    targets = adata[:, 0]
    ert0 = adata[:, 1]
    ert1 = adata[:, 2]

    return targets, ert0, ert1
Пример #3
0
    def __init__(self, dslist):
        """Instantiate one algorithm portfolio data set.
        
        :param dict dslist: list of :py:class:`pproc.DataSetList`
                            instances.
        
        """

        def _conv_evals(evals, algnb, maxevals):
            if evals > maxevals[algnb]:
                return np.nan
            res = 0.
            mevals = np.asarray(maxevals)
            if evals > len(maxevals) or not isinstance(evals, int):
                smevals = np.sort(mevals)
                for i in smevals:
                    res += min(evals - 1, i)
            else:
                for i in range(1, evals):
                    res += np.sum(i <= mevals)
            res += np.sum(evals <= mevals[:algnb+1])
            return res

        # Checking procedure
        d = set()
        f = set()
        trials = []
        for i in dslist:
            d.add(i.dim)
            f.add(i.funcId)
            trials.append(i.createDictInstanceCount())
        if len(f) > 1 or len(d) > 1:
            raise Usage('%s: Expect the data of algorithms for only one '
                        'function and one dimension.' % (dslist))
        elif trials[1:] != trials[:-1]:
            # this check will be superfluous if we find that all instances
            # are equivalent.
            # raise Usage('%s: Expect the data to have the same instances.'
            #             % (dslist))
            warnings.warn('portfolio will be generated from algorithm with different instances')

        self.dim = d.pop()
        self.funcId = f.pop()
        algId = []
        comment = []
        for i in dslist:
            algId.append(i.algId)
            comment.append(i.comment)
        self.algId = tuple(algId)
        self.comment = tuple(comment)

        # Data handling
        nbruns = dslist[0].nbRuns() # all data sets have the same #runs
        corresp = [[]] * len(dslist)
        if False:
            # find correspondence with respect to first element in dslist
            dictref = dslist[0].createDictInstance()
            for i, ds in enumerate(dslist):
                tmpdict = ds.createDictInstance()
                for j in sorted(dictref):
                    corresp[i].extend(tmpdict[j])
        else:
            for i in range(len(dslist)):
                corresp[i] = range(nbruns)
        self.instancenumbers = trials.pop()
        maxevals = []
        finalfunvals = []
        evals = []
        funvals = []
        for i in range(nbruns):
            tmpmaxevals = []
            tmpfinalfunvals = []
            tmpevals = []
            tmpfunvals = []
            for j, ds in enumerate(dslist):
                tmpmaxevals.append(ds.maxevals[corresp[j][i]])
                tmpfinalfunvals.append(ds.finalfunvals[corresp[j][i]])
                tmpevals.append(ds.evals[:, np.r_[0, corresp[j][i]+1]])
                tmpfunvals.append(ds.funvals[:, np.r_[0, corresp[j][i]+1]].copy())
            maxevals.append(np.sum(tmpmaxevals))
            finalfunvals.append(min(tmpfinalfunvals))
            tmpevals = ra.alignArrayData(ra.HArrayMultiReader(tmpevals))
            tmpres = []
            for j in tmpevals:
                tmp = []
                for k, e in enumerate(j[1:]):
                    tmp.append(_conv_evals(e, k, tmpmaxevals))
                tmpres.append(min(tmp))
            evals.append(np.column_stack((tmpevals[:, 0], tmpres)))

            for j, a in enumerate(tmpfunvals):
                for k in range(len(a[:, 0])):
                    a[k, 0] = _conv_evals(a[k, 0], j, tmpmaxevals)
            tmpfunvals = ra.alignArrayData(ra.VArrayMultiReader(tmpfunvals))
            tmpres = []
            for j in tmpfunvals:
                tmpres.append(min(j[1:]))
            funvals.append(np.column_stack((tmpfunvals[:, 0], tmpres)))
        self.maxevals = np.array(maxevals)
        self.finalfunvals = np.array(finalfunvals)
        self.evals = ra.alignArrayData(ra.HArrayMultiReader(evals))
        self.funvals = ra.alignArrayData(ra.VArrayMultiReader(funvals))
        self.computeERTfromEvals()
Пример #4
0
    def __init__(self, dictAlg):
        """Instantiate one best algorithm data set.
        
        :keyword dictAlg: dictionary of datasets, keys are algorithm
                          names, values are 1-element
                          :py:class:`DataSetList`.
        
        """

        # values of dict dictAlg are DataSetList which should have only one
        # element which will be assigned as values in the following lines.
        d = set()
        f = set()
        for i in dictAlg.values():
            d |= set(j.dim for j in i)
            f |= set(j.funcId for j in i)

        if len(f) > 1 or len(d) > 1:
            Usage('Expect the data of algorithms for only one function and '
                  'one dimension.')

        f = f.pop()
        d = d.pop()

        dictMaxEvals = {}
        dictFinalFunVals = {}
        tmpdictAlg = {}
        for alg, i in dictAlg.iteritems():
            if len(i) == 0:
                warnings.warn('Algorithm %s was not tested on f%d %d-D.'
                              % (alg, f, d))
                continue
            elif len(i) > 1:
                warnings.warn('Algorithm %s has a problem on f%d %d-D.'
                              % (alg, f, d))
                continue

            tmpdictAlg[alg] = i[0] # Assign ONLY the first element as value
            dictMaxEvals[alg] = i[0].maxevals
            dictFinalFunVals[alg] = i[0].finalfunvals

        dictAlg = tmpdictAlg

        sortedAlgs = dictAlg.keys()
        # algorithms will be sorted along sortedAlgs which is now a fixed list

        # Align ERT
        erts = list(np.transpose(np.vstack([dictAlg[i].target, dictAlg[i].ert]))
                    for i in sortedAlgs)
        res = readalign.alignArrayData(readalign.HArrayMultiReader(erts))

        resalgs = []
        reserts = []
        # For each function value
        for i in res:
            # Find best algorithm
            curerts = i[1:]
            assert len((np.isnan(curerts) == False)) > 0
            currentbestert = np.inf
            currentbestalg = ''
            for j, tmpert in enumerate(curerts):
                if np.isnan(tmpert):
                    continue # TODO: don't disregard these entries
                if tmpert == currentbestert:
                    # TODO: what do we do in case of ties?
                    # look at function values corresponding to the ERT?
                    # Look at the function evaluations? the success ratio?
                    pass
                elif tmpert < currentbestert:
                    currentbestert = tmpert
                    currentbestalg = sortedAlgs[j]
            reserts.append(currentbestert)
            resalgs.append(currentbestalg)

        dictiter = {}
        dictcurLine = {}
        resDataSet = []

        # write down the #fevals to reach the function value.
        for funval, alg in zip(res[:, 0], resalgs):
            it = dictiter.setdefault(alg, iter(dictAlg[alg].evals))
            curLine = dictcurLine.setdefault(alg, np.array([np.inf, 0]))
            while curLine[0] > funval:
                try:
                    curLine = it.next()
                except StopIteration:
                    break
            dictcurLine[alg] = curLine.copy()
            tmp = curLine.copy()
            tmp[0] = funval
            resDataSet.append(tmp)

        setalgs = set(resalgs)
        dictFunValsNoFail = {}
        for alg in setalgs:
            for curline in dictAlg[alg].funvals:
                if (curline[1:] == dictAlg[alg].finalfunvals).any():
                    # only works because the funvals are monotonous
                    break
            dictFunValsNoFail[alg] = curline.copy()

        self.evals = resDataSet
        # evals is not a np array but a list of arrays because they may not
        # all be of the same size.
        self.maxevals = dict((i, dictMaxEvals[i]) for i in setalgs)
        self.finalfunvals = dict((i, dictFinalFunVals[i]) for i in setalgs)
        self.funvalsnofail = dictFunValsNoFail
        self.dim = d
        self.funcId = f
        self.algs = resalgs
        self.algId = 'Virtual Best Algorithm of BBOB'
        self.comment = 'Combination of ' + ', '.join(sortedAlgs)
        self.ert = np.array(reserts)
        self.target = res[:, 0]

        bestfinalfunvals = np.array([np.inf])
        for alg in sortedAlgs:
            if np.median(dictAlg[alg].finalfunvals) < np.median(bestfinalfunvals):
                bestfinalfunvals = dictAlg[alg].finalfunvals
                algbestfinalfunvals = alg
        self.bestfinalfunvals = bestfinalfunvals
        self.algbestfinalfunvals = algbestfinalfunvals
Пример #5
0
    def __init__(self, dictAlg):

        # values of dict dictAlg are DataSetList which should have only one
        # element which will be assigned as values in the following lines.
        d = set()
        f = set()
        for i in dictAlg.values():
            d |= set(j.dim for j in i)
            f |= set(j.funcId for j in i)

        if len(f) > 1 or len(d) > 1:
            #set_trace()
            Usage(
                'Expect the data of algorithms for only one function and one dimension.'
            )

        f = f.pop()
        d = d.pop()

        dictMaxEvals = {}
        dictFinalFunVals = {}
        tmpdictAlg = {}
        for alg, i in dictAlg.iteritems():
            if len(i) != 1:
                # Special case could occur?
                txt = ('Algorithm %s has problem in this case: f%d %d-D.' %
                       (alg, f, d))
                warnings.warn(txt)
                continue

            tmpdictAlg[alg] = i[0]  # Assign the first element as value for alg
            dictMaxEvals[alg] = i[0].maxevals
            dictFinalFunVals[alg] = i[0].finalfunvals

        dictAlg = tmpdictAlg

        sortedAlgs = dictAlg.keys()
        # get a fixed list of the algs, algorithms will be sorted along sortedAlgs

        #Align ERT
        erts = list(
            numpy.transpose(numpy.vstack([dictAlg[i].target, dictAlg[i].ert]))
            for i in sortedAlgs)
        res = readalign.alignArrayData(readalign.HArrayMultiReader(erts))

        resalgs = []
        reserts = []
        #Foreach function value
        for i in res:
            #find best algorithm
            curerts = i[1:]
            assert len((numpy.isnan(curerts) == False)) > 0

            currentbestert = numpy.inf
            currentbestalg = ''
            #currentbestval = dictMaxEvals[alg]
            for j, tmpert in enumerate(curerts):
                if numpy.isnan(tmpert):
                    continue
                if tmpert == currentbestert:
                    # in case of tie? TODO: look at function values corresponding to the ERT?
                    # Look at the function evaluations? the success ratio?
                    pass
                elif tmpert < currentbestert:
                    currentbestert = tmpert
                    currentbestalg = sortedAlgs[j]

            reserts.append(currentbestert)
            resalgs.append(currentbestalg)

        dictiter = {}
        dictcurLine = {}
        resDataSet = []
        #write down the #fevals to reach the function value.
        for funval, alg in zip(res[:, 0], resalgs):
            it = dictiter.setdefault(alg, iter(dictAlg[alg].evals))
            curLine = dictcurLine.setdefault(alg, numpy.array([numpy.inf, 0]))

            while curLine[0] > funval:
                try:
                    curLine = it.next()
                except StopIteration:
                    break
            dictcurLine[alg] = curLine.copy()
            tmp = curLine.copy()
            tmp[0] = funval
            resDataSet.append(tmp)

        setalgs = set(resalgs)
        dictFunValsNoFail = {}
        for alg in setalgs:
            for curline in dictAlg[alg].funvals:
                if (curline[1:] == dictAlg[alg].finalfunvals).any():
                    # only works because the funvals are monotonous
                    break
            dictFunValsNoFail[alg] = curline.copy()

        self.evals = resDataSet
        # evals is not a numpy array but a list of arrays because they may not
        # all be of the same size.
        self.maxevals = dict((i, dictMaxEvals[i]) for i in setalgs)
        self.finalfunvals = dict((i, dictFinalFunVals[i]) for i in setalgs)
        self.funvalsnofail = dictFunValsNoFail
        self.dim = d
        self.funcId = f
        # What if some algorithms don't have the same number of runs
        # How do we save maxfunevals (to compute the ERT...?)
        self.algs = resalgs
        self.algId = 'Virtual Best Algorithm of BBOB 2009'
        self.comment = 'Combination of ' + ', '.join(algs)
        self.ert = numpy.array(reserts)
        self.target = res[:, 0]
        #return resds

        bestfinalfunvals = numpy.array([numpy.inf])
        for alg in sortedAlgs:
            if numpy.median(dictAlg[alg].finalfunvals) < numpy.median(
                    bestfinalfunvals):
                bestfinalfunvals = dictAlg[alg].finalfunvals
                algbestfinalfunvals = alg
        self.bestfinalfunvals = bestfinalfunvals
        self.algbestfinalfunvals = algbestfinalfunvals