Esempio n. 1
0
def nash2d():
    r = [numpy.zeros((nm, nm)), numpy.zeros((nm, nm))]# * 2#ng
    #r = numpy.zeros((nm, nm, 2))
    #r = numpy.zeros([ng] + ([nm] * ng))

    mkt = SmartMarket(case, priceCap=999.0, decommit=False,
                      auctionType=DISCRIMINATIVE
                      )

    t1 = ProfitTask(MarketEnvironment([gens[0]], mkt, markups=mup))
    t2 = ProfitTask(MarketEnvironment([gens[1]], mkt, markups=mup))
    t3 = ProfitTask(MarketEnvironment([gens[2]], mkt, markups=mup))

    for m1 in range(nm):
        for m2 in range(nm):
            t1.env.performAction(m1)
            t2.env.performAction(m2)
            t3.env.performAction(0)

            mkt.run()

            r[0][m2, m1] = t1.getReward()
            r[1][m2, m1] = t2.getReward()

            mkt.reset()

    print r[0]
    print r[1]
    #for gi in range(2):
    #    mmwrite("/tmp/nash_g%s.mtx" % gi, r[gi, :, :])

    return r
Esempio n. 2
0
    def setUp(self):
        """ The test runner will execute this method prior to each test.
        """
        self.case = Case.load(DATA_FILE)

        generators = self.case.generators

        self.offers = [
            Offer(generators[0], 12.0, 20.0),
            Offer(generators[0], 24.0, 50.0),
            Offer(generators[0], 24.0, 60.0),

            Offer(generators[1], 12.0, 20.0),
            Offer(generators[1], 24.0, 40.0),
            Offer(generators[1], 24.0, 70.0),

            Offer(generators[2], 12.0, 20.0),
            Offer(generators[2], 24.0, 42.0),
            Offer(generators[2], 24.0, 80.0),

            Offer(generators[3], 12.0, 20.0),
            Offer(generators[3], 24.0, 44.0),
            Offer(generators[3], 24.0, 90.0),

            Offer(generators[4], 12.0, 20.0),
            Offer(generators[4], 24.0, 46.0),
            Offer(generators[4], 24.0, 75.0),

            Offer(generators[5], 12.0, 20.0),
            Offer(generators[5], 24.0, 48.0),
            Offer(generators[5], 24.0, 60.0)
        ]

        self.bids = [
            Bid(generators[6], 10.0, 100.0),
            Bid(generators[6], 10.0, 70.0),
            Bid(generators[6], 10.0, 60.0),

            Bid(generators[7], 10.0, 100.0),
            Bid(generators[7], 10.0, 50.0),
            Bid(generators[7], 10.0, 20.0),

            Bid(generators[8], 10.0, 100.0),
            Bid(generators[8], 10.0, 60.0),
            Bid(generators[8], 10.0, 50.0)
        ]

        self.mkt = SmartMarket(self.case, self.offers, self.bids,
            locationalAdjustment='dc', auctionType=FIRST_PRICE, priceCap=100.0)
Esempio n. 3
0
class DCMarketTestCase(unittest.TestCase):
    """ Defines a test case for the Pyreto market using data from t_runmarket.
    """

    def setUp(self):
        """ The test runner will execute this method prior to each test.
        """
        self.case = Case.load(DATA_FILE)

        generators = self.case.generators

        self.offers = [
            Offer(generators[0], 12.0, 20.0),
            Offer(generators[0], 24.0, 50.0),
            Offer(generators[0], 24.0, 60.0),
            Offer(generators[1], 12.0, 20.0),
            Offer(generators[1], 24.0, 40.0),
            Offer(generators[1], 24.0, 70.0),
            Offer(generators[2], 12.0, 20.0),
            Offer(generators[2], 24.0, 42.0),
            Offer(generators[2], 24.0, 80.0),
            Offer(generators[3], 12.0, 20.0),
            Offer(generators[3], 24.0, 44.0),
            Offer(generators[3], 24.0, 90.0),
            Offer(generators[4], 12.0, 20.0),
            Offer(generators[4], 24.0, 46.0),
            Offer(generators[4], 24.0, 75.0),
            Offer(generators[5], 12.0, 20.0),
            Offer(generators[5], 24.0, 48.0),
            Offer(generators[5], 24.0, 60.0),
        ]

        self.bids = [
            Bid(generators[6], 10.0, 100.0),
            Bid(generators[6], 10.0, 70.0),
            Bid(generators[6], 10.0, 60.0),
            Bid(generators[7], 10.0, 100.0),
            Bid(generators[7], 10.0, 50.0),
            Bid(generators[7], 10.0, 20.0),
            Bid(generators[8], 10.0, 100.0),
            Bid(generators[8], 10.0, 60.0),
            Bid(generators[8], 10.0, 50.0),
        ]

        self.mkt = SmartMarket(
            self.case, self.offers, self.bids, locationalAdjustment="dc", auctionType=FIRST_PRICE, priceCap=100.0
        )

    def testDcOpf(self):
        """ Test solving the auction case using DC OPF.
        """
        solver = OPF(self.case, True, opt={"verbose": False})
        solution = solver.solve()
        self.assertTrue(solution["converged"])
        self.assertAlmostEqual(solution["f"], -517.81, 2)

    def testReset(self):
        """ Test resetting the market.
        """
        self.assertEqual(len(self.mkt.offers), 18)
        self.assertEqual(len(self.mkt.bids), 9)
        self.mkt.reset()
        self.assertEqual(len(self.mkt.offers), 0)
        self.assertEqual(len(self.mkt.bids), 0)

    def testHaveQ(self):
        """ Test reactive offers/bids.
        """
        self.assertFalse(self.mkt._isReactiveMarket())

    def testWithhold(self):
        """ Test witholding of invalid and limited offers/bids.
        """
        invalidOffer = Offer(self.case.generators[0], -10.0, 20.0)
        self.mkt.offers.append(invalidOffer)
        self.mkt.priceCap = 80.0

        self.mkt._withhold_offbids()

        self.assertFalse(self.offers[0].withheld)
        self.assertFalse(self.offers[8].withheld)
        self.assertTrue(self.offers[11].withheld)
        self.assertTrue(invalidOffer.withheld)

    def testOffbidToCase(self):
        """ Test conversion of offers/bids to pwl functions and limit updates.
        """
        self.mkt._withholdOffbids()
        self.mkt._offbidToCase()

        places = 2
        generators = self.case.generators

        for g in generators:
            self.assertTrue(g.online)

        self.assertAlmostEqual(generators[0].p_min, 35.0, places)
        self.assertAlmostEqual(generators[0].p_max, 60.0, places)
        self.assertAlmostEqual(generators[1].p_min, 12.0, places)
        self.assertAlmostEqual(generators[1].p_max, 60.0, places)
        self.assertAlmostEqual(generators[6].p_min, -30.0, places)
        self.assertAlmostEqual(generators[6].p_max, 0.0, places)

        self.assertAlmostEqual(generators[0].p_cost[2][0], 36.0, places)
        self.assertAlmostEqual(generators[0].p_cost[2][1], 1440.0, places)
        self.assertAlmostEqual(generators[0].p_cost[3][0], 60.0, places)
        self.assertAlmostEqual(generators[0].p_cost[3][1], 2880.0, places)

        self.assertAlmostEqual(generators[2].p_cost[2][0], 36.0, places)
        self.assertAlmostEqual(generators[2].p_cost[2][1], 1248.0, places)
        self.assertAlmostEqual(generators[2].p_cost[3][0], 60.0, places)
        self.assertAlmostEqual(generators[2].p_cost[3][1], 3168.0, places)

        self.assertAlmostEqual(generators[6].p_cost[0][0], -30.0, places)
        self.assertAlmostEqual(generators[6].p_cost[0][1], -2300.0, places)
        self.assertAlmostEqual(generators[6].p_cost[1][0], -20.0, places)
        self.assertAlmostEqual(generators[6].p_cost[1][1], -1700.0, places)

        self.assertAlmostEqual(generators[2].q_min, -15.0, places)
        self.assertAlmostEqual(generators[2].q_max, 60.0, places)
        self.assertAlmostEqual(generators[5].q_min, -15.0, places)
        self.assertAlmostEqual(generators[5].q_max, 60.0, places)
        self.assertAlmostEqual(generators[7].q_min, -12.0, places)
        self.assertAlmostEqual(generators[7].q_max, 0.0, places)

    #        self.assertAlmostEqual(generators[2].q_cost[0][0], -15.0, places)
    #        self.assertAlmostEqual(generators[2].q_cost[0][1], 0.0, places)
    #        self.assertAlmostEqual(generators[2].q_cost[2][0], 60.0, places)
    #        self.assertAlmostEqual(generators[2].q_cost[2][1], 0.0, places)
    #
    #        self.assertAlmostEqual(generators[5].q_cost[0][0], -15.0, places)
    #        self.assertAlmostEqual(generators[5].q_cost[0][1], 0.0, places)
    #        self.assertAlmostEqual(generators[5].q_cost[2][0], 60.0, places)
    #        self.assertAlmostEqual(generators[5].q_cost[2][1], 180.0, places)
    #
    #        self.assertAlmostEqual(generators[7].q_cost[0][0], -12.0, places)
    #        self.assertAlmostEqual(generators[7].q_cost[0][1], -240.0, places)
    #        self.assertAlmostEqual(generators[7].q_cost[2][0], 0.0, places)
    #        self.assertAlmostEqual(generators[7].q_cost[2][1], 0.0, places)

    def testRunOPF(self):
        """ Test generator dispatch points.
        """
        mkt = self.mkt
        mkt._withholdOffbids()
        mkt._offbidToCase()
        success = mkt._runOPF()

        self.assertTrue(success)
        self.assertAlmostEqual(mkt._solution["f"], 2802.19, 2)

    def testNodalMarginalPrices(self):
        """ Test nodal marginal prices from OPF.
        """
        self.mkt._withholdOffbids()
        self.mkt._offbidToCase()
        _ = self.mkt._runOPF()
        gteeOfferPrice, gteeBidPrice = self.mkt._nodalPrices(haveQ=True)

        self.assertTrue(gteeOfferPrice)
        self.assertTrue(gteeBidPrice)

        # Nodal marginal prices.
        for offbid in self.offers + self.bids:
            self.assertAlmostEqual(offbid.lmbda, 50.0, 4)

        places = 0  # TODO: Repeat using PDIPM.
        # Total dispatched quantity for associated generator.
        self.assertAlmostEqual(self.offers[0].totalQuantity, 35.6103, places)
        self.assertAlmostEqual(self.offers[3].totalQuantity, 36.0000, places)
        self.assertAlmostEqual(self.offers[6].totalQuantity, 36.0000, places)

        self.assertAlmostEqual(self.bids[0].totalQuantity, 30.0000, places)
        self.assertAlmostEqual(self.bids[3].totalQuantity, 11.1779, places)
        self.assertAlmostEqual(self.bids[6].totalQuantity, 22.7885, places)

    def testActivePowerAuction(self):
        """ Test auction for clearing offer/bid quantities and prices.
        """
        self.mkt._withholdOffbids()
        self.mkt._offbidToCase()
        _ = self.mkt._runOPF()
        gteeOfferPrice, gteeBidPrice = self.mkt._nodalPrices(haveQ=True)
        self.mkt._runAuction(gteeOfferPrice, gteeBidPrice, haveQ=True)

        places = 4

        for offer in self.offers:
            self.assertAlmostEqual(offer.clearedPrice, 50.0, places)
        for bid in self.bids:
            self.assertAlmostEqual(bid.clearedPrice, 50.0, places)

        offers = self.offers
        self.assertAlmostEqual(offers[0].clearedQuantity, 12.0, places)
        self.assertAlmostEqual(offers[1].clearedQuantity, 23.6103, places=0)
        self.assertAlmostEqual(offers[2].clearedQuantity, 0.0, places)

        self.assertAlmostEqual(offers[3].clearedQuantity, 12.0, places)
        self.assertAlmostEqual(offers[4].clearedQuantity, 24.0, places)
        self.assertAlmostEqual(offers[5].clearedQuantity, 0.00, places)

        self.assertAlmostEqual(offers[6].clearedQuantity, 12.0, places)
        self.assertAlmostEqual(offers[7].clearedQuantity, 24.0, places)
        self.assertAlmostEqual(offers[8].clearedQuantity, 0.00, places)

        self.assertAlmostEqual(offers[9].clearedQuantity, 12.0, places)
        self.assertAlmostEqual(offers[10].clearedQuantity, 24.0, places)
        self.assertAlmostEqual(offers[11].clearedQuantity, 0.00, places)

        self.assertAlmostEqual(offers[12].clearedQuantity, 12.0, places)
        self.assertAlmostEqual(offers[13].clearedQuantity, 24.0, places)
        self.assertAlmostEqual(offers[14].clearedQuantity, 0.00, places)

        self.assertAlmostEqual(offers[15].clearedQuantity, 12.0, places)
        self.assertAlmostEqual(offers[16].clearedQuantity, 24.0, places)
        self.assertAlmostEqual(offers[17].clearedQuantity, 0.00, places)

        bids = self.bids
        self.assertAlmostEqual(bids[0].clearedQuantity, 10.0, places)
        self.assertAlmostEqual(bids[1].clearedQuantity, 10.0, places)
        self.assertAlmostEqual(bids[2].clearedQuantity, 10.0, places)

        self.assertAlmostEqual(bids[3].clearedQuantity, 10.0, places)
        self.assertAlmostEqual(bids[4].clearedQuantity, 1.1779, places)
        self.assertAlmostEqual(bids[5].clearedQuantity, 0.0, places)

        self.assertAlmostEqual(bids[6].clearedQuantity, 10.0, places)
        self.assertAlmostEqual(bids[7].clearedQuantity, 10.0, places)
        self.assertAlmostEqual(bids[8].clearedQuantity, 2.7885, places)

    def test_constrained_market(self):
        """ Test cleared prices & quantities in a constrained system.
        """
        mkt = self.mkt

        # Introduce a constraint on the 16th branch by lowering the rating.
        constrained = self.case.branches[15]
        constrained.rate_a = 30.0

        offers, bids = mkt.run()

        places = 4

        self.assertAlmostEqual(mkt._solution["f"], 2949.10, 2)

        # Cleared offer prices.
        for i in range(0, 3):
            self.assertAlmostEqual(offers[i].cleared_price, 50.0, places)
        for i in range(3, 6):
            self.assertAlmostEqual(offers[i].cleared_price, 41.0442, places)
        for i in range(6, 9):
            self.assertAlmostEqual(offers[i].cleared_price, 52.3954, places)
        for i in range(9, 12):
            self.assertAlmostEqual(offers[i].cleared_price, 50.0, places)
        for i in range(12, 15):
            self.assertAlmostEqual(offers[i].cleared_price, 75.0, places)
        for i in range(15, 18):
            self.assertAlmostEqual(offers[i].cleared_price, 48.0, places)

        # Cleared offer quantities.
        self.assertAlmostEqual(offers[0].clearedQuantity, 12.0, places)
        self.assertAlmostEqual(offers[1].clearedQuantity, 22.9995, places)
        self.assertAlmostEqual(offers[2].clearedQuantity, 0.0, places)

        self.assertAlmostEqual(offers[3].clearedQuantity, 12.0, places)
        self.assertAlmostEqual(offers[4].clearedQuantity, 24.0, places)
        self.assertAlmostEqual(offers[5].clearedQuantity, 0.00, places)

        self.assertAlmostEqual(offers[6].clearedQuantity, 12.0, places)
        self.assertAlmostEqual(offers[7].clearedQuantity, 24.0, places)
        self.assertAlmostEqual(offers[8].clearedQuantity, 0.00, places)

        self.assertAlmostEqual(offers[9].clearedQuantity, 12.0, places)
        self.assertAlmostEqual(offers[10].clearedQuantity, 24.0, places)
        self.assertAlmostEqual(offers[11].clearedQuantity, 0.00, places)

        self.assertAlmostEqual(offers[12].clearedQuantity, 12.0, places)
        self.assertAlmostEqual(offers[13].clearedQuantity, 24.0, places)
        self.assertAlmostEqual(offers[14].clearedQuantity, 5.3963, places)

        self.assertAlmostEqual(offers[15].clearedQuantity, 12.0, places)
        self.assertAlmostEqual(offers[16].clearedQuantity, 18.0, places)
        self.assertAlmostEqual(offers[17].clearedQuantity, 0.00, places)

        # Cleared bid prices.
        for i in range(0, 3):
            self.assertAlmostEqual(bids[i].clearedPrice, 41.8831, places)
        for i in range(3, 6):
            self.assertAlmostEqual(bids[i].clearedPrice, 86.4585, places)
        for i in range(6, 9):
            self.assertAlmostEqual(bids[i].clearedPrice, 50.0000, places)

        # Cleared bid quantities.
        self.assertAlmostEqual(bids[0].clearedQuantity, 10.0, places)
        self.assertAlmostEqual(bids[1].clearedQuantity, 10.0, places)
        self.assertAlmostEqual(bids[2].clearedQuantity, 10.0, places)

        self.assertAlmostEqual(bids[3].clearedQuantity, 10.0, places)
        self.assertAlmostEqual(bids[4].clearedQuantity, 0.00, places)
        self.assertAlmostEqual(bids[5].clearedQuantity, 0.00, places)

        self.assertAlmostEqual(bids[6].clearedQuantity, 10.0, places)
        self.assertAlmostEqual(bids[7].clearedQuantity, 10.0, places)
        self.assertAlmostEqual(bids[8].clearedQuantity, 2.7519, places)