EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
TargetInvestmentRole.java
1 /*******************************************************************************
2  * Copyright 2012 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package emlab.gen.role.investment;
17 
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.beans.factory.annotation.Configurable;
20 import org.springframework.data.annotation.Transient;
21 import org.springframework.data.neo4j.annotation.NodeEntity;
22 import org.springframework.transaction.annotation.Transactional;
23 
35 
40 @Configurable
41 @NodeEntity
42 public class TargetInvestmentRole extends GenericInvestmentRole<TargetInvestor> {
43 
44  @Transient
45  @Autowired Reps reps;
46 
47  @Override
48  @Transactional
49  public void act(TargetInvestor targetInvestor) {
50 
51  for(PowerGeneratingTechnologyTarget target : targetInvestor.getPowerGenerationTechnologyTargets()){
52  PowerGeneratingTechnology pgt = target.getPowerGeneratingTechnology();
53  long futureTimePoint = getCurrentTick()+pgt.getExpectedLeadtime()+pgt.getExpectedPermittime();
54  double expectedInstalledCapacity = reps.powerPlantRepository.calculateCapacityOfExpectedOperationalPowerPlantsInMarketAndTechnology(targetInvestor.getInvestorMarket(), pgt, futureTimePoint);
55  double pgtNodeLimit = Double.MAX_VALUE;
56  // For simplicity using the market, instead of the node here. Needs
57  // to be changed, if more than one node per market exists.
58  PowerGeneratingTechnologyNodeLimit pgtLimit = reps.powerGeneratingTechnologyNodeLimitRepository
59  .findOneByTechnologyAndMarket(pgt, targetInvestor.getInvestorMarket());
60  if (pgtLimit != null) {
61  pgtNodeLimit = pgtLimit.getUpperCapacityLimit(futureTimePoint);
62  }
63  double targetCapacity = target.getTrend().getValue(futureTimePoint);
64  double installedCapacityDeviation = 0;
65  if (pgtNodeLimit > targetCapacity) {
66  installedCapacityDeviation = targetCapacity - expectedInstalledCapacity;
67  } else {
68  installedCapacityDeviation = pgtNodeLimit - expectedInstalledCapacity;
69  }
70 
71  if (installedCapacityDeviation > 0 && installedCapacityDeviation > pgt.getCapacity()) {
72 
73  double powerPlantCapacityRatio = installedCapacityDeviation/pgt.getCapacity();
74 
75  PowerPlant plant = new PowerPlant();
76  plant.specifyNotPersist(getCurrentTick(), targetInvestor, reps.powerGridNodeRepository.findFirstPowerGridNodeByElectricitySpotMarket(targetInvestor.getInvestorMarket()), pgt);
77  plant.setActualNominalCapacity(pgt.getCapacity()*powerPlantCapacityRatio);
78  PowerPlantManufacturer manufacturer = reps.genericRepository.findFirst(PowerPlantManufacturer.class);
79  BigBank bigbank = reps.genericRepository.findFirst(BigBank.class);
80 
81  double investmentCostPayedByEquity = plant.getActualInvestedCapital() * (1 - targetInvestor.getDebtRatioOfInvestments())*powerPlantCapacityRatio;
82  double investmentCostPayedByDebt = plant.getActualInvestedCapital() * targetInvestor.getDebtRatioOfInvestments()*powerPlantCapacityRatio;
83  double downPayment = investmentCostPayedByEquity;
84  createSpreadOutDownPayments(targetInvestor, manufacturer, downPayment, plant);
85 
86  double amount = determineLoanAnnuities(investmentCostPayedByDebt, plant.getTechnology().getDepreciationTime(),
87  targetInvestor.getLoanInterestRate());
88  // logger.warn("Loan amount is: " + amount);
89  Loan loan = reps.loanRepository.createLoan(targetInvestor, bigbank, amount, plant.getTechnology().getDepreciationTime(),
90  getCurrentTick(), plant);
91  // Create the loan
92  plant.createOrUpdateLoan(loan);
93 
94  }
95  }
96 
97  }
98 
99  private void createSpreadOutDownPayments(EnergyProducer agent, PowerPlantManufacturer manufacturer, double totalDownPayment,
100  PowerPlant plant) {
101  int buildingTime = (int) plant.getActualLeadtime();
102  for (int i = 0; i < buildingTime; i++) {
103  reps.nonTransactionalCreateRepository.createCashFlow(agent, manufacturer, totalDownPayment / buildingTime,
104  CashFlow.DOWNPAYMENT, getCurrentTick() + i, plant);
105  }
106  }
107 
108  @Override
109  public double determineLoanAnnuities(double totalLoan, double payBackTime, double interestRate) {
110 
111  double q = 1 + interestRate;
112  double annuity = totalLoan * (Math.pow(q, payBackTime) * (q - 1)) / (Math.pow(q, payBackTime) - 1);
113 
114  return annuity;
115  }
116 
117 }