Esempio n. 1
0
    def __init__(self):
        NonBlockingDispatchAgent.__init__(self)
        self.process_s_dtnd = None
        self.process_c_dtnd = None
        self.process_dtncpd = None
        self.process_dtncp = None
	self.collection = database.getCollection(self.name)
	self.flag = True
Esempio n. 2
0
 def __init__(self):
     NonBlockingDispatchAgent.__init__(self)
     self.configFileName = 'AGCDR_agent.mat'
     self.loadProfileConfigFileName = 'AGCDR_profiles.mat'
     self.inactiveGenConfigFile = 'AGCDR_agent_removed_<removed-node>.mat'
     self.N_iter = 100
     self.thread = None
     self.active = True
Esempio n. 3
0
 def __init__(self):
     NonBlockingDispatchAgent.__init__(self)
     self.configFileName = 'AGCDR_agent.mat'
     self.loadProfileConfigFileName = 'AGCDR_profiles.mat'
     self.inactiveGenConfigFile = 'AGCDR_agent_removed_<removed-node>.mat'
     self.N_iter = 100
     self.thread = None
     self.active = True
Esempio n. 4
0
 def __init__(self, *args, **kwargs):
     try:
         NonBlockingDispatchAgent.__init__(self, *args, **kwargs)
         self.collectionMetadata = dict()
         self.events = dict()
         self.rcvdPongs = set()
         self.dbProcesses = set()
         self.dbLogHandler = None
         self.setupDatabase()
         #self.setupDBLogHandler()
     except:
         log.exception('Exception while initializing data manager')
         self.stop(None)
Esempio n. 5
0
 def __init__(self, *args, **kwargs):
     try:
         NonBlockingDispatchAgent.__init__(self, *args, **kwargs)
         self.collectionMetadata = dict()
         self.events = dict()
         self.rcvdPongs = set()
         self.dbProcesses = set()
         self.dbLogHandler = None
         self.setupDatabase()
         #self.setupDBLogHandler()
     except:
         log.exception('Exception while initializing data manager')
         self.stop(None)
Esempio n. 6
0
 def setConfiguration(self, msg, **kwargs):
     NonBlockingDispatchAgent.setConfiguration(self, msg, **kwargs)
     
     log.info("Loading Config File: %s", self.configFileName)
     self.config = scipy.io.loadmat(self.configFileName, mat_dtype=True)
     
     log.info("Hostname: %s", self.hostname)
     from magi.testbed import testbed
     self.hostname = testbed.nodename
     log.info("Hostname: %s", self.hostname)
     
     self.index = int(self.hostname.split("-")[-1])
     
     self.N_gen = np.squeeze(self.config['N_gen'])
     
     self.c_G = self.config['c_G'][self.index, self.index]
     self.b_G = self.config['b_G'][self.index]
     
     self.Kmu3 = 0.01;
     self.Kmu4 = 0.01;
     
     self.P_G = np.zeros(self.N_iter+1);
     self.P_Gmax = np.squeeze(self.config['P_Gmax'][self.index])
     self.P_Gmin = np.squeeze(self.config['P_Gmin'][self.index])
     self.grad_f = np.zeros(self.N_iter+1);
     
     self.mu3 = np.zeros(self.N_iter+1);
     self.mu4 = np.zeros(self.N_iter+1);
     
     # Loading stabilized values
     self.mu3[0] = self.config['mu3'][self.index]
     self.mu4[0] = self.config['mu4'][self.index]
     
     self.collection = database.getCollection("gen_agent")
     self.collection.remove({})
     
     #Attack
     self.gradFDeceptionAttack = False
     self.gradFDeception = 0
     self.gradFDeceptionInc = 0
     
     #Out of Market
     self.lastPgRcvdTs = -1
     self.currentGridTs = -1
     
     self.commClient = None
Esempio n. 7
0
    def setConfiguration(self, msg, **kwargs):
        NonBlockingDispatchAgent.setConfiguration(self, msg, **kwargs)

        log.info("Loading Config File: %s", self.configFileName)
        self.config = scipy.io.loadmat(self.configFileName, mat_dtype=True)

        log.info("Hostname: %s", self.hostname)
        from magi.testbed import testbed
        self.hostname = testbed.nodename
        log.info("Hostname: %s", self.hostname)

        self.index = int(self.hostname.split("-")[-1])

        self.N_gen = np.squeeze(self.config['N_gen'])

        self.c_G = self.config['c_G'][self.index, self.index]
        self.b_G = self.config['b_G'][self.index]

        self.Kmu3 = 0.01
        self.Kmu4 = 0.01

        self.P_G = np.zeros(self.N_iter + 1)
        self.P_Gmax = np.squeeze(self.config['P_Gmax'][self.index])
        self.P_Gmin = np.squeeze(self.config['P_Gmin'][self.index])
        self.grad_f = np.zeros(self.N_iter + 1)

        self.mu3 = np.zeros(self.N_iter + 1)
        self.mu4 = np.zeros(self.N_iter + 1)

        # Loading stabilized values
        self.mu3[0] = self.config['mu3'][self.index]
        self.mu4[0] = self.config['mu4'][self.index]

        self.collection = database.getCollection("gen_agent")
        self.collection.remove({})

        #Attack
        self.gradFDeceptionAttack = False
        self.gradFDeception = 0
        self.gradFDeceptionInc = 0

        #Out of Market
        self.lastPgRcvdTs = -1
        self.currentGridTs = -1

        self.commClient = None
Esempio n. 8
0
 def stop(self, msg):
     if self.dbLogHandler:
         log.info("Removing database log handler")
         rootLogger = logging.getLogger()
         rootLogger.removeHandler(self.dbLogHandler)
         
     log.info("Terminating database processes")
     for p in self.dbProcesses:
         try:
             log.info("Terminating process %d", p.pid)
             p.terminate()
         except:
             log.exception("Could not terminate process %d", p.pid)
     
     log.info("Waiting for database processes to terminate")
     for p in self.dbProcesses:
         p.wait()
         log.info("Process %d terminated", p.pid)
     log.info("All database processes terminated")
     
     NonBlockingDispatchAgent.stop(self, msg)
Esempio n. 9
0
    def stop(self, msg):
        if self.dbLogHandler:
            log.info("Removing database log handler")
            rootLogger = logging.getLogger()
            rootLogger.removeHandler(self.dbLogHandler)

        log.info("Terminating database processes")
        for p in self.dbProcesses:
            try:
                log.info("Terminating process %d", p.pid)
                p.terminate()
            except:
                log.exception("Could not terminate process %d", p.pid)

        log.info("Waiting for database processes to terminate")
        for p in self.dbProcesses:
            p.wait()
            log.info("Process %d terminated", p.pid)
        log.info("All database processes terminated")

        NonBlockingDispatchAgent.stop(self, msg)
Esempio n. 10
0
 def setConfiguration(self, msg, **kwargs):
     NonBlockingDispatchAgent.setConfiguration(self, msg, **kwargs)
     self.config = scipy.io.loadmat(self.configFileName, mat_dtype=True)
     
     log.info("Hostname: %s", self.hostname)
     from magi.testbed import testbed
     self.hostname = testbed.nodename
     log.info("Hostname: %s", self.hostname)
     
     self.index = int(self.hostname.split("-")[-1])
     
     self.N_dem = np.squeeze(self.config['N_dem'])
     self.K_E = 0 # energy payback (turned off for now)
     
     self.c_D = self.config['c_D'][self.index, self.index]
     self.b_D = self.config['b_D'][self.index]
     
     self.Kmu5 = 0.05;
     self.Kmu6 = 0.05;
     
     self.P_D = np.zeros(self.N_iter+1);
     self.P_Dmax = np.squeeze(self.config['P_Dmax'][self.index])
     self.P_Dmin = np.squeeze(self.config['P_Dmin'][self.index])
     self.E_D = np.zeros(self.N_iter+1);
     self.grad_f = np.zeros(self.N_iter+1);
     
     self.mu5 = np.zeros(self.N_iter+1);
     self.mu6 = np.zeros(self.N_iter+1);
     
     # Loading stabilized values
     self.E_D[0] = self.config['E_D'][self.index]
     self.mu5[0] = self.config['mu5'][self.index]
     self.mu6[0] = self.config['mu6'][self.index]
     
     #Out of Market
     self.lastPdrRcvdTs = -1
     self.currentGridTs = -1
     
     self.commClient = None
Esempio n. 11
0
    def setConfiguration(self, msg, **kwargs):
        NonBlockingDispatchAgent.setConfiguration(self, msg, **kwargs)
        self.config = scipy.io.loadmat(self.configFileName, mat_dtype=True)

        log.info("Hostname: %s", self.hostname)
        from magi.testbed import testbed
        self.hostname = testbed.nodename
        log.info("Hostname: %s", self.hostname)

        self.index = int(self.hostname.split("-")[-1])

        self.N_dem = np.squeeze(self.config['N_dem'])
        self.K_E = 0  # energy payback (turned off for now)

        self.c_D = self.config['c_D'][self.index, self.index]
        self.b_D = self.config['b_D'][self.index]

        self.Kmu5 = 0.05
        self.Kmu6 = 0.05

        self.P_D = np.zeros(self.N_iter + 1)
        self.P_Dmax = np.squeeze(self.config['P_Dmax'][self.index])
        self.P_Dmin = np.squeeze(self.config['P_Dmin'][self.index])
        self.E_D = np.zeros(self.N_iter + 1)
        self.grad_f = np.zeros(self.N_iter + 1)

        self.mu5 = np.zeros(self.N_iter + 1)
        self.mu6 = np.zeros(self.N_iter + 1)

        # Loading stabilized values
        self.E_D[0] = self.config['E_D'][self.index]
        self.mu5[0] = self.config['mu5'][self.index]
        self.mu6[0] = self.config['mu6'][self.index]

        #Out of Market
        self.lastPdrRcvdTs = -1
        self.currentGridTs = -1

        self.commClient = None
Esempio n. 12
0
 def stop(self, msg):
     NonBlockingDispatchAgent.stop(self, msg)
     if self.commClient:
         self.commClient.stop()
Esempio n. 13
0
 def __init__(self):
     NonBlockingDispatchAgent.__init__(self)
     self.configFileName = 'AGCDR_agent.mat'
     self.N_iter = 100
     self.active = True
Esempio n. 14
0
 def setConfiguration(self, msg, **kwargs):
     NonBlockingDispatchAgent.setConfiguration(self, msg, **kwargs)
     
     log.info("Loading Config File: %s", self.configFileName)
     config = scipy.io.loadmat(self.configFileName, mat_dtype=True)
     self.loadProfileConfig = scipy.io.loadmat(self.loadProfileConfigFileName, mat_dtype=True)
     
     self.N_bus = int(config['N_bus']) # number of buses in the system
     self.N_ang = int(config['N_ang']) # number of voltage angles (excluding slack bus)
     self.N_gen = int(config['N_gen']) # number of (conventional) generators
     self.N_dem = int(config['N_dem']) # number of demand response units (specified by the user and randomly distributed in the grid)
     self.N_line = int(config['N_line']) # number of transmission lines
     self.N_xi = int(config['N_xi']) # number of entries in the xi vector
     
     self.P_L_base = np.squeeze(config['P_L_base'])
     
     self.P_L = np.zeros((len(self.P_L_base), 1))
     self.P_L_hat = np.zeros((len(self.P_L_base), 1))
     
     TotalLoadProfile = self.loadProfileConfig['TotalLoadProfile']
     
     T_start = 0 # Start time in hours (i.e. 8 => 8am)
     duration = 24 # Duration in hours
     
     # Construct P_L and P_L_hat
     index = T_start*3600/2
     scaling_signal = TotalLoadProfile[0,index:index+duration*3600/2]
     initial_magnitude = scaling_signal[0]
     scaling_signal = 1/initial_magnitude*scaling_signal
     #N_iter = len(scaling_signal)
     
     self.P_L = addColumn(self.P_L, self.N_iter-1)
     self.P_L_hat = addColumn(self.P_L_hat, self.N_iter-1)
     
     for k in range(self.N_iter):
         # DETERMINE LOADS FOR THIS TIME INTERVAL
         self.P_L[:,k] = scaling_signal[k]*self.P_L_base
         self.P_L_hat[:,k] = self.P_L_base
     
     self.T_lineR = config['T_lineR']
     self.Nh = config['Nh']
     self.Hbarinv = config['Hbarinv']
     
     self.A1 = config['A1']
     self.A2 = config['A2']
     self.A_G = config['A_G']
     
     self.Pmin = np.squeeze(config['Pmin'])
     self.Pmax = np.squeeze(config['Pmax'])
     self.beta = np.squeeze(config['beta'])
     self.gamma = np.squeeze(config['gamma'])
     
     # gains
     self.alpha = 0.9
     self.Kmu1 = 0.01*np.eye(self.N_line);
     self.Kmu2 = 0.01*np.eye(self.N_line);
     
     self.grad_f = np.zeros((self.N_xi, self.N_iter));
     self.grad_L = np.zeros((self.N_xi, self.N_iter));
     self.lambda_hat = np.zeros((self.N_bus, self.N_iter));
     self.lambda_ = np.zeros((self.N_bus, self.N_iter));
     
     self.grad_f_current = np.zeros((self.N_xi))
     
     self.xi = np.zeros((self.N_xi, self.N_iter+1));
     self.theta = np.zeros((self.N_ang, self.N_iter+1));
     self.P_G = np.zeros((self.N_gen, self.N_iter+1));
     self.P_D = np.zeros((self.N_dem, self.N_iter+1));
     self.inactivePgLoad = np.zeros((self.N_gen,));
     
     self.mu1 = np.zeros((self.N_line, self.N_iter+1));
     self.mu2 = np.zeros((self.N_line, self.N_iter+1));
     
     self.rho = np.zeros((self.N_iter+1, 1));
     
     # Loading stabilized values
     self.xi[:, 0] = config['xi'][:, -1]
     self.mu1[:, 0] = config['mu1'][:, -1]
     self.mu2[:, 0] = config['mu2'][:, -1]
     self.rho[0] = config['rho']
     
     # Generator Power Price
     self.gppu = np.zeros((self.N_gen, self.N_iter+1));
     # Generator Total Power Cost
     self.gtpc = np.zeros((self.N_gen, self.N_iter+1));
     
     # Splitting components of x
     self.theta[:, 0] = self.xi[0:self.N_ang, 0]
     self.P_G[:, 0] = self.xi[(self.N_ang):(self.N_ang+self.N_gen), 0]
     self.P_D[:, 0] = self.xi[(self.N_ang+self.N_gen):(self.N_ang+self.N_gen+self.N_dem), 0]
     
     self.collection = database.getCollection("iso_agent")
     self.collection.remove({})
     
     # generator status (active in the market or not)
     self.statusGens = np.ones((self.N_gen), dtype=bool)
     
     # frequency out soft limit or not, managed by grid dynamics agent
     self.freqErrorStatus = False
     
     # how many time-steps ago did the iso last hear from the generators
     self.lastHeardFromGenMap = np.zeros((self.N_gen))
     
     self.commServer = None
Esempio n. 15
0
 def __init__(self):
     NonBlockingDispatchAgent.__init__(self)
     self.configFileName = 'AGCDR_agent.mat'
     self.N_iter = 100
     self.active = True
Esempio n. 16
0
 def stop(self, msg):
     NonBlockingDispatchAgent.stop(self, msg)
     self.commServer.stop()
Esempio n. 17
0
    def setConfiguration(self, msg, **kwargs):
        NonBlockingDispatchAgent.setConfiguration(self, msg, **kwargs)

        config = scipy.io.loadmat(self.configFileName, mat_dtype=True)
        loadProfileConfig = scipy.io.loadmat(self.loadProfileConfigFileName, mat_dtype=True)
        
        self.N_bus = int(config['N_bus']) # number of buses in the system
        self.N_ang = int(config['N_ang']) # number of voltage angles (excluding slack bus)
        self.N_gen = int(config['N_gen']) # number of (conventional) generators
        self.N_dem = int(config['N_dem']) # number of demand response units (specified by the user and randomly distributed in the grid)
        self.N_xi = int(config['N_xi']) # number of entries in the xi vector
        self.N_y = int(config['N_y']) # number of entries in the y vector
        
        self.w_nom = int(config['w_nom']) # Nominal frequency
        self.w_state = config['w_state'].astype(int)-1
        self.delta_state = config['delta_state'].astype(int)-1
        self.Pc_state = config['Pc_state'].astype(int)-1
        self.Pm_state = config['Pm_state'].astype(int)-1
        
        self.Kf = 0.1;
        
        self.A3 = config['A3']
        self.A4 = config['A4']
        self.Phi = config['Phi']
        
        self.T_line = config['T_line']
        
        self.Gamma_P_L = config['Gamma_P_L']
        self.Gamma_P_G = config['Gamma_P_G']
        self.Gamma_P_D = config['Gamma_P_D']
        
        self.P_L_base = np.squeeze(config['P_L_base'])
        
        TotalLoadProfile = loadProfileConfig['TotalLoadProfile']
        
        T_start = 0 # Start time in hours (i.e. 8 => 8am)
        duration = 24 # Duration in hours
        
        # Construct P_L and P_L_hat
        index = T_start*3600/2
        scaling_signal = TotalLoadProfile[0,index:index+duration*3600/2]
        initial_magnitude = scaling_signal[0]
        scaling_signal = 1/initial_magnitude*scaling_signal
        #N_iter = len(scaling_signal)
        
        self.P_L = np.zeros((len(self.P_L_base), self.N_iter))
        self.P_L_hat = np.zeros((len(self.P_L_base), self.N_iter))
        
        for k in range(self.N_iter):
            # DETERMINE LOADS FOR THIS TIME INTERVAL
            self.P_L[:,k] = scaling_signal[k]*self.P_L_base;
            self.P_L_hat[:,k] = self.P_L_base;
            
        self.xi_current = np.zeros((self.N_xi));
        self.theta = np.zeros((self.N_ang));
        self.P_G = np.zeros((self.N_gen));
        self.P_D = np.zeros((self.N_dem));
        
        self.y = np.zeros((self.N_y, self.N_iter+1));
        self.E_D = np.zeros((self.N_dem, self.N_iter+1));
        self.rho = np.zeros((self.N_iter+1, 1));
        
        # Loading stabilized values
        self.xi_current[:] = config['xi'][:, -1]
        
        # Splitting components of x
        self.theta[:] = self.xi_current[0:self.N_ang]
        self.P_G[:] = self.xi_current[(self.N_ang):(self.N_ang+self.N_gen)]
        self.P_D[:] = self.xi_current[(self.N_ang+self.N_gen):(self.N_ang+self.N_gen+self.N_dem)]
        
        self.y[:, 0] = config['y'][:, -1]
        self.rho[0] = config['rho']
        self.E_D[:, 0] = config['E_D'][:, -1]
        
        self.collection = database.getCollection("grid_agent")
        self.collection.remove({})
        
        self.collection.insert({'k' : 0, 'y' : self.y[:, 0].tolist()})
        self.collection.insert({'k' : 0, 'rho' : self.rho[0].tolist()})
        self.collection.insert({'k' : 0, 'edr' : self.E_D[:, 0].tolist()})
        
        self.freqErrorStatus = False
Esempio n. 18
0
 def __init__(self):
     NonBlockingDispatchAgent.__init__(self)
     self.configFileName = 'AGCDR_agent.mat'
     self.loadProfileConfigFileName = 'AGCDR_profiles.mat'
     self.N_iter = 100
Esempio n. 19
0
    def setConfiguration(self, msg, **kwargs):
        NonBlockingDispatchAgent.setConfiguration(self, msg, **kwargs)

        log.info("Loading Config File: %s", self.configFileName)
        config = scipy.io.loadmat(self.configFileName, mat_dtype=True)
        self.loadProfileConfig = scipy.io.loadmat(
            self.loadProfileConfigFileName, mat_dtype=True)

        self.N_bus = int(config['N_bus'])  # number of buses in the system
        self.N_ang = int(
            config['N_ang'])  # number of voltage angles (excluding slack bus)
        self.N_gen = int(
            config['N_gen'])  # number of (conventional) generators
        self.N_dem = int(
            config['N_dem']
        )  # number of demand response units (specified by the user and randomly distributed in the grid)
        self.N_line = int(config['N_line'])  # number of transmission lines
        self.N_xi = int(config['N_xi'])  # number of entries in the xi vector

        self.P_L_base = np.squeeze(config['P_L_base'])

        self.P_L = np.zeros((len(self.P_L_base), 1))
        self.P_L_hat = np.zeros((len(self.P_L_base), 1))

        TotalLoadProfile = self.loadProfileConfig['TotalLoadProfile']

        T_start = 0  # Start time in hours (i.e. 8 => 8am)
        duration = 24  # Duration in hours

        # Construct P_L and P_L_hat
        index = T_start * 3600 / 2
        scaling_signal = TotalLoadProfile[0, index:index + duration * 3600 / 2]
        initial_magnitude = scaling_signal[0]
        scaling_signal = 1 / initial_magnitude * scaling_signal
        #N_iter = len(scaling_signal)

        self.P_L = addColumn(self.P_L, self.N_iter - 1)
        self.P_L_hat = addColumn(self.P_L_hat, self.N_iter - 1)

        for k in range(self.N_iter):
            # DETERMINE LOADS FOR THIS TIME INTERVAL
            self.P_L[:, k] = scaling_signal[k] * self.P_L_base
            self.P_L_hat[:, k] = self.P_L_base

        self.T_lineR = config['T_lineR']
        self.Nh = config['Nh']
        self.Hbarinv = config['Hbarinv']

        self.A1 = config['A1']
        self.A2 = config['A2']
        self.A_G = config['A_G']

        self.Pmin = np.squeeze(config['Pmin'])
        self.Pmax = np.squeeze(config['Pmax'])
        self.beta = np.squeeze(config['beta'])
        self.gamma = np.squeeze(config['gamma'])

        # gains
        self.alpha = 0.9
        self.Kmu1 = 0.01 * np.eye(self.N_line)
        self.Kmu2 = 0.01 * np.eye(self.N_line)

        self.grad_f = np.zeros((self.N_xi, self.N_iter))
        self.grad_L = np.zeros((self.N_xi, self.N_iter))
        self.lambda_hat = np.zeros((self.N_bus, self.N_iter))
        self.lambda_ = np.zeros((self.N_bus, self.N_iter))

        self.grad_f_current = np.zeros((self.N_xi))

        self.xi = np.zeros((self.N_xi, self.N_iter + 1))
        self.theta = np.zeros((self.N_ang, self.N_iter + 1))
        self.P_G = np.zeros((self.N_gen, self.N_iter + 1))
        self.P_D = np.zeros((self.N_dem, self.N_iter + 1))
        self.inactivePgLoad = np.zeros((self.N_gen, ))

        self.mu1 = np.zeros((self.N_line, self.N_iter + 1))
        self.mu2 = np.zeros((self.N_line, self.N_iter + 1))

        self.rho = np.zeros((self.N_iter + 1, 1))

        # Loading stabilized values
        self.xi[:, 0] = config['xi'][:, -1]
        self.mu1[:, 0] = config['mu1'][:, -1]
        self.mu2[:, 0] = config['mu2'][:, -1]
        self.rho[0] = config['rho']

        # Generator Power Price
        self.gppu = np.zeros((self.N_gen, self.N_iter + 1))
        # Generator Total Power Cost
        self.gtpc = np.zeros((self.N_gen, self.N_iter + 1))

        # Splitting components of x
        self.theta[:, 0] = self.xi[0:self.N_ang, 0]
        self.P_G[:, 0] = self.xi[(self.N_ang):(self.N_ang + self.N_gen), 0]
        self.P_D[:, 0] = self.xi[(self.N_ang +
                                  self.N_gen):(self.N_ang + self.N_gen +
                                               self.N_dem), 0]

        self.collection = database.getCollection("iso_agent")
        self.collection.remove({})

        # generator status (active in the market or not)
        self.statusGens = np.ones((self.N_gen), dtype=bool)

        # frequency out soft limit or not, managed by grid dynamics agent
        self.freqErrorStatus = False

        # how many time-steps ago did the iso last hear from the generators
        self.lastHeardFromGenMap = np.zeros((self.N_gen))

        self.commServer = None
Esempio n. 20
0
 def stop(self, msg):
     NonBlockingDispatchAgent.stop(self, msg)
     if self.commClient:
         self.commClient.stop()
Esempio n. 21
0
 def stop(self, msg):
     NonBlockingDispatchAgent.stop(self, msg)
     self.commServer.stop()