Я не знаю, почему MyCodel SimulationContoller не может быть подключен автоматически.И там появилось множество проблемных сообщений.Не могли бы вы мне помочь?
Приведенный ниже код в основном зависит от JASA (http://jasa.sourceforge.net/)and JABM (http://jabm.sourceforge.net/)and Пример встраивания JASA, который преобразован в код Java.
Вы можете установить его с основным классом MyLauncher.java и параметром VM, равным -Djabm.config=MyMain.java
в редакторе IntelliJ.
MyLauncher (основной класс)
package MyMy;
import net.sourceforge.jabm.DesktopSimulationManager;
import net.sourceforge.jabm.SimulationController;
import net.sourceforge.jabm.spring.PropertyOverrideWithReferencesConfigurer;
import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.swing.*;
import java.net.URL;
public class MyLauncher {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(ScopeConfig.class);
context.register(MyMain.class);
context.refresh();
final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(context);
final DesktopSimulationManager manager = new DesktopSimulationManager() {
@Override
public SimulationController getSimulationController() {
return (SimulationController) simulationController.getSimulation();
}
@Override
protected ImageIcon createImageIcon(String path, String description) {
URL imgURL = ClassLoader.getSystemResource("net/sourceforge/jabm/" + path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
throw new RuntimeException("Couldn't find file: " + path);
}
}
@Override
public void runSingleExperiment() {
if (this.simulationProperties != null) {
PropertyOverrideConfigurer configurer = new PropertyOverrideWithReferencesConfigurer();
configurer.setProperties(simulationProperties);
configurer.postProcessBeanFactory(beanFactory);
}
launch((SimulationController) beanFactory.getBean("simulationController"));
}
};
manager.initialise();
}
}
MyMain
package MyMy;
import net.sourceforge.jabm.SpringSimulationController;
import net.sourceforge.jabm.report.Report;
import net.sourceforge.jabm.util.MutableStringWrapper;
import org.springframework.context.annotation.Bean;
import java.util.ArrayList;
import java.util.Arrays;
public class MyMain {
@Bean
public SpringSimulationController simulationController(Report priceTimeSeriesChart,
Report currentPriceTimeSeriesChart,
Report equilibriumPriceTimeSeriesChart,
Report currentPriceCSVReport) {
SpringSimulationController ret = new SpringSimulationController();
ret.setSimulationBeanName("marketSimulation");
ret.setNumSimulations(10);
ret.setReports(new ArrayList<>(Arrays.asList(priceTimeSeriesChart, currentPriceTimeSeriesChart, equilibriumPriceTimeSeriesChart, currentPriceCSVReport)));
ret.setModelDescription("Iori and Chiarella - A Simulation analysis of a the microstructure of double auction markets");
return ret;
}
MyModel
import org.springframework.stereotype.Repository;
import java.util.Arrays;
import java.util.Date;
@Repository
@Configuration
public class MyModel {
@Bean
@Scope("simulation")
MarketSimulation marketSimulation(@Lazy SimulationController simulationController,
@Lazy Population evolvingPopulation,
AgentMixer randomArrivalAgentMixer,
@Lazy AgentInitialiser agentInitialiserProxy,
Auctioneer cda) {
MarketSimulation ret = new MarketSimulation();
ret.setSimulationController(simulationController);
return ret;
}
@Bean
@Scope("simulation")
public TimePriorityPricingPolicy timePriorityPricing() {
return new TimePriorityPricingPolicy();
}
@Bean
@Scope("simulation")
public ContinuousDoubleAuctioneer cda(TimePriorityPricingPolicy pricingPolicy) {
ContinuousDoubleAuctioneer ret = new ContinuousDoubleAuctioneer();
ret.setPricingPolicy(pricingPolicy);
return ret;
}
@Bean
@Scope("simulation")
public RandomArrivalAgentMixer randomArrivalAgentMixer(MersenneTwister64 prng) {
RandomArrivalAgentMixer ret = new RandomArrivalAgentMixer(prng);
return ret;
}
@Bean
@Scope("simulation")
public ProxyFactoryBean agentInitialiserProxy() {
LazyInitTargetSource src = new LazyInitTargetSource();
src.setTargetBeanName("agentInitialiser");
ProxyFactoryBean ret = new ProxyFactoryBean();
ret.setTargetSource(src);
return ret;
}
@Bean
@Scope("simulation")
public MarketAgentInitialiser agentInitialiser(MarketSimulation marketSimulation) {
MarketAgentInitialiser ret = new MarketAgentInitialiser();
ret.setMarket(marketSimulation);
return ret;
}
@Bean
public MersenneTwister64 prng() {
return new MersenneTwister64(new Date());
}
@Bean
@Scope("simulation")
public AgentList chartists(ObjectFactory<Agent> chartistFactory) {
AgentList ret = new AgentList();
ret.setAgentFactory(chartistFactory);
ret.populateFromFactory();
return ret;
}
@Bean
@Scope("simulation")
public ObjectFactoryCreatingFactoryBean chartistFactory() {
ObjectFactoryCreatingFactoryBean ret = new ObjectFactoryCreatingFactoryBean();
ret.setTargetBeanName("chartistPrototype");
return ret;
}
@Bean
@Scope("prototype")
public SimpleTradingAgent chartistPrototype(SimpleMarkupStrategy returnForecastStrategy,
ReturnForecastValuationPolicy chartistValuationPolicy) {
SimpleTradingAgent ret = new SimpleTradingAgent();
ret.setStrategy(returnForecastStrategy);
ret.setValuationPolicy(chartistValuationPolicy);
return ret;
}
@Bean
@Scope("prototype")
public SimpleMarkupStrategy returnForecastStrategy(SimulationController simulationController,
UniformDistribution markupDistribution,
ForecastTradeDirectionPolicy tradeDirectionPolicy,
MersenneTwister64 prng) {
SimpleMarkupStrategy ret = new SimpleMarkupStrategy();
ret.setScheduler(simulationController);
ret.setMarkupDistribution(markupDistribution);
ret.setPrng(prng);
ret.setTradeDirectionPolicy(tradeDirectionPolicy);
return ret;
}
@Bean
public ForecastTradeDirectionPolicy tradeDirectionPolicy(MersenneTwister64 prng) {
ForecastTradeDirectionPolicy ret = new ForecastTradeDirectionPolicy();
ret.setPrng(prng);
return ret;
}
@Bean
public UniformDistribution markupDistribution(MersenneTwister64 prng) {
UniformDistribution ret = new UniformDistribution();
ret.setMax(0.5);
ret.setMin(0.);
ret.setPrng(prng);
return ret;
}
@Bean
public MutableDoubleWrapper gbmFundamentalPrice() {
MutableDoubleWrapper ret = new MutableDoubleWrapper();
ret.setValue(500.);
return ret;
}
@Bean
@Scope("simulation")
public GeometricBrownianMotionPriceProcess gbmPriceProcess(MutableDoubleWrapper gbmFundamentalPrice,
MersenneTwister64 prng) {
GeometricBrownianMotionPriceProcess ret = new GeometricBrownianMotionPriceProcess();
ret.setPriceWrapper(gbmFundamentalPrice);
ret.setDrift(0.2);
ret.setVolatility(0.4);
ret.setDt(0.00005);
ret.setPrng(prng);
return ret;
}
@Bean
@Scope("prototype")
public ReturnForecastValuationPolicy chartistValuationPolicy(ChartistForecaster chartistForecaster) {
ReturnForecastValuationPolicy ret = new ReturnForecastValuationPolicy();
ret.setForecaster(chartistForecaster);
return ret;
}
@Bean
@Scope("prototype")
public ChartistForecaster chartistForecaster(final MarketSimulation mark,
UniformDistribution chartistWindowSizeDistribution) {
class _internal extends ChartistForecaster {
private _internal() {
this.market = mark;
}
}
ChartistForecaster ret = new _internal();
ret.setWindowSizeDistribution(chartistWindowSizeDistribution);
return ret;
}
@Bean
@Scope("prototype")
public UniformDistribution chartistWindowSizeDistribution(MersenneTwister64 prng) {
UniformDistribution ret = new UniformDistribution();
ret.setPrng(prng);
return ret;
}
@Bean
@Scope("simulation")
public EvolvingPopulation evolvingPopulation(AgentList agentList,
MersenneTwister64 prng,
CombiBreeder uniformBreeder) {
EvolvingPopulation ret = new EvolvingPopulation();
ret.setAgentList(agentList);
ret.setPrng(prng);
ret.setBreeder(uniformBreeder);
ret.setBreedingInterval(1);
return ret;
}
@Bean
public CombiBreeder uniformBreeder(//Breeder mutationBreeder,
Breeder fitnessProportionateBreeder) {
CombiBreeder ret = new CombiBreeder();
ret.setBreedingPipeline(Arrays.asList(//mutationBreeder,
fitnessProportionateBreeder));
return ret;
}
@Bean
public MutationBreeder mutationBreeder(MersenneTwister64 prng) {
MutationBreeder ret = new MutationBreeder();
ret.setPrng(prng);
ret.setMutationOperator(new WeightMutationOperator());
return ret;
}
@Bean
@Scope("simulation")
public FitnessProportionateBreeder fitnessProportionateBreeder(MersenneTwister64 prng) {
FitnessProportionateBreeder ret = new FitnessProportionateBreeder();
ret.setPrng(prng);
ret.setFitnessFunction(new ForecastErrorFitnessFunction());
ret.setImitationOperator(new ValuationPolicyImitationOperator());
return ret;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jasa_sample</groupId>
<artifactId>jasa_sample</artifactId>
<version>1.0-SNAPSHOT</version>
<name>jasa_sample</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sourceforge.jasa/jasa -->
<dependency>
<groupId>net.sourceforge.jasa</groupId>
<artifactId>jasa</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>