Skip to content

Commit

Permalink
InvestmentAllocation: switch from nanos to micros
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed May 31, 2015
1 parent 4ad1209 commit 741a0b3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 44 deletions.
Expand Up @@ -16,7 +16,6 @@

package org.optaplanner.examples.investmentallocation.domain;

import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.annotations.XStreamAlias;
Expand All @@ -26,10 +25,10 @@
public class AssetClass extends AbstractPersistable {

private String name;
private long expectedReturnNanos; // In nano's (so multiplied by 10^9)
private long standardDeviationRiskNanos; // In nano's (so multiplied by 10^9)
private long expectedReturnMicros; // In micro's (so multiplied by 10^6)
private long standardDeviationRiskMicros; // In micro's (so multiplied by 10^6)

private Map<AssetClass, Long> correlationNanosMap;
private Map<AssetClass, Long> correlationMicrosMap;

public String getName() {
return name;
Expand All @@ -39,28 +38,28 @@ public void setName(String name) {
this.name = name;
}

public long getExpectedReturnNanos() {
return expectedReturnNanos;
public long getExpectedReturnMicros() {
return expectedReturnMicros;
}

public void setExpectedReturnNanos(long expectedReturnNanos) {
this.expectedReturnNanos = expectedReturnNanos;
public void setExpectedReturnMicros(long expectedReturnMicros) {
this.expectedReturnMicros = expectedReturnMicros;
}

public long getStandardDeviationRiskNanos() {
return standardDeviationRiskNanos;
public long getStandardDeviationRiskMicros() {
return standardDeviationRiskMicros;
}

public void setStandardDeviationRiskNanos(long standardDeviationRiskNanos) {
this.standardDeviationRiskNanos = standardDeviationRiskNanos;
public void setStandardDeviationRiskMicros(long standardDeviationRiskMicros) {
this.standardDeviationRiskMicros = standardDeviationRiskMicros;
}

public Map<AssetClass, Long> getCorrelationNanosMap() {
return correlationNanosMap;
public Map<AssetClass, Long> getCorrelationMicrosMap() {
return correlationMicrosMap;
}

public void setCorrelationNanosMap(Map<AssetClass, Long> correlationNanosMap) {
this.correlationNanosMap = correlationNanosMap;
public void setCorrelationMicrosMap(Map<AssetClass, Long> correlationMicrosMap) {
this.correlationMicrosMap = correlationMicrosMap;
}

// ************************************************************************
Expand Down
Expand Up @@ -28,7 +28,7 @@ public class AssetClassAllocation extends AbstractPersistable {
private AssetClass assetClass;

// Planning variables: changes during planning, between score calculations.
private Long quantityNanos; // In nano's (so multiplied by 10^9)
private Long quantityMicros; // In micro's (so multiplied by 10^6)

public AssetClass getAssetClass() {
return assetClass;
Expand All @@ -38,13 +38,13 @@ public void setAssetClass(AssetClass assetClass) {
this.assetClass = assetClass;
}

@PlanningVariable(valueRangeProviderRefs = {"quantityNanosRange"})
public Long getQuantityNanos() {
return quantityNanos;
@PlanningVariable(valueRangeProviderRefs = {"quantityMicrosRange"})
public Long getQuantityMicros() {
return quantityMicros;
}

public void setQuantityNanos(Long quantityNanos) {
this.quantityNanos = quantityNanos;
public void setQuantityMicros(Long quantityMicros) {
this.quantityMicros = quantityMicros;
}

// ************************************************************************
Expand Down
Expand Up @@ -76,9 +76,9 @@ public void setScore(HardSoftLongScore score) {
// Complex methods
// ************************************************************************

@ValueRangeProvider(id = "quantityNanosRange")
public CountableValueRange<Long> getQuantityNanosRange() {
return ValueRangeFactory.createLongValueRange(0L, 1000000000L);
@ValueRangeProvider(id = "quantityMicrosRange")
public CountableValueRange<Long> getQuantityMicrosRange() {
return ValueRangeFactory.createLongValueRange(0L, 1000000L);
}

public Collection<? extends Object> getProblemFacts() {
Expand Down
Expand Up @@ -16,20 +16,14 @@

package org.optaplanner.examples.investmentallocation.persistence;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.optaplanner.core.api.domain.solution.Solution;
import org.optaplanner.examples.common.persistence.AbstractTxtSolutionImporter;
import org.optaplanner.examples.investmentallocation.domain.AssetClass;
Expand Down Expand Up @@ -99,15 +93,15 @@ private void readAssetClassList() throws IOException {
+ ") has an assetClass id (" + id + ") that is not in the headerLine (" + headerLine + ")");
}
assetClass.setName(tokens[1]);
assetClass.setExpectedReturnNanos(parsePercentageNanos(tokens[2]));
assetClass.setStandardDeviationRiskNanos(parsePercentageNanos(tokens[3]));
Map<AssetClass, Long> correlationNanosMap = new LinkedHashMap<AssetClass, Long>(assetClassListSize);
assetClass.setExpectedReturnMicros(parsePercentageMicros(tokens[2]));
assetClass.setStandardDeviationRiskMicros(parsePercentageMicros(tokens[3]));
Map<AssetClass, Long> correlationMicrosMap = new LinkedHashMap<AssetClass, Long>(assetClassListSize);
for (int i = 0; i < assetClassListSize; i++) {
AssetClass other = assetClassList.get(i);
long correlationNanos = parsePercentageNanos(tokens[ASSET_CLASS_PROPERTIES_COUNT + i]);
correlationNanosMap.put(other, correlationNanos);
long correlationMicros = parsePercentageMicros(tokens[ASSET_CLASS_PROPERTIES_COUNT + i]);
correlationMicrosMap.put(other, correlationMicros);
}
assetClass.setCorrelationNanosMap(correlationNanosMap);
assetClass.setCorrelationMicrosMap(correlationMicrosMap);
}
solution.setAssetClassList(assetClassList);
}
Expand All @@ -124,16 +118,16 @@ private void createAssetClassAllocationList() {
solution.setAssetClassAllocationList(assetClassAllocationList);
}

protected long parsePercentageNanos(String token) {
BigDecimal nanos;
protected long parsePercentageMicros(String token) {
BigDecimal micros;
if (token.endsWith("%")) {
nanos = new BigDecimal(token.substring(0, token.length() - 1))
.multiply(new BigDecimal(10000000L));
micros = new BigDecimal(token.substring(0, token.length() - 1))
.multiply(new BigDecimal(10000L));
} else {
nanos = new BigDecimal(token)
.multiply(new BigDecimal(1000000000L));
micros = new BigDecimal(token)
.multiply(new BigDecimal(1000000L));
}
return nanos.longValueExact();
return micros.longValueExact();
}

}
Expand Down

0 comments on commit 741a0b3

Please sign in to comment.