def run(): """Run the evolution.""" if args.verbose and __name__ == '__main__': print "objective: minimise", eval_func.__doc__ if args.seed is not None: np.random.seed(args.seed) hof = tools.HallOfFame(1) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("min", np.min) try: algorithms.eaGenerateUpdate(toolbox, ngen=args.generations, stats=stats, halloffame=hof, verbose=True) except KeyboardInterrupt: print 'user terminated early' (score,) = hof[0].fitness.values print 'Score: %.2f $/MWh' % score print 'List:', [max(0, param) for param in hof[0]] set_generators(hof[0]) nem.run(context) context.verbose = True print context if args.transmission: x = context.exchanges.max(axis=0) print np.array_str(x, precision=1, suppress_small=True) f = open('results.json', 'w') obj = {'exchanges': x.tolist(), 'generators': context} json.dump(obj, f, cls=nem.Context.JSONEncoder) f.close()
def run_one(chromosome): """Annual cost of the system (in billion $).""" context.costs = costs.AETA2013_2030Mid(0.05, 1.86, 11, 27) context.costs.carbon = 0 set_generators(chromosome) context.verbose = 0 nem.run(context) context.verbose = args.v print context
def test_003(self): c = nem.Context() # Add 25 DR generators so that the abbreviated legend is used. for i in range(25): dr = nem.generators.DemandResponse(polygons.wildcard, 100, 0) c.generators += [dr] print len(c.generators) nem.run(c) nem.plot(c, filename='foo.png') os.unlink('foo.png')
def test_009(self): """A NSW generators runs in any set of regions that includes NSW.""" rgnset = [] for rgn in regions.All: rgnset.append(rgn) self.context.regions = rgnset gen = SuperGenerator(None) self.context.generators = [gen] nem.run(self.context) self.assertEqual(gen.runhours, self.context.timesteps)
def eval_func(chromosome): """Annual cost of the system (in billion $).""" set_generators(chromosome) nem.run(context) score, penalty, reason = cost(context, transmission_p=args.transmission) if args.trace_file is not None: # write the score and individual to the trace file with open(args.trace_file, 'a') as csvfile: writer = csv.writer(csvfile) writer.writerow([score, penalty, reason] + list(chromosome)) return score + penalty,
def eval_func(chromosome): """Average cost of energy (in $/MWh).""" set_generators(chromosome) nem.run(context) score, penalty, reason = cost(context) if args.trace_file is not None: # write the score and individual to the trace file with open(args.trace_file, 'a') as csvfile: writer = csv.writer(csvfile) writer.writerow([score, penalty, reason] + list(chromosome)) return score + penalty,
def test_012(self): """A NSW generator does not run in other regions.""" rgnset = [] # Skip NSW (first in the list). for rgn in regions.All[1:]: rgnset.append(rgn) self.context.regions = rgnset gen = SuperGenerator(None) self.context.generators = [gen] nem.run(self.context) self.assertEqual(gen.runhours, 0)
def test_008(self): """A NSW generator runs in NSW only.""" for rgn in regions.All: self.context.regions = [rgn] gen = SuperGenerator(None) self.context.generators = [gen] nem.run(self.context) if rgn == regions.nsw: self.assertEqual(gen.runhours, self.context.timesteps) else: self.assertEqual(gen.runhours, 0)
def test_002(self): c = nem.Context() # Make sure there is unserved energy by setting 2nd and # subsequent generator capacity to 0. for g in c.generators[1:]: g.set_capacity(0) nem.run(c) nem.plot(c, filename='foo.png') os.unlink('foo.png') nem.plot(c, filename='foo.png', spills=True) os.unlink('foo.png')
def test_011(self): """Running in two regions only produces limited interstate exchanges.""" for rgn1 in regions.All: for rgn2 in regions.All: if rgn1 is rgn2: continue self.context.regions = [rgn1, rgn2] nem.run(self.context, endhour=1) self.assertTrue(self.context.exchanges[0, rgn1, rgn1] >= 0) self.assertTrue(self.context.exchanges[0, rgn2, rgn2] >= 0) for i in regions.All: for j in regions.All: # Check that various elements of the exchanges matrix are 0. # Ignore: diagonals, [RGN1,RGN2] and [RGN2,RGN1]. if i != j and (i, j) != (rgn1, rgn2) and (i, j) != (rgn2, rgn1): self.assertEqual(self.context.exchanges[0, i, j], 0)
def test_010(self): """Running in one region only produces no interstate exchanges.""" for rgn in regions.All: if rgn is regions.snowy: continue self.context = nem.Context() self.context.track_exchanges = True self.context.regions = [rgn] self.context.generators = [generators.OCGT(regions.nsw, 100), generators.OCGT(regions.qld, 100), generators.OCGT(regions.sa, 100), generators.OCGT(regions.tas, 100), generators.OCGT(regions.vic, 100)] nem.run(self.context, endhour=100) self.assertEqual((self.context.exchanges[0] > 0).sum(), 1, 'Only one exchange > 0') self.assertTrue(self.context.exchanges[0, rgn, rgn] > 0, 'Only rgn->rgn is > 0')
def run_one(chromosome): """Run a single simulation.""" context.costs = costs.AETA2013_2030Mid(0.05, 1.86, 11, 27) context.costs.carbon = 0 set_generators(chromosome) context.verbose = args.v > 1 if args.transmission: context.track_exchanges = 1 nem.run(context) context.verbose = args.v > 0 print context if args.transmission: x = context.exchanges.max(axis=0) print np.array_str(x, precision=1, suppress_small=True) f = open('results.json', 'w') json.dump(x.tolist(), f) f.close()
def test_010(self): """Running in one region only produces no interstate exchanges.""" for rgn in regions.All: if rgn is regions.snowy: continue self.context = nem.Context() self.context.track_exchanges = True self.context.regions = [rgn] loadpoly = [k for k, v in rgn.polygons.items() if v > 0][0] nswpoly = [k for k, v in regions.nsw.polygons.items() if v > 0][0] qldpoly = [k for k, v in regions.qld.polygons.items() if v > 0][0] sapoly = [k for k, v in regions.sa.polygons.items() if v > 0][0] taspoly = [k for k, v in regions.tas.polygons.items() if v > 0][0] vicpoly = [k for k, v in regions.vic.polygons.items() if v > 0][0] self.context.generators = [generators.OCGT(nswpoly, 100), generators.OCGT(qldpoly, 100), generators.OCGT(sapoly, 100), generators.OCGT(taspoly, 100), generators.OCGT(vicpoly, 100)] nem.run(self.context, endhour=100) self.assertEqual((self.context.exchanges[0] > 0).sum(), 1, 'Only one exchange > 0') # FIXME: we need a numpy array that can be indexed from 1 self.assertTrue(self.context.exchanges[0, loadpoly - 1, loadpoly - 1] > 0, 'Only rgn->rgn is > 0')
def run(): """Run the evolution.""" if args.verbose and __name__ == '__main__': print "objective: minimise", eval_func.__doc__ if args.seed is not None: np.random.seed(args.seed) hof = tools.HallOfFame(1) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("min", np.min) algorithms.eaGenerateUpdate(toolbox, ngen=args.generations, stats=stats, halloffame=hof, verbose=True) (score,) = hof[0].fitness.values print 'Score: %.2f $/MWh' % score print 'List:', hof[0] set_generators(hof[0]) nem.run(context) context.verbose = True print context if args.transmission: print context.exchanges.max(axis=0)
def test_004(self): """100 MW fossil plant generates exactly 876,000 MWh.""" ccgt = generators.CCGT(regions.nsw, 100) self.context.generators = [ccgt] nem.run(self.context) self.assertEqual(sum(ccgt.hourly_power.values()), self.context.timesteps * 100)
def test_013(self): """Fossil plant records power generation history.""" ccgt = generators.CCGT(regions.nsw, 100) self.context.generators = [ccgt] nem.run(self.context) self.assertTrue(len(self.context.generators[0].hourly_power) > 0)
def test_002(self): """Demand equals approx. 204 TWh.""" self.context.generators = [] nem.run(self.context) self.assertEqual(math.trunc(self.context.demand.sum() / pow(10, 6)), 204)
"""A stub for profiling tools to run one basic simulation.""" import nem c = nem.Context() nem.run(c)
def test_003(self): """Power system with no generators meets none of the demand.""" self.context.generators = [] nem.run(self.context) self.assertEqual(math.trunc(self.context.unserved_energy), math.trunc(self.context.demand.sum()))
def test_001(self): c = nem.Context() c.regions = [regions.nsw, regions.vic, regions.sa] c.track_exchanges = True c.verbose = 1 nem.run(c)
def test_007(self): """Generation to meet minimum load + 1GW produces some spills.""" self.context.generators = [SuperGenerator(self.minload + 1000)] nem.run(self.context) self.assertTrue(self.context.spill.sum() > 0)
def test_006(self): """Generation to meet minimum load leads to no spills.""" self.context.generators = [SuperGenerator(self.minload)] nem.run(self.context) self.assertEqual(self.context.spill.sum(), 0)
def test_005(self): """Super generator runs every hour.""" gen = SuperGenerator(None) self.context.generators = [gen] nem.run(self.context) self.assertEqual(gen.runhours, self.context.timesteps)