Ejemplo n.º 1
0
def forecast_load(week, day, hour):
    """returns the peak load as a percentage of annual value"""
    if not (0 <= week <= 51):
        raise Error("week (%s) is out of bounds" % str(week))
    if not (0 <= hour <= 23):
        raise Error("hour (%s) is out of bounds" % str(hour))

    m1 = weekly[week]
    m2 = daily[day]
    m3 = hourly[season(week)][weektype(day)][hour]
    m = (m1 * m2 * m3) / (100.0 * 100.0 * 100.0)
    # print m1, m2, m3, m
    return m
Ejemplo n.º 2
0
 def handleTableEvent(self, event: TableEvent):
     if isinstance(event, PlayerJoin):
         self.table.player_join(event.player, event.seat, event.buyin)
     elif isinstance(event, PlayerLeave):
         self.table.player_leave(event.player)
     else:
         raise Error(f'unknown TableEvent: {event}')
Ejemplo n.º 3
0
 def receive(self, event: Event):
     if isinstance(event, TableEvent):
         self.handleTableEvent(event)
     elif isinstance(event, PlayerAction):
         self.handlePlayerAction(event)
     else:
         raise Error(f'unknown Event: {event}')
Ejemplo n.º 4
0
def weektype(day):
    if weekend(day):
        return "weekend"
    if weekday(day):
        return "weekday"
    else:
        raise Error("day (%s) is not a weekday or weekend?!" % str(day))
Ejemplo n.º 5
0
def batch_matlab_script(filename, batch):
    """func batch_matlab_script  :: Str, SimulationBatch -> 
       ----
       create a matlab script file which simulates all the Scenarios
       in the batch assuming their filename is 
           "psat_" + scenario.title + ".m"
    """

    EnsureNotEqual(len(batch), 0)
    with open(filename, "w") as matlab_stream:

        matlab_stream.write("initpsat;\n")
        matlab_stream.write("Settings.lfmit = 50;\n")
        matlab_stream.write("Settings.violations = 'on'\n")
        matlab_stream.write("OPF.basepg = 0;\n")
        matlab_stream.write("OPF.basepl = 0;\n")

        simtype = batch[0].simtype

        for scenario in batch:
            filename = "psat_" + scenario.title + ".m"
            matlab_stream.write("runpsat('" + filename + "','data');\n")

            EnsureEqual(simtype, scenario.simtype)

            if simtype == "pf":
                matlab_stream.write("runpsat pf;\n")
            elif simtype == "opf":
                matlab_stream.write("runpsat opf;\n")
            else:
                raise Error("expected pf or opf got: " + scenario.simtype)
            matlab_stream.write("runpsat pfrep;\n")

        matlab_stream.write("closepsat;\n")
        matlab_stream.write("exit\n")
Ejemplo n.º 6
0
def single_matlab_script(filename, psat_filename, simtype):
    """func single_matlab_script :: Str, Str, Str -> 
       ----
       create a matlab script file which simulates the psat_file specified
       either a a power flow (simtype='pf') or optimal power flow 
       (simtype='opf').
    """

    with open(filename, "w") as matlab_stream:

        matlab_stream.write("initpsat;\n")
        matlab_stream.write("Settings.lfmit = 50;\n")
        matlab_stream.write("Settings.violations = 'on'\n")
        matlab_stream.write("runpsat('" + psat_filename + "','data');\n")

        if simtype == "pf":
            matlab_stream.write("runpsat pf;\n")
        elif simtype == "opf":
            matlab_stream.write("OPF.basepg = 0;\n")
            matlab_stream.write("OPF.basepl = 0;\n")
            matlab_stream.write("runpsat opf;\n")
        else:
            raise Error("expected pf or opf got: " + simtype)
        matlab_stream.write("runpsat pfrep;\n")
        matlab_stream.write("closepsat;\n")
        matlab_stream.write("exit;\n")
Ejemplo n.º 7
0
 def player_join(self, player: Player, seat: Seat, buyin: ChipAmount):
     if self.seating[seat] is not None:
         raise Error(
             f'{player} cannot sit in {seat} because it is occupied by {self.seating[seat]}'
         )
     else:
         self.seating[seat] = player
         self.stacks[player] = buyin
Ejemplo n.º 8
0
 def postParse(self, _, _2, tokenlist):
     """ Converts the first token to boolean """
     tok = string.lower(tokenlist[0])
     if tok in ["t", "true", "1"]:
         return True
     elif tok in ["f", "false", "0"]:
         return False
     else:
         raise Error("token (%s) must be boolean" % tok)
Ejemplo n.º 9
0
 def process_header_title(self, tokens):
     # print("Header : %s" % tokens)
     if len(tokens[0]) == 1:
         # print "Power Flow"
         pass
     elif len(tokens[0]) == 2:
         # print "Optimal Power Flow"
         pass
     else:
         raise Error("%s" % tokens)
Ejemplo n.º 10
0
 def player_leave(self, player: Player):
     seats = [
         seat for seat, seated_player in self.seating.items()
         if seated_player == player
     ]
     if len(seats) == 0:
         raise Error(f'{player} cannot leave because they are not seated')
     else:
         for seat in seats:
             self.seating[seat] = None
         self.stacks.pop(player, None)
Ejemplo n.º 11
0
def season(week):
    if spring(week):
        return "spring"
    if summer(week):
        return "summer"
    if autumn(week):
        return "autumn"
    if winter(week):
        return "winter"
    else:
        raise Error("week (%s) is not a proper season?!" % str(week))
Ejemplo n.º 12
0
    def read(self, stream):
        def title_matches(line, title):
            return line.startswith(title)

        def read_as_cid(storage, classtype):
            EnsureEqual(len(storage), 0)
            for item in read_section(stream, classtype):
                storage[item.cid] = item
            Ensure(len(storage) >= 0, "failed to read any items")

        def read_as_bus_no(storage, classtype):
            EnsureEqual(len(storage), 0)
            for item in read_section(stream, classtype):
                storage[item.bus_no] = item
            Ensure(len(storage) >= 0, "failed to read any items")

        for line in stream:
            line = line.strip()

            if len(line) == 0 or line.startswith("%"):
                continue
            elif title_matches(line, "Bus.con"):
                read_as_bus_no(self.busses, self.Bus)
            elif title_matches(line, "Line.con"):
                read_as_cid(self.lines, self.Line)
            elif title_matches(line, "SW.con"):
                read_as_bus_no(self.slack, self.Slack)
            elif title_matches(line, "PV.con"):
                read_as_bus_no(self.generators, self.Generator)
            elif title_matches(line, "PQ.con"):
                read_as_bus_no(self.loads, self.Load)
            elif title_matches(line, "Shunt.con"):
                read_as_bus_no(self.shunts, self.Shunt)
            elif title_matches(line, "Supply.con"):
                read_as_cid(self.supply, self.Supply)
            elif title_matches(line, "Demand.con"):
                read_as_bus_no(self.demand, self.Demand)
            else:
                raise Error("expected matlab section, got '" + line + "'")

        self.invariant()
Ejemplo n.º 13
0
    def read(self, stream):

        current_scen = None

        for line in stream:

            line = [x.lower() for x in line.split()]

            # comments
            if len(line) == 0 or line[0].startswith("#"):
                continue

            # title
            elif line[0].startswith("["):
                if current_scen is not None:
                    # logger.debug("Added Scenario: %s" % title)
                    self.add(current_scen)

                title = line[0][1:-1]
                simtype = line[1]

                if len(line) == 3:
                    count = int(line[2])
                else:
                    EnsureEqual(len(line), 2)
                    count = 1

                EnsureIn(simtype, set("pf opf".split()))
                current_scen = Scenario(title, simtype, count)

            # remove
            elif line[0] == "remove":
                if line[1] == "bus":
                    current_scen.kill_bus.append(int(line[2]))
                elif line[1] == "line":
                    current_scen.kill_line.append(line[2])
                elif line[1] == "generator":
                    current_scen.kill_gen.append(line[2])
                else:
                    raise Error("got %s expected (line, generator, bus)" %
                                line[1])

            # set
            elif line[0] == "set":
                if line[1:3] == ["all", "demand"]:
                    current_scen.all_demand = float(line[3])
                else:
                    raise Error("got %s expected 'demand'" % line[1])

            # results
            elif line[0] == "result":
                EnsureIn(line[1], set("pass fail error".split()))
                current_scen.result = line[1]

            # nothing else allowed
            else:
                raise Error("got %s expected (remove, set, result, [])" %
                            line[0])

        if current_scen is not None:
            # logger.debug("Added Scenario: %s" % title)
            self.add(current_scen)
Ejemplo n.º 14
0
 def handlePlayerAction(self, action: PlayerAction):
     raise Error(f'unknown PlayerAction: {action}')