Ejemplo n.º 1
0
 def lq (self, thresholds, logger, title):
     stc, sco, stw, sws = self.sum()
     if stc == 0:
         return (float('NaN'), 0, 0, [])
     # base rates
     brc = float(stc)/sco
     brw = float(stw)/sws
     if thresholds is None:
         thresholds = [brc] if brc == brw else [brc,brw]
     # convert thresholds to whole numbers of bins
     cumco = util.cumsum([co for _tc, co, _tw, _ws in self.bins])
     top = len(self.bins) - 2 # max bin index for thresholds
     thresholds = [min(top,bisect.bisect_left(cumco,th*sco)) for th in thresholds]
     thresholds = util.dedup(thresholds)
     # http://en.wikipedia.org/wiki/Trapezoidal_rule
     cph = sum([(stw - tw/2)*ws for stw,(tw,ws) in
                zip(util.cumsum([tw for _tc, _co, tw, _ws in self.bins]),
                    [(tw,ws) for _tc, _co, tw, ws in self.bins])])
     cph = float(cph) / (sws*stw)
     logger.info("%s: cph=%g targetLevel=%g totalWeight=%g",
                 title,cph,stw,sws)
     lq = (2*cph - 1) / (1 - brw)
     mxl = []
     for threshold in thresholds:
         mx = ConfusionMX("{h:s} at {d:.2%}".format(h=title,d=float(cumco[threshold])/sco))
         mxl.append(mx)
         for i in range(len(self.bins)):
             _tc, _co, tw, ws = self.bins[i]
             if tw > 0:
                 mx.add(True, i <= threshold, tw)
             if ws > tw:
                 mx.add(False, i <= threshold, ws - tw)
     return (lq, brc, brw, mxl)
Ejemplo n.º 2
0
    def __init__(self, machines, processing_times):
        """
		Initialize the needed representation data
		structures for a job shop scheduling problem.
		"""

        self.machines = machines
        self.processing_times = processing_times

        # D: weight of operations
        self._D = tuple(flatten(processing_times))
        # N: amount of nodes
        self.N = len(self._D) + 2
        # V: nodes
        self.V = (-1, ) + tuple(range(self.N - 2)) + (self.N - 2, )
        # J: amount of operations in jobs
        self.J = tuple(len(x) for x in machines)

        # A: edges encoding the job precedence constraints
        self.A = {
            i: [i + 1]
            for i in range(self.N - 2) if i + 1 not in cumsum(self.J)
        }
        self.A[-1] = [x for x in [0] + cumsum(self.J)[:-1]]
        for i in cumsum(self.J):
            self.A[i - 1] = [self.N - 2]
        self.A[self.N - 2] = []

        # E: edges encoding the machine constraints
        self.E = dict()
        for node in self.V:
            self.E[node] = []
        e = [None] * len(machines)
        for i, j in enumerate(flatten(machines)):
            if e[j]: e[j].append(i)
            else: e[j] = [i]
        self.e = [x for x in e if x]
        for el in self.e:
            for i in el:
                self.E[i] = list(set(el) - set([i]))

        # r: start time of node (also length from start node)
        self._r = list(
            flatten([[0] + cumsum(x)[:-1] for x in processing_times]))
        # t: length to end node
        self._t = list(
            flatten([
                list(reversed(cumsum(tuple(reversed(x)))))[1:] + [0]
                for x in processing_times
            ]))

        self.machines = tuple(flatten(self.machines))
        self.optimize()
Ejemplo n.º 3
0
    def optimize(self):
        # hardcodes several parameters for performance
        self._PMP = dict()
        self._SMP = dict()
        self._PM = deepcopy(self.E)
        self._PJ = {-1: [], (self.N - 2): [x - 1 for x in cumsum(self.J)]}
        for node in self.V:
            self._PMP[node] = []
            self._SMP[node] = []
            if node in self._PJ.keys(): pass
            elif node in [0] + cumsum(self.J)[:-1]: self._PJ[node] = [-1]
            else: self._PJ[node] = [node - 1]

        self._DD = dict()
        for node in self.V:
            if node < 0 or node >= self.N - 2:
                self._DD[node] = 0
            else:
                self._DD[node] = self._D[node]
Ejemplo n.º 4
0
 def lq(self, thresholds, logger, title):
     stc, sco, stw, sws = self.sum()
     if stc == 0:
         return (float('NaN'), 0, 0, [])
     # base rates
     brc = float(stc) / sco
     brw = float(stw) / sws
     if thresholds is None:
         thresholds = [brc] if brc == brw else [brc, brw]
     # convert thresholds to whole numbers of bins
     cumco = util.cumsum([co for _tc, co, _tw, _ws in self.bins])
     top = len(self.bins) - 2  # max bin index for thresholds
     thresholds = [
         min(top, bisect.bisect_left(cumco, th * sco)) for th in thresholds
     ]
     thresholds = util.dedup(thresholds)
     # http://en.wikipedia.org/wiki/Trapezoidal_rule
     cph = sum([(stw - tw / 2) * ws for stw, (
         tw,
         ws) in zip(util.cumsum([tw for _tc, _co, tw, _ws in self.bins]), [(
             tw, ws) for _tc, _co, tw, ws in self.bins])])
     cph = float(cph) / (sws * stw)
     logger.info("%s: cph=%g targetLevel=%g totalWeight=%g", title, cph,
                 stw, sws)
     lq = (2 * cph - 1) / (1 - brw)
     mxl = []
     for threshold in thresholds:
         mx = ConfusionMX("{h:s} at {d:.2%}".format(
             h=title, d=float(cumco[threshold]) / sco))
         mxl.append(mx)
         for i in range(len(self.bins)):
             _tc, _co, tw, ws = self.bins[i]
             if tw > 0:
                 mx.add(True, i <= threshold, tw)
             if ws > tw:
                 mx.add(False, i <= threshold, ws - tw)
     return (lq, brc, brw, mxl)
Ejemplo n.º 5
0
def plot_art(art, pattern, plotfolder):
    xticks = ["" for _ in art.views]
    i = 0
    while i < len(art.views):
        xticks[i] = str(i % 24) + "h"
        i += 6

    max_length = len(art.views)
    log_estimate = zeros(max_length).tolist()

    if art.pattern.has_gamma:
        log_estimate[art.pattern.start : art.pattern.length * 24] = [
            log(art.red_views[art.pattern.start]) + log(art.beta) * x for x in range(art.pattern.length * 24)
        ]
        log_estimate[art.pattern.length * 24 : max_length] = [
            log(art.red_views[art.pattern.start]) + log(art.gamma) + log(art.beta) * (x - 1)
            for x in range(art.pattern.length * 24, max_length)
        ]
    else:
        log_estimate[art.pattern.start : art.pattern.start + art.pattern.length * 24] = [
            log(art.red_views[art.pattern.start]) + log(art.beta) * x for x in range(art.pattern.length * 24)
        ]

    estimate = [exp(log_estimate[x]) for x in range(max_length)]

    plotinfo = {"normal": {}, "estimate": {}, "model": {}}
    plotinfo["normal"]["x"] = xrange(len(art.views))
    plotinfo["normal"]["y"] = art.views
    plotinfo["normal"]["line_style"] = "-"
    plotinfo["normal"]["line_color"] = "blue"
    plotinfo["normal"]["label"] = "Page-views"
    plotinfo["model"]["x"] = xrange(len(art.views))
    plotinfo["model"]["y"] = estimate
    plotinfo["model"]["line_style"] = "--"
    plotinfo["model"]["line_color"] = "green"
    plotinfo["model"]["label"] = "Model"
    plotinfo["estimate"]["x"] = xrange(len(art.views))
    plotinfo["estimate"]["y"] = art.est_params
    plotinfo["estimate"]["line_style"] = "--"
    plotinfo["estimate"]["line_color"] = "red"
    if pattern.has_gamma:
        plotinfo["estimate"]["label"] = r"Estimated views ($\beta,\gamma$)"
        plotinfo["gamma"] = {}
        plotinfo["gamma"]["x"] = xrange(len(art.views))
        plotinfo["gamma"]["y"] = art.est_gamma_func
        plotinfo["gamma"]["line_style"] = "--"
        plotinfo["gamma"]["line_color"] = "black"
        plotinfo["gamma"]["label"] = r"Estimated views ($\beta,\gamma(v_1)$)"
    else:
        plotinfo["estimate"]["label"] = r"Estimated views ($\beta$)"

    axisinfo = {
        "filename": plotfolder + art.link_title,
        "ylabel": r"Number of page-views",
        "xticks": xticks,
        "title": "Hourly progression of " + art.title,
        "xlim": {"start": 0, "end": len(xticks)},
        "ylim": get_ylims(plotinfo),
    }
    plot_pattern(plotinfo, axisinfo)

    plotinfo = {"cumsum": {}}
    plotinfo["cumsum"]
    plotinfo["cumsum"]["x"] = xrange(len(art.views))
    plotinfo["cumsum"]["y"] = cumsum(art.views)
    plotinfo["cumsum"]["line_style"] = "-"
    plotinfo["cumsum"]["line_color"] = "blue"
    plotinfo["cumsum"]["label"] = "Cumulative views"

    axisinfo = {
        "filename": plotfolder + "cum_" + art.link_title,
        "ylabel": r"Cumulative number of page-views",
        "xticks": xticks,
        "title": "Cumulative progression of " + art.title,
        "xlim": {"start": 0, "end": len(xticks)},
        "ylim": get_ylims(plotinfo),
    }
    plot_pattern(plotinfo, axisinfo)

    plotinfo = {"normalized": {}}
    plotinfo["normalized"]
    plotinfo["normalized"]["x"] = xrange(len(art.views))
    plotinfo["normalized"]["y"] = art.norm_views
    plotinfo["normalized"]["line_style"] = "-"
    plotinfo["normalized"]["line_color"] = "blue"
    plotinfo["normalized"]["label"] = "Normalized views"

    axisinfo = {
        "filename": plotfolder + "norm_" + art.link_title,
        "ylabel": r"Number of page-views",
        "xticks": xticks,
        "title": "Hourly progression of " + art.title,
        "xlim": {"start": 0, "end": len(xticks)},
        "ylim": {"start": 0, "end": ceil(max(plotinfo["normalized"]["y"]) * 100) / 100},
    }
    plot_pattern(plotinfo, axisinfo)