Java tutorial
/* * TacTex - a power trading agent that competed in the Power Trading Agent Competition (Power TAC) www.powertac.org * Copyright (c) 2013-2016 Daniel Urieli and Peter Stone {urieli,pstone}@cs.utexas.edu * * * This file is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This file is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * This file incorporates work covered by the following copyright and * permission notice: * * Copyright 2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package edu.utexas.cs.tactex.servercustomers.factoredcustomer; import java.util.List; import java.util.ArrayList; import org.w3c.dom.*; import edu.utexas.cs.tactex.servercustomers.factoredcustomer.CustomerFactory.CustomerCreator; import edu.utexas.cs.tactex.servercustomers.factoredcustomer.interfaces.*; import org.apache.commons.math3.linear.RealVector; import org.apache.log4j.Logger; import org.powertac.common.Timeslot; import org.powertac.common.enumerations.PowerType; import org.powertac.common.repo.CustomerRepo; import org.powertac.common.repo.TimeslotRepo; import org.powertac.common.state.Domain; /** * Key class that encapsulates the behavior of one customer. Much of the functionality * is delegated to contained utility optimizers and capacity bundles, however. * * @author Prashant Reddy */ @Domain class DefaultFactoredCustomer implements FactoredCustomer { protected Logger log = Logger.getLogger(DefaultFactoredCustomer.class.getName()); protected CustomerStructure customerStructure; protected UtilityOptimizer utilityOptimizer; protected final List<CapacityBundle> capacityBundles = new ArrayList<CapacityBundle>(); protected FactoredCustomerService service; DefaultFactoredCustomer(CustomerStructure structure) { super(); customerStructure = structure; } @Override public void initialize(FactoredCustomerService service, CustomerStructure structure) { log.info("Initializing customer " + customerStructure.name); this.service = service; NodeList capacityBundleNodes = customerStructure.getConfigXml().getElementsByTagName("capacityBundle"); for (int i = 0; i < capacityBundleNodes.getLength(); ++i) { Element capacityBundleElement = (Element) capacityBundleNodes.item(i); CapacityBundle capacityBundle = createCapacityBundle(structure, capacityBundleElement); capacityBundle.initialize(structure, capacityBundleElement); capacityBundles.add(capacityBundle); getCustomerRepo().add(capacityBundle.getCustomerInfo()); } utilityOptimizer = createUtilityOptimizer(structure, capacityBundles); utilityOptimizer.initialize(service); log.info("Successfully initialized customer " + customerStructure.name); } // Component accessors protected CustomerRepo getCustomerRepo() { return service.getCustomerRepo(); } protected TimeslotRepo getTimeslotRepo() { return service.getTimeslotRepo(); } /** @Override hook **/ protected CapacityBundle createCapacityBundle(CustomerStructure structure, Element capacityBundleElement) { return new DefaultCapacityBundle(service, structure, capacityBundleElement); } /** @Override hook **/ protected UtilityOptimizer createUtilityOptimizer(CustomerStructure structure, List<CapacityBundle> capacityBundles) { return new DefaultUtilityOptimizer(structure, capacityBundles); } // @Override // public void evaluateTariffs() // { // Timeslot timeslot = getTimeslotRepo().currentTimeslot(); // log.info("Customer " + getName() + " evaluating tariffs at timeslot " + timeslot.getSerialNumber()); // utilityOptimizer.evaluateTariffs(); // } // @Override // public void updatedSubscriptionRepo() { // utilityOptimizer.updatedSubscriptionRepo(); // } @Override public void handleNewTimeslot() { // Timeslot timeslot = getTimeslotRepo().currentTimeslot(); // log.info("Customer " + getName() + " activated for timeslot " + timeslot.getSerialNumber()); // utilityOptimizer.handleNewTimeslot(timeslot); } String getName() { return customerStructure.name; } CustomerStructure getCustomerStructure() { return customerStructure; } @Override public String toString() { return this.getClass().getCanonicalName() + ":" + getName(); } // STATIC INNER CLASS public static class Creator implements CustomerCreator { @Override public String getKey() { return null; // registered as default creator } @Override public FactoredCustomer createModel(CustomerStructure structure) { return new DefaultFactoredCustomer(structure); } } private static Creator creator = new Creator(); public static CustomerCreator getCreator() { return creator; } // added: data access methods @Override public List<CapacityBundle> getCapacityBundlesOfTypeThatCanUse(PowerType pt) { List<CapacityBundle> result = new ArrayList<CapacityBundle>(); for (CapacityBundle bundle : capacityBundles) { if (bundle.getPowerType().canUse(pt)) { result.add(bundle); } } return result; } @Override public List<CapacityBundle> getCapacityBundles() { List<CapacityBundle> result = new ArrayList<CapacityBundle>(); for (CapacityBundle bundle : capacityBundles) { result.add(bundle); } return result; } /** * use my predictions to override the originator's profile */ @Override public void updateEnergyRecord(CapacityBundle bundle, RealVector populationEstimatedEnergy, int currentTimeslot) { List<CapacityOriginator> capacityOriginators = bundle.getCapacityOriginators(); int numOriginators = capacityOriginators.size(); // divide estimated energy by number of originators RealVector originatorEnergy = populationEstimatedEnergy.mapDivide(numOriginators); for (CapacityOriginator originator : capacityOriginators) { originator.convertEnergyProfileFromBrokerToServer(originatorEnergy, currentTimeslot); } } @Override public void updateWithShiftingAndElasticity(int currentTimeslot) { utilityOptimizer.recommendProfilesToBundles(currentTimeslot); } @Override public void cleanSubscriptionRelatedData() { for (CapacityBundle bundle : capacityBundles) { bundle.clearSubscriptionRelatedData(); } } } // end class