def test_update_costs(): print('Running AbstractModel.test_update_costs()') pf = 'pass' # Create a test market test_mkt test_mkt = Market() # Create a sample time interval ti dt = datetime.now() at = dt # NOTE: Function Hours() corrects behavior of Matlab hours(). dur = timedelta(hours=1) mkt = test_mkt mct = dt st = datetime.combine(date.today(), time()) + timedelta(hours=20) ti = TimeInterval(at, dur, mkt, mct, st) # Save the time interval test_mkt.timeIntervals = [ti] # Assign a marginal price in the time interval test_mkt.check_marginal_prices() # Create a Neighbor test object and give it a default maximum power value test_obj = Neighbor() # test_obj.maximumPower = 100 # Create a corresponding NeighborModel test_mdl = NeighborModel() # Make sure that the model and object cross-reference one another test_obj.model = test_mdl test_mdl.object = test_obj test_mdl.scheduledPowers = [ IntervalValue(test_mdl, ti, test_mkt, MeasurementType.ScheduledPower, 100) ] test_mdl.activeVertices = [ IntervalValue(test_mdl, ti, test_mkt, MeasurementType.ActiveVertex, Vertex(0.05, 0, 100)) ] # Run a test with a NeighborModel object print('- running test with a NeighborModel:') try: test_mdl.update_costs(test_mkt) print(' - the method encountered no errors') except: pf = 'fail' raise ' - the method did not run without errors' if len(test_mdl.productionCosts) != 1: pf = 'fail' raise ' - the method did not store a production cost' else: print(' - the method calculated and stored a production cost') if len(test_mdl.dualCosts) != 1: pf = 'fail' raise ' - the method did not store a dual cost' else: print(' - the method stored a dual cost') if test_mdl.totalProductionCost != sum( [x.value for x in test_mdl.productionCosts]): pf = 'fail' raise ' - the method did not store a total production cost' else: print(' - the method stored an total production cost') if test_mdl.totalDualCost != sum([x.value for x in test_mdl.dualCosts]): pf = 'fail' raise ' - the method did not store a total dual cost' else: print(' - the method stored an total dual cost') # Run a test again with a LocalAssetModel object test_obj = LocalAsset() # test_obj.maximumPower = 100 test_mdl = LocalAssetModel() test_obj.model = test_mdl test_mdl.object = test_obj test_mdl.scheduledPowers = [ IntervalValue(test_mdl, ti, test_mkt, MeasurementType.ScheduledPower, 100) ] test_mdl.activeVertices = [ IntervalValue(test_mdl, ti, test_mkt, MeasurementType.ActiveVertex, Vertex(0.05, 0, 100)) ] print('- running test with a LocalAssetModel:') try: test_mdl.update_costs(test_mkt) print(' - the method encountered no errors') except: pf = 'fail' raise ' - the method did not run without errors' if len(test_mdl.productionCosts) != 1: pf = 'fail' raise ' - the method did not store a production cost' else: print(' - the method calculated and stored a production cost') if len(test_mdl.dualCosts) != 1: pf = 'fail' raise ' - the method did not store a dual cost' else: print(' - the method stored a dual cost') if test_mdl.totalProductionCost != sum( [x.value for x in test_mdl.productionCosts]): pf = 'fail' raise ' - the method did not store a total production cost' else: print(' - the method stored an total production cost') if test_mdl.totalDualCost != sum([x.value for x in test_mdl.dualCosts]): pf = 'fail' raise ' - the method did not store a total dual cost' else: print(' - the method stored an total dual cost') # Success print('- the test ran to completion') print('Result: %s', pf)
def test_sum_vertices(): print('Running Market.test_sum_vertices()') pf = 'pass' # Create a test myTransactiveNode object. test_node = myTransactiveNode() # Create a test Market object. test_market = Market() # List the test market with the test_node. test_node.markets = test_market # Create and store a time interval to work with. dt = datetime.now() at = dt dur = timedelta(hours=1) mkt = test_market mct = dt st = dt time_interval = TimeInterval(at, dur, mkt, mct, st) test_market.timeIntervals = [time_interval] # Create test LocalAsset and LocalAssetModel objects test_asset = LocalAsset() test_asset_model = LocalAssetModel() # Add the test_asset to the test node list. test_node.localAssets = [test_asset] # Have the test asset and its model cross reference one another. test_asset.model = test_asset_model test_asset_model.object = test_asset # Create and store an active Vertex or two for the test asset test_vertex = [Vertex(0.2, 0, -110), Vertex(0.2, 0, -90)] interval_values = [ IntervalValue(test_node, time_interval, test_market, MeasurementType.ActiveVertex, test_vertex[0]), IntervalValue(test_node, time_interval, test_market, MeasurementType.ActiveVertex, test_vertex[1]) ] test_asset_model.activeVertices = [interval_values[0], interval_values[1] ] # interval_value(1:2) # Create test Neighbor and NeighborModel objects. test_neighbor = Neighbor() test_neighbor_model = NeighborModel() # Add the test neighbor to the test node list. test_node.neighbors = [test_neighbor] # Have the test neighbor and its model cross reference one another. test_neighbor.model = test_neighbor_model test_neighbor.model.object = test_neighbor # Create and store an active Vertex or two for the test neighbor test_vertex.append(Vertex(0.1, 0, 0)) test_vertex.append(Vertex(0.3, 0, 200)) interval_values.append( IntervalValue(test_node, time_interval, test_market, MeasurementType.ActiveVertex, test_vertex[2])) interval_values.append( IntervalValue(test_node, time_interval, test_market, MeasurementType.ActiveVertex, test_vertex[3])) test_neighbor_model.activeVertices = [ interval_values[2], interval_values[3] ] ## Case 1 print('- Case 1: Basic case with interleaved vertices') # Run the test. try: vertices = test_market.sum_vertices(test_node, time_interval) print(' - the method ran without errors') except: pf = 'fail' print(' - the method had errors when called and stopped') if len(vertices) != 4: pf = 'fail' print(' - an unexpected number of vertices was returned') else: print(' - the expected number of vertices was returned') powers = [x.power for x in vertices] # if any(~ismember(single(powers), single([-110.0000, -10.0000, 10.0000, 110.0000]))) if len([ x for x in powers if round(x, 4) not in [-110.0000, -10.0000, 10.0000, 110.0000] ]) > 0: pf = 'fail' print(' - the vertex powers were not as expected') else: print(' - the vertex powers were as expected') marginal_prices = [round(x.marginalPrice, 4) for x in vertices] # if any(~ismember(single(marginal_prices), single([0.1000, 0.2000, 0.3000]))) if len([ x for x in marginal_prices if round(x, 4) not in [0.1000, 0.2000, 0.3000] ]) > 0: pf = 'fail' print(' - the vertex powers were not as expected') else: print(' - the vertex marginal prices were as expected') ## CASE 2: NEIGHBOR MODEL TO BE EXCLUDED # This case is needed when a demand or supply curve must be created for a # transactive Neighbor object. The active vertices of the target Neighbor # must be excluded, leaving a residual supply or demand curve against which # the Neighbor may plan. print('- Case 2: Exclude test Neighbor model') # Run the test. try: # [vertices] = test_market.sum_vertices(test_node, time_interval, test_neighbor_model) vertices = test_market.sum_vertices(test_node, time_interval, test_neighbor_model) print(' - the method ran without errors') except: pf = 'fail' print(' - the method encountered errors and stopped') if len(vertices) != 2: pf = 'fail' print(' - an unexpected number of vertices was returned') else: print(' - the expected number of vertices was returned') powers = [round(x.power, 4) for x in vertices] # if any(~ismember(single(powers), single([-110.0000, -90.0000]))) if len([x for x in powers if x not in [-110.0000, -90.0000]]) > 0: pf = 'fail' print(' - the vertex powers were not as expected') else: print(' - the vertex powers were as expected') marginal_prices = [x.marginalPrice for x in vertices] # if any(~ismember(single(marginal_prices), single([0.2000]))) if len([x for x in marginal_prices if round(x, 4) not in [0.2000]]) > 0: pf = 'fail' print(' - the vertex powers were not as expected') else: print(' - the vertex marginal prices were as expected') ## CASE 3: CONSTANT SHOULD NOT CREATE NEW NET VERTEX print('- Case 3: Include a constant vertex. No net vertex should be added') # Change the test asset to NOT have any flexibility. A constant should # not introduce a net vertex at a constant's marginal price. Marginal # price is NOT meaningful for an inelastic device. test_asset_model.activeVertices = [interval_values[0]] # Run the test. try: # [vertices] = test_market.sum_vertices(test_node, time_interval) vertices = test_market.sum_vertices(test_node, time_interval) print(' - the method ran without errors') except: pf = 'fail' print(' - the method encountered errors and stopped') #%[180907DJH: THIS TEST IS CORRECTED. THE NEIGHBOR HAS TWO VERTICES. ADDING #AN ASSET WITH ONE VERTEX (NO FLEXIBILITY) SHOULD NOT CHANGE THE NUMBER OF #ACTIVE VERTICES, SO THE CORRECTED TEST CONFIRMS TWO VERTICES. THE CODE HAS #BEEN CORRECTED ACCORDINGLY.] if len(vertices) != 2: pf = 'fail' print(' - an unexpected number of vertices was returned') else: print(' - the expected number of vertices was returned') powers = [x.power for x in vertices] # if any(~ismember(single(powers), single([-110.0000, 90]))) if len([x for x in powers if round(x, 4) not in [-110.0000, 90]]) > 0: pf = 'fail' print(' - the vertex powers were not as expected') else: print(' - the vertex powers were as expected') marginal_prices = [x.marginalPrice for x in vertices] # if any(~ismember(single(marginal_prices), single([0.1000, 0.3000, Inf]))) if len([ x for x in marginal_prices if round(x, 4) not in [0.1000, 0.3000, float("inf")] ]) > 0: pf = 'fail' print(' - the vertex powers were not as expected') else: print(' - the vertex marginal prices were as expected') # CASE 4: More than two vertices at any marginal price print('- Case 4: More than two vertices at same marginal price') # Move the two active vertices of the test asset to be at the same # marginal price as one of the neighbor active vertices. test_vertex = [Vertex(0.1, 0, -110), Vertex(0.1, 0, -90)] interval_values = [ IntervalValue(test_node, time_interval, test_market, MeasurementType.ActiveVertex, test_vertex[0]), IntervalValue(test_node, time_interval, test_market, MeasurementType.ActiveVertex, test_vertex[1]) ] test_asset_model.activeVertices = [interval_values[0], interval_values[1] ] # interval_value(1:2) # Run the test. try: vertices = test_market.sum_vertices(test_node, time_interval) print(' - the method ran without errors') except: pf = 'fail' print(' - the method encountered errors and stopped') if len(vertices) != 3: pf = 'fail' print(' - an unexpected number of vertices was returned') else: print(' - the expected number of vertices was returned') powers = [x.power for x in vertices] # if any(~ismember(single(powers), single([-110.0000, -90.0000, 110.0000]))) if len([ x for x in powers if round(x, 4) not in [-110.0000, -90.0000, 110.0000] ]) > 0: pf = 'fail' print(' - the vertex powers were not as expected') else: print(' - the vertex powers were as expected') marginal_prices = [x.marginalPrice for x in vertices] # if any(~ismember(single(marginal_prices), single([0.1000, 0.3000]))) if len([x for x in marginal_prices if round(x, 4) not in [0.1000, 0.3000] ]) > 0: pf = 'fail' print(' - the vertex powers were not as expected') else: print(' - the vertex marginal prices were as expected') # Success print('- the test ran to completion') print('Result: #s\n\n', pf)