def show_year_by_week(): from misc import as_csv weeks = range(52) days = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday".split() hour = 7 for week in weeks: weekly = [forecast_load(week, day, hour) for day in days] print as_csv([week] + weekly, "\t")
def show_day(): from misc import as_csv weeks = [0, 13, 26, 39] hours = range(24) day = "Monday" for hour in hours: quartly = [forecast_load(week, day, hour) for week in weeks] print as_csv([hour] + quartly, "\t")
def show_all(): from misc import as_csv weeks = range(52) days = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday".split() hours = range(24) result = [] for week in weeks: for day in days: for hour in hours: load = forecast_load(week, day, hour) result.append(load) print as_csv([day, week, hour, load], "\t") return result
def dicthash(self): """returns a text of the csv value *without* the count title or result. To be used to store a Scenario in a dict""" self.invariant() infoline = [self.simtype, self.all_demand] kills = self.kill_bus + self.kill_line + self.kill_gen return as_csv(infoline + kills, "\t")
def csv_write(self, stream): self.invariant() infoline = [ self.title, self.simtype, self.count, self.all_demand, self.result ] kills = self.kill_bus + self.kill_line + self.kill_gen stream.write(as_csv(infoline + kills, "\t") + "\n")
def simulate_cases(outage_batch, failure_batch, psat, summary_file, mismatch_file): clean_files() print "[C] simulate %d unique states with %d unique contingencies" % ( len(outage_batch), len(failure_batch)) for n, scenario in enumerate(outage_batch): try: print "[C] simulating state", n + 1, "of", int( math.ceil(len(outage_batch))) report = simulate_scenario(psat, scenario, False) scenario_psat = report_to_psat(report, psat) clean_files() mismatch_file.write( as_csv("---- ---- ---- ---- ---- ----".split()) + "\n") mismatch_file.write( as_csv([scenario.title] + list(scenario_psat.get_stats())) + "\n") mismatch_file.write( as_csv("---- ---- ---- ---- ---- ----".split()) + "\n") print "[C] simulating state - prep done." for x in failure_batch: x.result = None batch_simulate(failure_batch, scenario_psat, 100, True, mismatch_file) filename = scenario.title + ".txt" with open(filename, "w") as result_file: failure_batch.csv_write(result_file) print "-" * 80 print "---- Outage Case %d Stats ----" % n failure_batch.write_stats(sys.stdout) summary_file.write("Outage Case %d Stats\n" % n) summary_file.write("%s\n" % ("-" * 80)) failure_batch.write_stats(summary_file) print "[C] simulating state", n + 1, "done" except Exception as exce: print "[E] Error Caught at main.simulate_cases (%s)" % scenario.title print exce raise
def write(self, stream): stream.write("# NetworkProbability data file\n") stream.write("# bus " + as_csv(self.Bus.entries, " ") + "\n") for bus in self.busses: stream.write("bus " + str(bus) + "\n") stream.write("# line " + as_csv(self.Line.entries, " ") + "\n") for line in self.lines: stream.write("line " + str(line) + "\n") stream.write("# generator " + as_csv(self.Generator.entries, " ") + "\n") for generator in self.generators: stream.write("generator " + str(generator) + "\n") stream.write("# crow " + as_csv(self.Crow.entries, " ") + "\n") for crow in self.crows: stream.write("crow " + str(crow) + "\n")
def simulate_cases(outage_batch, failure_batch, psat, summary_file, mismatch_file): clean_files() print "[C] simulate %d unique states with %d unique contingencies" % ( len(outage_batch), len(failure_batch)) for n, scenario in enumerate(outage_batch): try: print "[C] simulating state", n + 1, "of", int(math.ceil(len(outage_batch))) report = simulate_scenario(psat, scenario, False) scenario_psat = report_to_psat(report, psat) clean_files() mismatch_file.write(as_csv("---- ---- ---- ---- ---- ----".split()) + "\n") mismatch_file.write(as_csv([scenario.title] + list(scenario_psat.get_stats())) + "\n") mismatch_file.write(as_csv("---- ---- ---- ---- ---- ----".split()) + "\n") print "[C] simulating state - prep done." for x in failure_batch: x.result = None batch_simulate(failure_batch, scenario_psat, 100, True, mismatch_file) filename = scenario.title + ".txt" with open(filename, "w") as result_file: failure_batch.csv_write(result_file) print "-" * 80 print "---- Outage Case %d Stats ----" % n failure_batch.write_stats(sys.stdout) summary_file.write("Outage Case %d Stats\n" % n) summary_file.write("%s\n" % ("-"*80)) failure_batch.write_stats(summary_file) print "[C] simulating state", n + 1, "done" except Exception as exce: print "[E] Error Caught at main.simulate_cases (%s)" % scenario.title print exce raise
def write_section(stream, items, title): """write one section of a Matlab file""" if len(items) == 0: return stream.write(title + ".con = [ ... \n") for _, value in sorted(items.items()): if "cid" == value.entries[-1]: tmp = as_csv([value.__dict__[x] for x in value.entries[:-1]], " ") stream.write(" " + tmp + "; %" + value.cid + "\n") else: stream.write(" " + str(value) + ";\n") stream.write("];\n\n")
def main(infilea, infileb, outfile): summary = dict() for line in infilea: # id, ...rest.. items = [x.strip() for x in line.split(",")] scen_id = int(items[0]) scenario = scenario_from_csv(items[1:]) summary[scenario] = (scen_id, 0, "NONE", "") # for k, v in summary.items(): # print str(k) + misc.as_csv(v, ", ") for line in infileb: # num-fail, count, type, result, reason, ..rest.. # 1313 1 outage TRUE ok 0.7 G17 G85 G4 G13 G10 items = [x.strip() for x in line.split(",")] num_fail = int(items[0]) assert 0 <= num_fail <= 10000000 result = items[3] assert result in set(["TRUE", "FALSE", "NONE"]) reason = items[4] scenario = scenario_from_csv(items[5:]) # print str(scenario) # for k, v in summary.items(): # print str(k) # print scenario == k, str(scenario) == str(k) assert scenario in summary.keys(), str(scenario) assert summary[scenario][1] == 0, str(scenario) scen_id = summary[scenario][0] info = (scen_id, num_fail, result, reason) summary[scenario] = info for scenario, info in summary.items(): outfile.write(misc.as_csv(info, ",") + "\n")
def csv_write(self, stream): self.invariant() infoline = [self.title, self.simtype, self.count, self.all_demand, self.result] kills = self.kill_bus + self.kill_line + self.kill_gen stream.write(as_csv(infoline + kills, "\t") + "\n")
def output_group_stats(grp): group_size = sum(c for c, s in grp) failures = float(sum(c for c, s in grp if s.result is False)) pfail.append(failures / group_size) out_stream.write(as_csv([failures, group_size, failures / group_size], ",") + ",") out_stream.write(str(count) + "," + str(current_scenario) + "\n")
def batch_simulate(batch, psat, size=10, clean=True, mismatch_file=None): """func batch_simulate :: SimulationBatch, PsatData, Int -> ---- Simulate all Scenarios in `batch` (with a base of `psat`) in groups of size `size`. Modify `batch` in place. delete all temp files if it succedes """ print "[b] batch simulate %d cases" % len(batch) for n, group in enumerate(split_every(size, batch)): try: timer_start = time.clock() print "[b] simulating batch", n + 1, "of", int( math.ceil(len(batch) / size)) + 1 sys.stdout.flush() # make the matlab_script matlab_filename = "matlab_" + str(n) batch_matlab_script(matlab_filename + ".m", group) # write all the scenarios to file as psat_files for scenario in group: scenario.result = None try: new_psat = scenario_to_psat(scenario, psat) except Exception as exce: print "[E] Error Caught at script.batch_simulate (%s) - failed to convert scenario to psat" % scenario.title print exce scenario.result = "error" # we probably shouldn't have this but it might cause error in matlab as it is expected. new_psat = deepcopy(psat) if mismatch_file: mismatch_file.write( as_csv([scenario.title] + list(new_psat.get_stats())) + "\n") new_psat_filename = "psat_" + scenario.title + ".m" with open(new_psat_filename, "w") as new_psat_file: new_psat.write(new_psat_file) # run matlab resutls = simulate(matlab_filename, False) EnsureEqual(len(resutls), len(group)) for res, scenario in zip(resutls, group): if not (res): print "[b] did not converge (%s)" % scenario.title scenario.result = "fail" # gather results for scenario in group: if not scenario.result: report_filename = "psat_" + scenario.title + "_01.txt" try: report = read_report(report_filename) scenario.result = report_in_limits(report) except Exception as exce: print "[E] Error Caught at script.batch_simulate (%s) - report check failure" % scenario.title print exce scenario.result = "error" timer_end = time.clock() timer_time = (timer_end - timer_start) print "[b] batch time of", int(math.ceil(timer_time)), "seconds" except Exception as exce: print "[E] Error Caught at script.batch_simulate (%s) - failed to simulate batch" % scenario.title print exce if clean: clean_files()
def batch_simulate(batch, psat, size=10, clean=True, mismatch_file=None): """func batch_simulate :: SimulationBatch, PsatData, Int -> ---- Simulate all Scenarios in `batch` (with a base of `psat`) in groups of size `size`. Modify `batch` in place. delete all temp files if it succedes """ print "[b] batch simulate %d cases" % len(batch) for n, group in enumerate(split_every(size, batch)): try: timer_start = time.clock() print "[b] simulating batch", n + 1, "of", int(math.ceil(len(batch) / size)) + 1 sys.stdout.flush() # make the matlab_script matlab_filename = "matlab_" + str(n) batch_matlab_script(matlab_filename + ".m", group) # write all the scenarios to file as psat_files for scenario in group: scenario.result = None try: new_psat = scenario_to_psat(scenario, psat) except Exception as exce: print "[E] Error Caught at script.batch_simulate (%s) - failed to convert scenario to psat" % scenario.title print exce scenario.result = "error" # we probably shouldn't have this but it might cause error in matlab as it is expected. new_psat = deepcopy(psat) if mismatch_file: mismatch_file.write(as_csv([scenario.title] + list(new_psat.get_stats())) + "\n") new_psat_filename = "psat_" + scenario.title + ".m" with open(new_psat_filename, "w") as new_psat_file: new_psat.write(new_psat_file) # run matlab resutls = simulate(matlab_filename, False) EnsureEqual(len(resutls), len(group)) for res, scenario in zip(resutls, group): if not(res): print "[b] did not converge (%s)" % scenario.title scenario.result = "fail" # gather results for scenario in group: if not scenario.result: report_filename = "psat_" + scenario.title + "_01.txt" try: report = read_report(report_filename) scenario.result = report_in_limits(report) except Exception as exce: print "[E] Error Caught at script.batch_simulate (%s) - report check failure" % scenario.title print exce scenario.result = "error" timer_end = time.clock() timer_time = (timer_end - timer_start) print "[b] batch time of", int(math.ceil(timer_time)), "seconds" except Exception as exce: print "[E] Error Caught at script.batch_simulate (%s) - failed to simulate batch" % scenario.title print exce if clean: clean_files()
def __str__(self): return misc.as_csv([self.bus_level] + list(self.kill_list), ", ")
def generate_cases(n_outages=10, n_failures=1000, sim=True, full_sim=True): timer_begin = time.clock() timer_start = timer_begin print "[G] start simulation with %d states and %d contingencies." % (n_outages, n_failures) if full_sim: Ensure(n_outages and n_failures and sim, "can only do full sim if we have everything") clean_files() batch_size = 100 psat = read_psat("rts.m") prob = read_probabilities("rts.net") # create the base cases by sampling for outages # simulate these and print to a file. # it should contain `n_outages` outages. summary_file = open("summary.txt", "w") mismatch_file = open("mismatch.txt", "w") print "Base Stats: mis= %f gen= %f load= %f lim ( %f < X < %f )" % psat.get_stats() mismatch_file.write(as_csv("title mismatch gen load min max".split()) + "\n") mismatch_file.write(as_csv(["base"] + list(psat.get_stats())) + "\n") try: if n_outages: outage_batch = make_outage_cases(prob, n_outages) if sim: batch_simulate(outage_batch, psat, batch_size, True, mismatch_file) with open("outage.txt", "w") as result_file: outage_batch.csv_write(result_file) print "-" * 80 print "---- Outage Stats ----" outage_batch.write_stats(sys.stdout) summary_file.write("%s\n" % ("-"*80)) summary_file.write("Outage Stats\n") summary_file.write("%s\n" % ("-"*80)) outage_batch.write_stats(summary_file) timer_end = time.clock() timer_time = (timer_end - timer_start) print "[G] outages created in %d seconds." % int(math.ceil(timer_time)) timer_start = time.clock() # do the same for one hour changes to the system. if n_failures: failure_batch = make_failure_cases(prob, n_failures) if sim: batch_simulate(failure_batch, psat, batch_size, True, mismatch_file) with open("failure.txt", "w") as result_file: failure_batch.csv_write(result_file) print "-" * 80 print "---- Failure Stats ----" failure_batch.write_stats(sys.stdout) summary_file.write("Failure Stats\n") summary_file.write("%s\n" % ("-"*80)) failure_batch.write_stats(summary_file) timer_end = time.clock() timer_time = (timer_end - timer_start) print "[G] failures created in %d seconds." % int(math.ceil(timer_time)) timer_start = time.clock() # simulate each of the changes to each base case if full_sim: simulate_cases(outage_batch, failure_batch, psat, summary_file, mismatch_file) timer_end = time.clock() timer_time = (timer_end - timer_start) print "[G] full sim in %d seconds." % int(math.ceil(timer_time)) timer_start = time.clock() finally: timer_end = time.clock() timer_time = (timer_end - timer_begin) print "[G] total time %d seconds." % int(math.ceil(timer_time))
def generate_cases(n_outages=10, n_failures=1000, sim=True, full_sim=True): timer_begin = time.clock() timer_start = timer_begin print "[G] start simulation with %d states and %d contingencies." % ( n_outages, n_failures) if full_sim: Ensure(n_outages and n_failures and sim, "can only do full sim if we have everything") clean_files() batch_size = 100 psat = read_psat("rts.m") prob = read_probabilities("rts.net") # create the base cases by sampling for outages # simulate these and print to a file. # it should contain `n_outages` outages. summary_file = open("summary.txt", "w") mismatch_file = open("mismatch.txt", "w") print "Base Stats: mis= %f gen= %f load= %f lim ( %f < X < %f )" % psat.get_stats( ) mismatch_file.write( as_csv("title mismatch gen load min max".split()) + "\n") mismatch_file.write(as_csv(["base"] + list(psat.get_stats())) + "\n") try: if n_outages: outage_batch = make_outage_cases(prob, n_outages) if sim: batch_simulate(outage_batch, psat, batch_size, True, mismatch_file) with open("outage.txt", "w") as result_file: outage_batch.csv_write(result_file) print "-" * 80 print "---- Outage Stats ----" outage_batch.write_stats(sys.stdout) summary_file.write("%s\n" % ("-" * 80)) summary_file.write("Outage Stats\n") summary_file.write("%s\n" % ("-" * 80)) outage_batch.write_stats(summary_file) timer_end = time.clock() timer_time = (timer_end - timer_start) print "[G] outages created in %d seconds." % int( math.ceil(timer_time)) timer_start = time.clock() # do the same for one hour changes to the system. if n_failures: failure_batch = make_failure_cases(prob, n_failures) if sim: batch_simulate(failure_batch, psat, batch_size, True, mismatch_file) with open("failure.txt", "w") as result_file: failure_batch.csv_write(result_file) print "-" * 80 print "---- Failure Stats ----" failure_batch.write_stats(sys.stdout) summary_file.write("Failure Stats\n") summary_file.write("%s\n" % ("-" * 80)) failure_batch.write_stats(summary_file) timer_end = time.clock() timer_time = (timer_end - timer_start) print "[G] failures created in %d seconds." % int( math.ceil(timer_time)) timer_start = time.clock() # simulate each of the changes to each base case if full_sim: simulate_cases(outage_batch, failure_batch, psat, summary_file, mismatch_file) timer_end = time.clock() timer_time = (timer_end - timer_start) print "[G] full sim in %d seconds." % int(math.ceil(timer_time)) timer_start = time.clock() finally: timer_end = time.clock() timer_time = (timer_end - timer_begin) print "[G] total time %d seconds." % int(math.ceil(timer_time))
def __str__(self): return misc.as_csv([ self.scenario_type, self.result, self.result_reason, self.bus_level ] + list(self.wind_levels) + list(self.kill_list), ", ")