📄 tradermarketmodelswarm3.java
字号:
import swarm.Globals;
import swarm.Selector;
import swarm.defobj.Zone;
import swarm.defobj.SymbolImpl;
import swarm.defobj.FArguments;
import swarm.defobj.FArgumentsImpl;
import swarm.defobj.FCall;
import swarm.defobj.FCallImpl;
import swarm.activity.Activity;
import swarm.activity.ActionGroup;
import swarm.activity.ActionGroupImpl;
import swarm.activity.Schedule;
import swarm.activity.ScheduleImpl;
import swarm.activity.FActionForEach;
import swarm.objectbase.Swarm;
import swarm.objectbase.SwarmImpl;
import swarm.objectbase.VarProbe;
import swarm.objectbase.MessageProbe;
import swarm.objectbase.EmptyProbeMapImpl;
import java.util.LinkedList;
import java.util.List;
public class TraderMarketModelSwarm3 extends SwarmImpl
{
//添加变量的声明
/*public static final int NUM=10;
public static final int T=500;
public double limitPrice;
public int numTraders;
public int time=0;
public static double expectedPrice[]=new double[T+1];
public double r;
public double beta,gama;
public double tao[]=new double[T+1];
public static double slopeProfit[]=new double[T+1];
public boolean randomizeTraderUpdateOrder;*/
public static final int NUM=10;
public static final int T=500;
//public static final double limitPrice=300;
public int numTraders;
public double limitPrice;
public double r;
public static final double R=1.0,A=0.0;
public int time=0;
public double bprice,sprice;//bprice是买方开价。sprice是此买者卖时的心理价位
public static double bargainprice[]=new double[T+1];
//public double beta=0.1,gama=0.6;
public double beta,gama;
public double max,sellmax,roalprice;//max为买时价格;sellmax为卖时价格
public double tao[]=new double[T+1];
public static double expectedPrice[]=new double[T+1];
public static double slopeProfit[]=new double[T+1];
public static boolean isSold;
public double price[]=new double[NUM];
public double sellprice[]=new double[NUM];
public double seller[]=new double[NUM];
public int updateseller;
public boolean randomizeTraderUpdateOrder;
public ActionGroup modelActions;
public Schedule modelSchedule;
public List traderList;
public TraderMarket tradermarket;
FActionForEach actionForEach;
public List getTraderList(){
return traderList;
}
public double getExpectedPrice(int t){
return expectedPrice[t];
}
/*public void init(){
int i,k,k1;
double max=0.0;
price[0]=300+400*Math.random();//定义第一个买家的喊价(随机)
sellprice[0]=price[0]*(1+0.1);//0.1是市场当时的平均收益率
System.out.println("price[0]="+price[0]+" "+"sellprice[0]="+sellprice[0]);
System.out.println("sellprice[0]="+sellprice[0]);
for(k=1;k<NUM;k++)//根据第一个买家喊价,以后的买家依次喊出的价格
{
double temp=0.0;
sellmax=0.0;
for(k1=0;k1<k;k1++)
{temp=price[k1]+temp;}
//price[k]=(temp/k)-100+200*Math.random();//后面的买家的喊价是用前面两个买家的喊价的加权平均算出的,要在加权平均的基础上加个随机数
//sellprice[k]=price[k]*(1+0.1);//根据市场平均收益率算出卖时心理价位(0.1为市场当时的平均收益率),如有可能将加入一个收益的随机扰动。
//price[k]=price[k-1]-100+200*Math.random();
//System.out.println("price["+k+"]="+price[k]+" "+"sellprice["+k+"]="+sellprice[k]);
}
}*/
public boolean toggleRandomizedOrder(){
randomizeTraderUpdateOrder=!randomizeTraderUpdateOrder;
syncUpdateOrder();
return randomizeTraderUpdateOrder;
}
public void syncUpdateOrder(){
if(actionForEach!=null)
actionForEach.setDefaultOrder
(randomizeTraderUpdateOrder?Globals.env.Randomized:Globals.env.Sequential);
}
public Object addTrader(Trader3 aTrader){
traderList.add(aTrader);
return this;
}
public TraderMarketModelSwarm3(Zone aZone){
super(aZone);
//添加在模型swarm图中需要显示的各属性的内容
numTraders=10;
randomizeTraderUpdateOrder=false;
limitPrice=300;//可接受的最低价格
r=0.1;//市场平均收益率
beta=0.1/*~0.5?*/;
gama=0.6;
class TraderMarketModelProbeMap extends EmptyProbeMapImpl
{
private VarProbe probeVariable(String name){
return
Globals.env.probeLibrary.getProbeForVariable$inClass
(name,TraderMarketModelSwarm3.this.getClass());
}
private MessageProbe probeMessage(String name){
return
Globals.env.probeLibrary.getProbeForMessage$inClass
(name,TraderMarketModelSwarm3.this.getClass());
}
private void addVar(String name){
addProbe(probeVariable(name));
}
private void addMessage(String name){
addProbe(probeMessage(name));
}
public TraderMarketModelProbeMap(Zone _aZone,Class aClass){
super(_aZone,aClass);
//可能还需要添加点别的内容,依赖于前面的属性
addVar("numTraders");
addVar("limitPrice");
addVar("r");
addVar("beta");
addVar("gama");
addMessage("toggleRandomizedOrder");
addMessage("addTrader:");
}
}
Globals.env.probeLibrary.setProbeMap$For
(new TraderMarketModelProbeMap(aZone,getClass()),getClass());
}
public Object buildObjects(){
int i;
super.buildObjects();
traderList=new LinkedList();
tradermarket=new TraderMarket();
for(i=0;i<numTraders;i++){
Trader3 aTrader;
//double p;
//p=Globals.env.uniformDblRand.getDoubleWithMin$withMax(0.0,MAX);
aTrader=new Trader3(numTraders);
traderList.add(aTrader);
//aTrader.setPrice(p);
//aTrader.setRandomEncounterProb(randomEncounterProb);
}
return this;
}
public Object buildActions(){
//该部分是对仿真系统的各种行为的时间上的排序应该按照系统行为发生的顺序编写代码!这与CarMarket不同,两个try似乎应该调顺序。调换后如下:
super.buildActions();
modelActions=new ActionGroupImpl(getZone());
try{
modelActions.createActionTo$message
(tradermarket,new Selector(tradermarket.getClass(),"init",false));
}catch(Exception e){
System.err.println("Exception init"+e.getMessage());
}
/*try{
Trader3 proto=(Trader3)traderList.get(0);
Selector sel=new Selector(proto.getClass(),"init",false);
actionForEach=modelActions.createFActionForEachHomogeneous$call
(traderList,new FCallImpl(this,proto,sel,new FArgumentsImpl(this,sel,true)));
}catch(Exception e){
e.printStackTrace(System.err);
}*/
try{
Trader3 proto=(Trader3)traderList.get(0);
Selector sel=new Selector(proto.getClass(),"tradeprocess",false);
actionForEach=modelActions.createFActionForEachHomogeneous$call
(traderList,new FCallImpl(this,proto,sel,new FArgumentsImpl(this,sel,true)));
}catch(Exception e){
e.printStackTrace(System.err);
}
syncUpdateOrder ();
//若需要更新边际收益,应该再加上计算边际收益的try语句
/*try{
Trader3 proto=(Trader3)traderList.get(0);
Selector sel=new Selector(proto.getClass(),"updateSlope",false);
actionForEach=modelActions.createFActionForEachHomogeneous$call
(traderList,new FCallImpl(this,proto,sel,new FArgumentsImpl(this,sel,true)));
}catch(Exception e){
e.printStackTrace(System.err);
}*/
// syncUpdateOrder ();
/*try{
modelActions.createActionTo$message
(this,new Selector(getClass(),"updateSlope",false));
}catch(Exception e){
System.err.println("Exception updateSlope"+e.getMessage());
} */
/*try{
modelActions.createActionTo$message
(this,new Selector(getClass(),"calculateExpectedPrice",false));
}catch(Exception e){
System.err.println("Exception getExpected"+e.getMessage());
} */
modelSchedule=new ScheduleImpl(getZone(),1);
modelSchedule.at$createAction(0,modelActions);
return this;
}
public Activity activateIn(Swarm swarmContext){
super.activateIn(swarmContext);
modelSchedule.activateIn(this);
return getActivity();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -