src/de/pik/lagom/generic/ImportExport.java

00001 
00002 // Copyright 2010 by Carlo Jaeger, Antoine Mandel, Steffen Fuerst and European Climate Forum
00003 // Licensed under the Open Software License version 3.0
00004 // See the file "License-OSL-3.0.txt" in the distribution for more information 
00005 // The License text can be also found under http://www.opensource.org/licenses/osl-3.0.php
00007 
00008 package de.pik.lagom.generic;
00009 
00010 import static de.pik.lagom.annotations.Initialization.*;
00011 import static de.pik.lagom.annotations.Variability.*;
00012 import de.pik.lagom.annotations.Description;
00013 import de.pik.lagom.annotations.Initialization;
00014 import de.pik.lagom.annotations.NameInUI;
00015 import de.pik.lagom.annotations.Variability;
00016 import de.pik.lagom.annotations.WriteToFile;
00017 import de.pik.lagom.generic.initvalues.ImportExportInitValues;
00018 import de.pik.lagom.toolbox.ArrayTools;
00019 import de.pik.lagom.toolbox.ProbeBase;
00020 import de.pik.lagom.toolbox.StepManager;
00021 import de.pik.lagom.toolbox.StepManager.Callback;
00022 
00023 import net.sf.oval.constraint.NotNegative;
00024 import net.sf.oval.guard.Guarded;
00025 import net.sf.oval.guard.PostValidateThis;
00026 import net.sf.oval.guard.PreValidateThis;
00027 
00036 @Guarded
00037 public class ImportExport implements IBuyer, ISeller {
00038   public class Probe extends ProbeBase {
00039     public class StepCallback extends Callback {
00040       public Object getOwner() {
00041         return ImportExport.Probe.this;
00042       }
00043     }
00044 
00045     @Description("The quantity of goods exported in the last exchange/trading step.")
00046     @Variability(PERIOD)
00047     @Initialization(INDIVIDUAL)
00048     double quantityExported;
00049 
00050     @Description("The quantity of goods imported in the last exchange/trading step.")
00051     @Variability(PERIOD)
00052     @Initialization(INDIVIDUAL)
00053     double quantityImported;
00054 
00055     void init() {
00056       foundation.getProbeManager().addProbe(ImportExport.Probe.this);
00057 
00058       foundation.getStepManager().registerCallback(Foundation.EXCHANGE_STEP,
00059                                                     StepManager.Timing.POST,
00060                                                     1,
00061                                                     new StepCallback() {
00062         public void postStep() {
00063           quantityExported = maxExportInPeriod - exportLeft;
00064           quantityImported = maxImportInPeriod - importLeft;
00065         }
00066       });
00067     }
00068 
00069     public String toString() {
00070       return "ImportExport of Sector " + sector.getName();
00071     }
00072 
00073     public String portrayalName() {
00074       return sector.getName();
00075     }
00076 
00077     @WriteToFile
00078     @NameInUI("Exported Quantity")
00079     @Description("The quantity of goods exported in the last exchange/trading step.")
00080     public double getQuantityExported() {
00081       return quantityExported;
00082     }
00083 
00084     @WriteToFile
00085     @NameInUI("Imported Quantity")
00086     @Description("The quantity of goods imported in the last exchange/trading step.")
00087     public double getQuantityImported() {
00088       return quantityImported;
00089     }
00090 
00091     @WriteToFile
00092     @NameInUI("Potential Export")
00093     @Description("The maximal number of units of goods that can be exported in this period.")
00094     public double getMaxExport() {
00095       return maxExportInPeriod;
00096     }
00097 
00098     @WriteToFile
00099     @NameInUI("Potential Import")
00100     @Description("The maximal number of units of goods that can be imported in this period.")
00101     public double getMaxImport() {
00102       return maxImportInPeriod;
00103     }
00104   }
00105 
00106   Foundation foundation;
00107 
00108   Sector sector;
00109 
00110   ImportExportInitValues initValues;
00111 
00112   ImportExport.Probe probe = new ImportExport.Probe();
00113 
00114   Suppliers suppliers = new Suppliers();
00115     
00117   @NotNegative
00118   @Description("The maximal number of units of goods that can be imported in this period.")
00119   @Variability(PERIOD)
00120   @Initialization(INDIVIDUAL)
00121   double maxImportInPeriod;
00122 
00129   @NotNegative
00130   @Variability(VOLATILE)
00131   @Initialization(INDIVIDUAL)
00132   @Description("The number of units that can be imported at the actual time.")
00133   double importLeft;
00134 
00136   @NotNegative
00137   @Description("The maximal number of units of goods that can be exported in this period.")
00138   @Variability(PERIOD)
00139   @Initialization(INDIVIDUAL)
00140   double maxExportInPeriod;
00141 
00150   @NotNegative
00151   @Variability(VOLATILE)
00152   @Initialization(INDIVIDUAL)
00153   @Description("The number of units that can be exported at the actual time.")
00154   double exportLeft;
00155 
00156 
00163   ImportExport(Foundation pFoundation, ImportExportInitValues pInitValues) {
00164     foundation = pFoundation;
00165     initValues = pInitValues;
00166   }
00167 
00177   public void init(Sector pSector) {
00178     sector = pSector;
00179     maxImportInPeriod = sector.getInitProduction() *
00180       initValues.getInitialMaxImportAsOverallProductionFactorArray()[sector.getArrayIndex()];
00181     importLeft = maxImportInPeriod;
00182 
00183     maxExportInPeriod = sector.getInitConsumption() *
00184       initValues.getInitialMaxExportAsOverallProductionFactorArray()[sector.getArrayIndex()];
00185     exportLeft = maxExportInPeriod;
00186 
00187     probe.init();
00188 
00189     suppliers.init(foundation, this, pSector, initValues.getObservedSuppliersQuotient());
00190   }
00191 
00197   @PreValidateThis
00198   @PostValidateThis
00199   public void trade() {
00200     suppliers.updateSuppliers(Suppliers.SellerSet.EXCLUDE_IMPORT);
00201     //add supppliers in case of rationning
00202     suppliers.addSuppliers(exportLeft, Suppliers.SellerSet.EXCLUDE_IMPORT);
00203     // buy 
00204     final double lTrade = Math.min(exportLeft, suppliers.maxSupply());
00205     suppliers.buy(lTrade);
00206     exportLeft -= lTrade;
00207   }
00211   @PostValidateThis
00212   public void account() {
00213     maxImportInPeriod *= initValues.getGrowthOfMaxImport();
00214     maxImportInPeriod= Math.max(maxImportInPeriod, sector.getOverallProduction() *
00215       initValues.getInitialMaxImportAsOverallProductionFactorArray()[sector.getArrayIndex()]);
00216     importLeft = maxImportInPeriod;
00217 
00218     maxExportInPeriod = sector.getOverallProduction() *
00219       initValues.getInitialMaxExportAsOverallProductionFactorArray()[sector.getArrayIndex()];
00220     exportLeft = maxExportInPeriod;
00221   }
00222 
00223   // ISeller methods
00227   public void sell(double pQuantity) {
00228     importLeft -= pQuantity;
00229   }
00230   
00234   public double getPrice() {
00235     return sector.getStats().getMaxPrice() * 1.001;
00236   }
00237   
00241   public double getInventory() {
00242     return importLeft;
00243   }
00244 
00248   public Sector getProductionSector() {
00249     return sector;
00250   } 
00251   
00252   // getter/setter
00253   public ImportExport.Probe getProbe() {
00254     return probe;
00255   }
00256   
00257   
00258   public double getBenchmarkSupply() {
00259     return maxImportInPeriod;
00260   }
00261   
00262   public double getBAUSupply() {
00263     return maxExportInPeriod;
00264   }
00265   
00266   public double getExtraSupply() {
00267     return 0;
00268   }
00269 
00270   
00271   public double getBenchmarkDemand(Sector lSector){
00272     return exportLeft;
00273   }
00274   
00275   public Suppliers getSuppliers(Sector lSector) {
00276     assert(sector==lSector);
00277     return suppliers;
00278   }
00279   
00280   public double[] getRationing(){
00281   return ArrayTools.createArray(foundation.getNumSectors(),0.0);
00282   }
00283 
00284   public double getObservedSuppliersQuotient(){
00285     return 1;
00286   }
00287 
00288 }
00289 
00291 // EOF
00293 

Generated on Tue Sep 14 11:11:48 2010 for lagom_generiC by  doxygen 1.5.4