Esempio n. 1
0
    def testCreateTraining(self):
        """ no exceptions may be thrown """
        if Constants.thoroughness >= 2:
            env = SoftEnvironment()
            morph = SpringMorphology(noNodes=10,
                                     spring=1000,
                                     noNeighbours=3,
                                     environment=env)
            control = SineControl(morph)
            state = RobotState(0, morph)
            robot = Robot(morph, control, state)

            plotter = Plotter(plotCycle=50, plot=False)
            simulenv = SimulationEnvironment(timeStep=0.0005,
                                             simulationLength=2000,
                                             plot=plotter)

            simul = Simulation(simulenv, robot)
            simul.runSimulation()

            trainscheme = TrainingScheme()
            trainscheme.createTrainVariable("spring", 0, 3000)
            trainscheme.createTrainVariable("phase", 0, 2 * np.pi)

            training = RandomTraining(trainscheme, robot, simulenv)
            trainSave = Save(training, 'temp', 'default')
            trainSave.save([[10, 10], [20, 20]])
        else:
            print "testthoroughness is set too low for this test"
Esempio n. 2
0
def train_controller(json_path, dreaming=False):
    s = Simulation(json_path)

    es = cma.CMAEvolutionStrategy(s.controller.num_params * [0], 0.5)
    n_iters = 0
    rewards = []
    while not es.stop() or n_iters >= s.params['cma-es_hps']['max_iters']:
        # TODO: Number of models to make to be a param in the json
        solutions = es.ask()
        loss = []
        for x in solutions:
            s.controller.set_weights(x)
            loss.append(s.simulate(dreaming=dreaming))
        es.tell(solutions, loss)

        reward = -sum(loss)
        rewards.append(reward)
        best_sol = solutions[np.argmin(loss)]

        # es.logger.add()

        if n_iters % 100 == 0:
            # TODO: Better naming
            np.save(
                '../' + s.params['cma-es_hps']['weights_dir'] +
                '/cma_model_rewards', np.array(rewards))
            np.save(
                '../' + s.params['cma-es_hps']['weights_dir'] +
                '/cma_model_{}'.format(n_iters), np.array(best_sol))
        n_iters += 1
Esempio n. 3
0
    def init_sim(self):
        self.stdscr.addstr(0, 0, "Initialising simulation...")
        self.stdscr.noutrefresh()
        curses.doupdate()

        self.sim = Simulation(config)

        self.stats = {
            "Time":
            self.get_info(self.sim, "t"),
            "Step delay":
            self.get_info(self, "step_delay"),
            "Frameskip":
            self.get_info(self, "frameskip"),
            "Actors":
            self.get_info(
                self.sim,
                "num_actors",
                func=True,
            ),
            "Human inf.rate":
            self.get_info(self.sim.stats, "infected_percentage", ['h'], True,
                          True),
            "Acquired resistance":
            self.get_info(self.sim.stats, "resistance_percentage", ['h'], True,
                          True),
            "Human vax.rate":
            self.get_info(self.sim.stats, "vaccinated_percentage", ['h'], True,
                          True)
        }
Esempio n. 4
0
def embed(vnsPerWin, windows, isInf, linkRate, maxCPU, maxBw, maxLife):
    sn_structure = Network.mkGraph(100, 0.5, 100, 1000)
    SN = SubstrateN(sn_structure["nodes"],sn_structure["links"])
    SN.findKShortestPaths()

    test = Simulation(SN, vnsPerWin, windows)
    test.dispatch(isInf, linkRate, maxCPU, maxBw, maxLife)
Esempio n. 5
0
    def __init__(self, site_id=1, period_id=1, battery_id=1):
        """
        Resets the state of the environment and returns an initial observation.

        # Returns
            observation (object): The initial observation of the space. Initial reward is assumed
                                  to be 0.
        """
        self.actual_previous_pv = []
        self.actual_previous_load = []
        self.battery_charge = []
        self.battery_energy = []
        self.grid_energy = []
        self.money_saved = []
        self.score = []
        self.current_timestep_idx = 0
        simulation_dir = (Path(__file__) / os.pardir / os.pardir).resolve()
        data_dir = simulation_dir / 'data'

        # load available metadata to determine the runs
        metadata_path = data_dir / 'metadata.csv'
        metadata = pd.read_csv(metadata_path, index_col=0)

        self.site_id = site_id
        self.period_id = period_id
        self.battery_id = battery_id

        site_data_path = data_dir / "submit" / f"{self.site_id}.csv"
        site_data = pd.read_csv(site_data_path,
                                parse_dates=['timestamp'],
                                index_col='timestamp')

        parameters = metadata.loc[self.site_id]
        battery = Battery(
            capacity=parameters[f"Battery_{self.battery_id}_Capacity"] * 1000,
            charging_power_limit=parameters[f"Battery_{self.battery_id}_Power"]
            * 1000,
            discharging_power_limit=-parameters[
                f"Battery_{self.battery_id}_Power"] * 1000,
            charging_efficiency=parameters[
                f"Battery_{self.battery_id}_Charge_Efficiency"],
            discharging_efficiency=parameters[
                f"Battery_{self.battery_id}_Discharge_Efficiency"])

        # n_periods = site_data.period_id.nunique() # keep as comment for now, might be useful later
        g_df = site_data[site_data.period_id == self.period_id]

        self.simulation = Simulation(g_df, battery, self.site_id)
        self.battery_controller = BatteryContoller()
    def test_with_and_with_out_my_algorithm(self):
        s = Simulation(nodes=20, simulate_times=1000, use_my_algorithm=False)
        s.run()
        print("With out my algorithm: "+str(s.all_nodes_received_the_packet_freq))

        s = Simulation(nodes=20, simulate_times=1000, use_my_algorithm=True)
        s.run()
        print("With my algorithm: " + str(s.all_nodes_received_the_packet_freq))
Esempio n. 7
0
 def run(self):
     for i in range(self.roundNumber):
         result = []
         print('Generation ' + str(i + 1) + ':')
         for g in self.genes:
             print('\tGene String ' + g.geneStr)
             self.carmap.updateGeneInfo(GeneInfo(g))
             simulation = Simulation(self.cars, self.carmap)
             (total, average) = simulation.run(False, 10000)
             print('\tTotal: ' + str(total) + ' Average: ' + str(average) + '\n')
             if average == -1:
                 self.carmap.clearAllCars()
                 continue
             result.append((average, g))
         result.sort(key = lambda x:x[0])
         self.addResults(result[0:int(self.geneNumber / 2 )+ 1])
         self.evolve(result[0:int(self.geneNumber / 2 )+ 1])
     return self.results
Esempio n. 8
0
def Plotter(simulation_list: simulate.Simulation,
            x_units='bullet',
            y_units='total damage'):
    ''' Le plot.
    '''

    unit_dict = ({
        'bullet': [0, 'Bullet Number'],
        'time': [1, 'Time'],
        'ammo': [2, 'Ammo Left'],
        'damage': [3, 'Damage'],
        'total damage': [4, 'Total Damage'],
        'impact': [5, 'Impact Damage'],
        'puncture': [6, 'Puncture Damage'],
        'slash': [7, 'Slash Damage'],
        'cold': [8, 'Cold Damage'],
        'electricity': [9, 'Electrical Damage'],
        'heat': [10, 'Heat Damage'],
        'toxin': [11, 'Toxin Damage'],
        'blast': [12, 'Blast Damage'],
        'corrosive': [13, 'Corrosive Damage'],
        'gas': [14, 'Gas Damage'],
        'magnetic': [15, 'Magnetic Damage'],
        'radiation': [16, 'Radiation Damage'],
        'viral': [17, 'Viral Damage']
    })

    # Check to make sure x_units and y_units are in unit_dict
    if (x_units not in unit_dict.keys()) or (y_units not in unit_dict.keys()):
        raise Exception('{} or {} is not an available unit'.format(
            x_units, y_units))

    for simulation in simulation_list:
        # Plot the values
        x_values = ([
            column[unit_dict[x_units][0]] for column in simulation.array
        ])

        y_values = ([
            column[unit_dict[y_units][0]] for column in simulation.array
        ])

        plt.plot(x_values,
                 y_values,
                 c='C' + str(simulation_list.index(simulation)),
                 label=LabelStringCreater(simulation_list, simulation))

        plt.xlabel(unit_dict[x_units][1])
        plt.ylabel(unit_dict[y_units][1])
        plt.title(
            TitleStringCreator(simulation_list, simulation, x_units, y_units,
                               unit_dict))
        plt.legend()
    plt.show()
Esempio n. 9
0
def simpleSimulation(robot, timeStep=1e-3, simulationLength=1000, verlet=True):
    """ create a simple simulation without any plotting """
    plotenv = Plotter(plot=False)
    simulenv = SimulationEnvironment(timeStep=timeStep,
                                     simulationLength=simulationLength,
                                     plot=plotenv,
                                     verlet=verlet)
    if verlet:
        simulation = VerletSimulation(simulenv, robot)
    else:
        simulation = Simulation(simulenv, robot)
    return simulation
	def testCreateTraining(self):
		""" no exceptions may be thrown """
		if Constants.thoroughness >= 2:
			env=SoftEnvironment()
			morph=SpringMorphology(noNodes = 10,spring = 1000, noNeighbours = 3,environment = env)
			control=SineControl(morph)
			state=RobotState(0,morph)
			robot=Robot(morph,control,state)

			plotter =Plotter(plotCycle=50,plot=False);
			simulenv=SimulationEnvironment(timeStep = 0.0005,simulationLength=2000,plot =plotter)

			simul = Simulation(simulenv,robot)
			simul.runSimulation()

			trainscheme = TrainingScheme()
			trainscheme.createTrainVariable("spring",0,3000)
			trainscheme.createTrainVariable("phase",0,2*np.pi)

			training=RandomTraining(trainscheme,robot,simulenv)
			trainSave = Save(training, 'temp', 'default')
			trainSave.save([[10,10],[20,20]])
		else: print "testthoroughness is set too low for this test"
Esempio n. 11
0
    def evaluateParam(self, param):
        """ calculate the performance index of a parameter set"""

        self.trainscheme.normalizedList2robot(param, self.robot)
        # FIX NOISE
        if self.simulEnv.verlet:
            if self.simulEnv.noisy:
                score = NoisyImpulseVerletSimulation(
                    self.simulEnv, self.robot).runSimulation()
            else:
                score = VerletSimulation(self.simulEnv,
                                         self.robot).runSimulation()
        else:
            score = Simulation(self.simulEnv, self.robot).runSimulation()

        params = dict()
        i = 0
        j = 0
        for p in self.trainscheme.trainableParams:
            params[p.name] = param[j:j + self.lenParamList[i]].tolist()
            j += self.lenParamList[i]
            i += 1
        self.addResult(params, score)
        return score[0]
Esempio n. 12
0
import sys
from simulate import RVContinuous, Simulation

# unpack command-line arguments
sample_size = int(sys.argv[1])


# target probability distribution
def target_cdf(x):
    if x >= 2.0 and x <= 3.0:
        return 0.25 * (x - 2.0)**2
    elif x > 3.0 and x <= 6.0:
        return 0.25 + (x - 3.0) * (9.0 - x) / 12.0
    else:
        return 0.0


# inverse of the target distribution
def inv_cdf(y):
    if y <= 0.25:
        return 2 * (y**0.5 + 1)
    else:
        return 6 - 2 * (3 * (1 - y))**0.5


# simulate and compare
rv = RVContinuous(support=[2.0, 6.0], cdf=target_cdf)
sim = Simulation(target_rv=rv, algorithm='inverse', inv_cdf=inv_cdf)
sim.generate(sample_size)
sim.compare(file_path='../images/p2_{}.png'.format(sample_size))
Esempio n. 13
0
# target probability distribution
def target_cdf(x, *args):
    return sum([a * x**(i + 1) for i, a in enumerate(alpha)])


# distribution for x^n
def cdf(y, n):
    return x**n


# inverse for the distribution x^n
def inv_cdf(y, n):
    return y**(1.0 / n)


# simulate and compare
sim_components = [
    Simulation(RVContinuous(support=[0.0, 1.0], cdf=cdf, n=i + 1),
               algorithm='inverse',
               inv_cdf=inv_cdf) for i in range(degree)
]
rv = RVContinuous(support=[0.0, 1.0], cdf=target_cdf)
sim = Simulation(target_rv=rv,
                 algorithm='composition',
                 sim_components=sim_components,
                 probabilties=alpha)
sim.generate(sample_size)
sim.compare(file_path='../images/p8c_{}_{}.png'.format(degree, sample_size))
print('The generated probability weights are:\n{}'.format(alpha))
Esempio n. 14
0
class Gui:
    def __init__(self, config):
        self.config = config
        self.stdscr = curses.initscr()
        if not curses.has_colors():
            raise RuntimeError("Your terminal must support colors!")
        curses.start_color()

        self.verify_screen_size()

        required_colors = [
            0,  #1
            46,  #2   (vaccinated, green)
            148,  #3
            142,  #4
            136,  #5
            130,  #6
            202,  #7 (infected - orange)
            196,  #8  (resistant, but infected - red)
            226,  #9 
            220,  #10
            214,  #11
            208,  #12
            202,  #13
            196,  #14
            51,  #15 light blue (netted)
        ]

        for i in range(1, len(required_colors) + 1):
            curses.init_pair(i, required_colors[i - 1], 0)
            curses.init_pair(i + 128, required_colors[i - 1], 236)

        curses.init_pair(128, 250, 236)

        curses.noecho()
        curses.cbreak()
        self.stdscr.keypad(True)

        self.running = True
        self.n = 0
        self.step_delay = 0.0025
        self.frameskip = 0

    def verify_screen_size(self):
        """ Verifies that the terminal we are running in is large enough. """
        height, width = self.stdscr.getmaxyx()

        grid_x, grid_y = self.config.Grid.size

        required_x = 2 * grid_x + 24  # sidebar
        required_y = grid_y + 2

        if (required_x > width or required_y > height):
            self.cleanup()

            print("Your terminal is too small.")
            print("The required terminal size for this configuration is: "
                  f"{required_x} columns, {required_y} lines.")
            print(f"Your current terminal size is: {width} columns, "
                  f"{height} lines.")

            raise SystemExit

    def init_sim(self):
        self.stdscr.addstr(0, 0, "Initialising simulation...")
        self.stdscr.noutrefresh()
        curses.doupdate()

        self.sim = Simulation(config)

        self.stats = {
            "Time":
            self.get_info(self.sim, "t"),
            "Step delay":
            self.get_info(self, "step_delay"),
            "Frameskip":
            self.get_info(self, "frameskip"),
            "Actors":
            self.get_info(
                self.sim,
                "num_actors",
                func=True,
            ),
            "Human inf.rate":
            self.get_info(self.sim.stats, "infected_percentage", ['h'], True,
                          True),
            "Acquired resistance":
            self.get_info(self.sim.stats, "resistance_percentage", ['h'], True,
                          True),
            "Human vax.rate":
            self.get_info(self.sim.stats, "vaccinated_percentage", ['h'], True,
                          True)
        }

    def get_info(
        self,
        obj,
        name,
        args=[],
        func=False,
        trunc=False,
    ):
        if func:
            return getattr(obj, name), args, trunc
        else:

            def info():
                return getattr(obj, name)

            return info, args, trunc

    def run(self):
        self.init_sim()
        try:
            self.draw_border()
            self.draw()
            while self.running:
                self.handle_input()
        except curses.error:
            self.verify_screen_size()
        finally:
            self.cleanup()

    def cleanup(self):
        curses.nocbreak()
        curses.echo()
        self.stdscr.keypad(False)
        curses.endwin()

    def handle_input(self):
        c = self.stdscr.getch()

        if c == ord('n'):
            self.sim.step()
            self.draw()

        if c == ord('c'):
            self.stdscr.nodelay(True)

            mod = 1

            while True:
                time.sleep(self.step_delay)
                ch = self.stdscr.getch()
                if (ch == ord('c')):
                    break
                elif (ch == ord('+')):
                    self.step_delay /= 2
                elif (ch == ord('-')):
                    self.step_delay *= 2
                elif (ch == ord(',')):
                    self.frameskip = max(0, self.frameskip - 1)
                elif (ch == ord('.')):
                    self.frameskip += 1
                elif (ch == ord('v')):
                    self.sim.vax_mosquitos = True
                elif (ch == ord('k')):
                    self.sim.use_net = True

                self.sim.step()
                if (self.sim.t % (1 + self.frameskip) == 0):
                    self.draw()
                self.stdscr.refresh()
            self.stdscr.nodelay(False)

        if c == ord('q'):
            self.running = False

        if c == ord('r'):
            raise ResetException()

        if c == ord('v'):
            self.sim.vax_mosquitos = True

        if c == ord('p'):
            self.plot_plots()

    def plot_plots(self):
        self.sim.stats.plot("population", "mh", save_fig=True)
        self.sim.stats.plot("infected_percentage", "mh", save_fig=True)
        self.sim.stats.plot("resistance_percentage", "h", save_fig=True)
        self.sim.stats.plot("vaccinated_percentage", "h", save_fig=True)

    def draw_border(self):
        for x in range(2 * self.sim.grid.x_max + 21):
            if x == 2 * self.sim.grid.x_max:
                continue
            self.put(0, x + 1, "═")
            self.put(self.sim.grid.y_max + 1, x + 1, "═")
        for y in range(self.sim.grid.y_max):
            self.put(y + 1, 0, "║")
            self.put(y + 1, 2 * self.sim.grid.x_max + 1, "║")
            self.put(y + 1, 2 * self.sim.grid.x_max + 22, "║")
        self.put(0, 0, "╔")
        self.put(0, 2 * self.sim.grid.x_max + 1, "╦")
        self.put(self.sim.grid.y_max + 1, 0, "╚")
        self.put(self.sim.grid.y_max + 1, 2 * self.sim.grid.x_max + 1, "╩")
        self.put(0, 2 * self.sim.grid.x_max + 22, "╗")
        self.put(self.sim.grid.y_max + 1, 2 * self.sim.grid.x_max + 22, "╝")
        self.put(0, 2, "Simulation:")
        self.put(0, 2 * self.sim.grid.x_max + 3, "Statistics:")

    def draw_stats(self):
        x_off = 2 * self.sim.grid.x_max + 2
        for i, (k, (f, args, trunc)) in enumerate(self.stats.items()):
            self.put(1 + i * 3, x_off, f"{k}:")
            if args:
                val = f(*args)
            else:
                val = f()
            if trunc:
                self.put(2 + i * 3, x_off, f"{val:.2f}")
            else:
                self.put(2 + i * 3, x_off, f"{val}")
            self.stdscr.clrtoeol()
            self.put(3 + i * 3, x_off - 1, "╠════════════════════╣")

    def put(self, y, x, str, *args):
        self.stdscr.addstr(y, x, str.encode('utf-8'), *args)

    def redefine_colors(self, bgcolor):
        for i in range(1, curses.COLORS):
            curses.init_pair(i, i, bgcolor)

    def draw(self):
        self.draw_stats()

        x_max, y_max = self.sim.grid.x_max, self.sim.grid.y_max
        for x in range(x_max):
            for y in range(y_max):
                COLOR_BASE = 0
                if (x % 2 == y % 2):
                    COLOR_BASE = 128

                square = self.sim.grid[x, y]

                has_human = False
                has_mosquitos = False
                mosquito_infected = 0
                human_infected = False
                human_immune = False
                human_vaccinated = False
                human_use_net = False
                mosquito_vaccinated = False

                for actor in square:
                    if actor.is_mosquito():
                        has_mosquitos = True
                        if actor.infected:
                            mosquito_infected += 1
                        if actor.vaccinated:
                            mosquito_vaccinated = True
                    if actor.is_human():
                        has_human = True
                        if actor.infected:
                            human_infected = True
                        if actor.immune:
                            human_immune = True
                        if actor.vaccinated:
                            human_vaccinated = True
                        if actor.use_net:
                            human_use_net = True

                color = curses.color_pair(COLOR_BASE)

                if human_infected:
                    color = curses.color_pair(COLOR_BASE + 7)

                if human_vaccinated:
                    color = curses.color_pair(COLOR_BASE + 2)

                if human_immune:
                    color = curses.color_pair(COLOR_BASE + 8)

                if human_use_net:
                    color = curses.color_pair(COLOR_BASE + 15)

                self.put(y + 1, x * 2 + 1, "H" if has_human else " ", color)

                args = [y + 1, x * 2 + 2, "•" if has_mosquitos else " "]

                colors = [0, 9, 10, 11, 12, 13, 14]

                if mosquito_infected > 6:
                    mosquito_infected = 6

                color_pair = curses.color_pair(COLOR_BASE +
                                               colors[mosquito_infected])

                if mosquito_vaccinated:
                    color_pair = curses.color_pair(COLOR_BASE + 2)
                args.append(color_pair)

                self.put(*args)
Esempio n. 15
0
# target probability distribution
def target_cdf(x):
    return 1.0 - np.exp(-x) - np.exp(-2.0 * x) + np.exp(-3.0 * x)


# target probability density
def target_pdf(x):
    return np.exp(-x) + 2.0 * np.exp(-2.0 * x) - 3.0 * np.exp(-3.0 * x)


# define our random variable to be simulated
rv = RVContinuous(support=[0.0, np.inf], cdf=target_cdf, pdf=target_pdf)

# fisrt simulation
helper_rv = RVContinuous(support=[0.0, np.inf], pdf=lambda x: np.exp(-x))
helper_sim = Simulation(target_rv=helper_rv,
                        algorithm=lambda *args: np.random.exponential())
sim = Simulation(target_rv=rv,
                 algorithm='rejection',
                 helper_sim=helper_sim,
                 ratio_bound=4.0)
sim.generate(sample_size)
sim.compare(file_path='../images/p16_1_{}.png'.format(sample_size))

# second simulation
helper_rv = RVContinuous(support=[0.0, np.inf], pdf=lambda x: np.exp(-x / 2.0))
helper_sim = Simulation(
    target_rv=helper_rv,
    algorithm=lambda *args: np.random.exponential(scale=2.0))
sim.set_algorithm(algorithm='rejection',
                  helper_sim=helper_sim,
                  ratio_bound=3.0)
Esempio n. 16
0
# target probability distribution
def target_cdf(x):
    if x < 1.0:
        return (1 - np.exp(-2 * x) + 2 * x) / 3.0
    else:
        return (3 - np.exp(-2 * x)) / 3.0


# cdfs for the component distributions
cdfs = [
    lambda x, *args: 1.0 - np.exp(-2 * x), lambda x, *args: x
    if x < 1.0 else 1.0
]

# inverse for the component distributions
inv_cdfs = [lambda y, *args: -0.5 * np.log(1 - y), lambda y, *args: y]

# simulate and compare
rv = RVContinuous(support=[0.0, np.inf], cdf=target_cdf)
sim_components = [
    Simulation(RVContinuous(support=[0.0, np.inf], cdf=cdf),
               algorithm='inverse',
               inv_cdf=inv_cdf) for cdf, inv_cdf in zip(cdfs, inv_cdfs)
]
sim = Simulation(target_rv=rv,
                 algorithm='composition',
                 sim_components=sim_components,
                 probabilties=[1.0 / 3.0, 2.0 / 3.0])
sim.generate(sample_size)
sim.compare(file_path='../images/p8b_{}.png'.format(sample_size))
Esempio n. 17
0
            counter = 0
            for r in results[1::2]:
                counter += 1
                (a, s) = r
                print('[' + str(counter) + '] ' + '{0:.2f}'.format(a) + ' ' +
                      s)

            save(Result(args['layoutName'], cars), args['save'])
        else:
            mapLayout = args['layout']
            gene = Gene(mapLayout.getTrafficLights())
            geneInfo = GeneInfo(gene)
            carmap = CarMap(mapLayout, geneInfo)
            cars = randomStartEndPoint(args['number'])
            carmap = CarMap(mapLayout, geneInfo)
            simulation = Simulation(cars, carmap)

            app = Graphic(mapLayout.mapInfo, carmap.cars, carmap.trafficlights,
                          args['size'])
            start_new_thread(run, ())
            app.run()

    else:
        r = load(args['load'])
        mapLayout = getLayout(r.layout)
        geneStr = raw_input('Input Gene String: ')
        gene = Gene(mapLayout.getTrafficLights(), False, geneStr)
        geneInfo = GeneInfo(gene)
        carmap = CarMap(mapLayout, geneInfo)
        cars = r.cars
        simulation = Simulation(cars, carmap)
Esempio n. 18
0
# sampling algorithm for our random variable Y
def algorithm(*args):
    claim_count = np.random.choice([0, 1], size=n, p=[1 - p, p]).sum()
    return np.random.exponential(scale=mu, size=claim_count).sum()


# remove components with near zero probabilties for faster computation
significant_probabilites = []
significant_indices = []
for k in range(1, n + 1):
    try:
        probability = binom.pmf(k, n, p)
    except:
        probability = 0.0
    if probability > epsilon:
        significant_indices.append(k)
        significant_probabilites.append(probability)

# print('number of significant components = {}'.format(len(significant_probabilites)))

# simulate and compare
rv =  RVContinuous(support = [0.0, np.inf], cdf = target_cdf, find_mean = find_mean, find_var = find_var,\
                   shapes = significant_indices, scale = mu, weights = significant_probabilites)
sim = Simulation(target_rv=rv, algorithm=algorithm)
sim.generate(sample_size)
sim.compare(file_path='../images/p10_alt1_{}.png'.format(sample_size))

# display results
print('simulated probability = {}\nactual probability = {}'\
      .format(1.0 - sim.ecdf(A), 1.0 - target_cdf(A, shapes = significant_indices, scale = mu, weights = significant_probabilites)))
Esempio n. 19
0
class PvBatteryEnv(object):
    nb_time_steps = 959

    battery_charge = None
    money_saved = None
    current_timestep_idx = None

    actual_previous_load = None
    actual_previous_pv = None

    simulation = None
    battery_controller = None

    site_id = None
    battery_id = None
    period_id = None

    def __init__(self, site_id=1, period_id=1, battery_id=1):
        """
        Resets the state of the environment and returns an initial observation.
        # Returns
            observation (object): The initial observation of the space. Initial reward is assumed
                                  to be 0.
        """
        self.actual_previous_pv = []
        self.actual_previous_load = []
        self.battery_charge = []
        self.battery_energy = []
        self.grid_energy = []
        self.money_saved = []
        self.score = []
        self.current_timestep_idx = 0
        simulation_dir = (Path(__file__) / os.pardir / os.pardir).resolve()
        data_dir = "/Users/anastasiamarkelova/PycharmProjects/Smart energy/data"

        # load available metadata to determine the runs
        metadata_path = data_dir +'/metadata.csv'
        metadata = pd.read_csv(metadata_path, index_col=0,sep = ';')

        self.site_id = site_id
        self.period_id = period_id
        self.battery_id = battery_id

        site_data_path = data_dir +"/submit/"+f"{self.site_id}.csv"
        site_data = pd.read_csv(site_data_path, parse_dates=['timestamp'],
                                index_col='timestamp',sep = ';')

        parameters = metadata.loc[self.site_id]
        battery = Battery(capacity=parameters[f"Battery_{self.battery_id}_Capacity"] * 1000,
                          charging_power_limit=parameters[f"Battery_{self.battery_id}_Power"] * 1000,
                          discharging_power_limit=-parameters[f"Battery_{self.battery_id}_Power"] * 1000,
                          charging_efficiency=parameters[f"Battery_{self.battery_id}_Charge_Efficiency"],
                          discharging_efficiency=parameters[
                              f"Battery_{self.battery_id}_Discharge_Efficiency"])

        # n_periods = site_data.period_id.nunique() # keep as comment for now, might be useful later
        g_df = site_data[site_data.period_id == self.period_id]

        self.simulation = Simulation(g_df, battery, self.site_id)
        self.battery_controller = BatteryContoller()

    def step(self):
        """Run one timestep of the environment's dynamics.
        Accepts an action and returns a tuple (observation, reward, done, info).
        # Returns
            observation (object): Agent's observation of the current environment.
            done (boolean): Whether the episode has ended, in which case further step() calls
                            will return undefined results.
            info (dict): Contains auxiliary diagnostic information (helpful for debugging,
                         and sometimes learning).
        """

        money_saved, grid_energy = self.simulation.simulate_timestep(
            self.battery_controller,
            self.simulation.data.index[self.current_timestep_idx],
            self.simulation.data.iloc[self.current_timestep_idx])

        self.grid_energy.append(grid_energy)
        self.battery_charge.append(self.simulation.battery.current_charge)
        self.battery_energy.append(self.simulation.battery.capacity *
                                   self.simulation.battery.current_charge)
        self.money_saved.append(money_saved)
        self.score.append(self.simulation.score)
        self.actual_previous_load.append(self.simulation.actual_previous_load)
        self.actual_previous_pv.append(self.simulation.actual_previous_pv)

        self.current_timestep_idx += 1

    def render(self, mode='human', close=False):
        plt.subplot(2, 3, 1)
        plt.plot(self.battery_charge)
        plt.legend(['Charge', 'Target'])
        plt.title('Battery')

        plt.subplot(2, 3, 2)
        plt.plot(np.array(self.actual_previous_load) - np.array(self.actual_previous_pv))
        plt.title('Load - PV')

        plt.subplot(2, 3, 3)
        plt.plot(self.money_saved)
        plt.plot(np.cumsum(self.money_saved))
        plt.legend(['Instantaneous', 'Cumulative'])
        plt.title('Agregate reward')

        ax = plt.subplot(2, 3, 4)
        self.simulation.data[['price_sell_00', 'price_buy_00']].plot(ax=ax)

        plt.subplot(2, 3, 5)
        plt.plot(self.score)
        plt.title('Simulation score')

    def save(self):
        t = (self.battery_charge,
             self.battery_energy,
             self.simulation.data.actual_consumption,
             self.simulation.data.actual_pv,
             self.grid_energy,
             self.simulation.data['price_sell_00'],
             self.simulation.data['price_buy_00'],
             self.money_saved,
             self.score)

        # save data in pickle file
        simulation_dir = (Path(__file__) / os.pardir / os.pardir).resolve()
        pickle_path = "/Users/anastasiamarkelova/PycharmProjects/Smart energy/analysis_of_results/env_DDPG/"+f"env_s{self.site_id}_b{self.battery_id}_p{self.period_id}.p"
        with open(pickle_path, 'wb') as f:
            pickle.dump(t, f)
Esempio n. 20
0
significant_probabilites = []
significant_indices = []
for k in range(1, n + 1):
    try:
        probability = binom.pmf(k, n, p)
    except:
        probability = 0.0
    if probability > epsilon:
        significant_indices.append(k)
        significant_probabilites.append(probability)

# print('number of significant components = {}'.format(len(significant_probabilites)))

# simulate and compare
sim_components = []
for i in significant_indices:
    gamma_rv = RVContinuous(cdf=gamma_cdf, shape=i + 1, scale=mu)
    sim_components.append(Simulation(target_rv=gamma_rv, algorithm='gamma'))
rv =  RVContinuous(support = [0.0, np.inf], cdf = target_cdf, find_mean = find_mean, find_var = find_var,\
                   shapes = significant_indices, scale = mu, weights = significant_probabilites)
sim = Simulation(target_rv=rv,
                 algorithm='composition',
                 sim_components=sim_components,
                 probabilties=significant_probabilites)
sim.generate(sample_size)
sim.compare(file_path='../images/p10_{}.png'.format(sample_size))

# display results
print('simulated probability = {}\nactual probability = {}'\
      .format(1.0 - sim.ecdf(A), 1.0 - target_cdf(A, shapes = significant_indices, scale = mu, weights = significant_probabilites)))
Esempio n. 21
0
 def simulate(self):
     if len(self.link.getIDs()) == 0:
         popup("Nothing to simulate!", "Information")
         return
     self.sim = Simulation(self.link, self.arcana, self.level, self.angle)
Esempio n. 22
0
import numpy as np
from simulate import RVContinuous, Simulation

# a useful constant
c = 1.0 - np.exp(-0.05)


# target probability distribution
def target_cdf(x):
    return (1.0 - np.exp(-x)) / c


# inverse of the target distribution
def inv_cdf(y):
    return -np.log(1.0 - c * y)


# simulate and compare
rv = RVContinuous(support=[0.0, 0.05], cdf=target_cdf)
sim = Simulation(target_rv=rv, algorithm='inverse', inv_cdf=inv_cdf)
sim.generate(1000)
sim.compare(file_path='../images/p6.png')
print('simulated mean = {:.4f}\nexact mean = {:.4f}'.format(
    sim.mean, sim.rv.mean))