PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/RepastMATer/src/mater/agents/Patch.java

http://mater.googlecode.com/
Java | 467 lines | 289 code | 68 blank | 110 comment | 55 complexity | 77bb0b6cfb09de414b15663cadbb0ead MD5 | raw file
  1. /*$$
  2. * Copyright (c) 2007, Centre of Informatics and Systems of the University of Coimbra
  3. * All rights reserved.
  4. *
  5. * Rui Lopes
  6. *$$*/
  7. package mater.agents;
  8. import mater.MaterModel;
  9. import mater.data.Definitions;
  10. import mater.space.Diffuse2D;
  11. import mater.space.MaterKeys;
  12. import uchicago.src.sim.gui.Drawable;
  13. import uchicago.src.sim.gui.SimGraphics;
  14. import java.awt.*;
  15. import java.util.List;
  16. public abstract class Patch implements Comparable, Drawable {
  17. private static final double MAX_ROADUSE = Diffuse2D.MAX;
  18. private int x = 0,
  19. y = 0;
  20. private double elevation = 0,
  21. baseCapacity = 0,
  22. agentCapacity = 0,
  23. hostingLimit = 0,
  24. roadUse = 0,
  25. potential=0;
  26. private boolean water,
  27. frontier = false,
  28. blocked = false,
  29. inhabited = false;
  30. //TODO:isto nao deve ser aqui!!!
  31. //public static final double MAX_E = 610;
  32. protected MaterModel model;
  33. private City city = null;
  34. private int centerDistance = 0;
  35. //private double roadUseDiffusion = 0;
  36. //to be used by the path finder
  37. //private Patch parent = null;
  38. private Diffuse2D roadUseSpace;
  39. public Patch( int x, int y, MaterModel model ){
  40. this.x = x;
  41. this.y = y;
  42. if( model != null ){
  43. //this.settleRate = model.getSettleRate();
  44. this.model = model;
  45. roadUseSpace = model.getRoadUseSpace();
  46. }
  47. }
  48. /*public void setAgentNum(){
  49. this.agentNum = model.getAgentSpace().getObjectsAt( x, y).size();
  50. }
  51. */
  52. private double calcAgentCapacity(boolean isInhabited) {
  53. double e = 0;
  54. /*if( isInhabited )
  55. e = (1 - this.elevation / MAX_E) * (model.getMaxPopDensity() - model.getMinPopDensity())
  56. + model.getMinPopDensity();
  57. else*/
  58. //TODO: commented to experiment
  59. //e = (1 - this.elevation / MAX_E) * (model.getOverallMaxPopDensity() - model.getOverallMinPopDensity())
  60. //+ model.getOverallMinPopDensity();
  61. //
  62. //(OverallMaxPopDensity?-OverallMinPopDensity?)/max(altitude)k-(min(altitude)k) x
  63. //((max(altitude)-patchAltitude)k) + OverallMinPopDensity?
  64. int expoente = 9;
  65. double exp_max_elevation = Math.pow( MaterModel._max_elevation , expoente );
  66. double exp_elevation = Math.pow( this.elevation , expoente );
  67. e =
  68. (( model.getOverallMaxPopDensity() - model.getOverallMinPopDensity() ) / exp_max_elevation ) *
  69. ( Math.pow( MaterModel._max_elevation - elevation, expoente) ) + model.getOverallMinPopDensity();
  70. //e = ( 1 - this.elevation / model._max_elevation ) * (model.getOverallMaxPopDensity() - model.getOverallMinPopDensity())
  71. //+ model.getOverallMinPopDensity();
  72. //System.out.print("WME = "+ world.getMeanElevation() + " ; "+ "elevation = " + e +" ; ");
  73. //return this.baseCapacity + ( this.baseCapacity + (world.getMeanElevation() / e ) );
  74. //return this.baseCapacity + ( this.baseCapacity * (world.getMeanElevation() / e ) );
  75. //System.out.print("ADD = "+ new Double((610 / e) )+" ; ");
  76. //MaterModel.pError("model.getMaxPopDensity = "+ model.getMaxPopDensity() + " model.getMinPopDensity = " + model.getMinPopDensity() + " model.getOverallMaxPopDensity = "+ model.getOverallMaxPopDensity() + " model.getOverallMinPopDensity = "+ model.getOverallMinPopDensity());
  77. //MaterModel.pError("**********************");
  78. //MaterModel.pError("elevation = "+ this.elevation + "elevation / MAX_E = " + this.elevation / MAX_E);
  79. //MaterModel.pError("RESULT: "+ e);
  80. /*if( e < 0 )
  81. MaterModel.pError("BUG: valor de capacidade de suporte de uma c?Šlula inferior a 0!!! elevation is "+this.elevation+ " and isWater "+this.water);*/
  82. return e;
  83. }
  84. private double calcHostingLimit() {
  85. double e = 0;
  86. e = (1 - this.elevation / model._max_elevation ) * (model.getMaxPopDensity() - model.getMinPopDensity())
  87. + model.getMinPopDensity();
  88. /*else
  89. e = (1 - this.elevation / MAX_E) * (model.getOverallMaxPopDensity() - model.getOverallMinPopDensity())
  90. + model.getOverallMinPopDensity();*/
  91. //System.out.print("WME = "+ world.getMeanElevation() + " ; "+ "elevation = " + e +" ; ");
  92. //return this.baseCapacity + ( this.baseCapacity + (world.getMeanElevation() / e ) );
  93. //return this.baseCapacity + ( this.baseCapacity * (world.getMeanElevation() / e ) );
  94. //System.out.print("ADD = "+ new Double((610 / e) )+" ; ");
  95. //MaterModel.pError("model.getMaxPopDensity = "+ model.getMaxPopDensity() + " model.getMinPopDensity = " + model.getMinPopDensity() + " model.getOverallMaxPopDensity = "+ model.getOverallMaxPopDensity() + " model.getOverallMinPopDensity = "+ model.getOverallMinPopDensity());
  96. //MaterModel.pError("**********************");
  97. //MaterModel.pError("elevation = "+ this.elevation + "elevation / MAX_E = " + this.elevation / MAX_E);
  98. //MaterModel.pError("RESULT: "+ e);
  99. /*if( e < 0 )
  100. MaterModel.pError("BUG: valor de capacidade de suporte de uma c?Šlula inferior a 0!!! elevation is "+this.elevation+ " and isWater "+this.water);*/
  101. return e;
  102. }
  103. public double getAgentCapacity() {
  104. return agentCapacity;
  105. }
  106. public void setAgentCapacity() {
  107. if( this.water || this.elevation == 1000000)
  108. agentCapacity = 0;
  109. else{
  110. agentCapacity = this.calcAgentCapacity( isInhabited() );
  111. //MaterModel.pError("Agent Capacity: "+agentCapacity);
  112. }
  113. baseCapacity = agentCapacity;
  114. }
  115. public void updateAgentCapacity(){
  116. double temp = agentCapacity;
  117. agentCapacity = baseCapacity * (1 + (roadUseSpace.getValueAt(x, y) / Diffuse2D.MAX) );
  118. if( city != null )
  119. city.updateAgentCapacity( agentCapacity - temp );
  120. }
  121. public double getElevation() {
  122. return elevation;
  123. }
  124. public void setElevation(int e) {
  125. this.elevation = e;
  126. }
  127. public boolean isWater() {
  128. return water;
  129. }
  130. public void setWater(boolean is_water) {
  131. this.water = is_water;
  132. }
  133. /*
  134. public double getSettleRate() {
  135. return settleRate;
  136. }
  137. public void setSettleRate(double settleRate) {
  138. this.settleRate = settleRate;
  139. }
  140. */
  141. public int getX() {
  142. return x;
  143. }
  144. public void setX(int x) {
  145. this.x = x;
  146. }
  147. public int getY() {
  148. return y;
  149. }
  150. public void setY(int y) {
  151. this.y = y;
  152. }
  153. public City getCity() {
  154. return city;
  155. }
  156. public void setCity(City city) {
  157. this.city = city;
  158. if( city == null && isInhabited() )
  159. throw new NullPointerException();
  160. //this.roadUse = 0;
  161. }
  162. public boolean hasAgents(){
  163. List o = model.getAgentSpace().getObjectsAt(x,y);
  164. return !o.isEmpty() ;
  165. }
  166. public void addAgent( MaterAgent a ){
  167. //fazer outras coisas e tb se for patch de cidade
  168. //if( !a.isOnRoad() && !this.isInhabited() )
  169. //System.out.println("agent not on road added to uninhabited patch");
  170. model.getAgentSpace().putObjectAt( x, y, a );
  171. //agentNum++;
  172. }
  173. public void removeAgent( MaterAgent a ){
  174. model.getAgentSpace().removeObjectAt( x, y, a );
  175. }
  176. public int getAgentNum(){
  177. return model.getAgentSpace().getObjectsAt(x,y).size();
  178. }
  179. public double getCost( Patch parent, boolean ignoreRoadUse ) {
  180. double d = 0;
  181. if( parent != null )
  182. if( parent.isWater() )
  183. d = ( this.isWater()? model.getCostWaterToWater() : model.getCostWaterToLandVV() );
  184. else{
  185. if( this.isWater() )
  186. d = model.getCostWaterToLandVV();
  187. else{
  188. d = parent.getElevation() - this.getElevation();
  189. if( d < 0 )
  190. //FIXME parameterizar o incremento de subir?
  191. d *= (-2);
  192. }
  193. }
  194. if( this.isInhabited() )
  195. d += model.getCostInCity();
  196. d += model.getBaseCost();
  197. if( parent !=null && parent.getX() != x && parent.getY() != y )
  198. d = d * Math.sqrt( 2 );
  199. //if( !ignoreRoadUse )
  200. d -= this.roadUse;
  201. return d >= 0? d : 0;
  202. //return d;
  203. }
  204. public Point getXY() {
  205. return new Point( this.x, this.y );
  206. }
  207. public String getID(){
  208. return getX()+":"+getY();
  209. }
  210. public int compareTo( Object arg0 ) {
  211. //esta invertido para usar c priorityQueue
  212. Patch p = ( Patch )arg0;
  213. if( p.getAgentCapacity() > getAgentCapacity() )
  214. return -1;
  215. else if( p.getAgentCapacity() < getAgentCapacity() )
  216. return 1;
  217. else
  218. return 0;
  219. }
  220. /*
  221. * This method was implemented because the PriorityQueue does not remove the instances well.
  222. * However, if the references to the instances are used it should remove correctly. why doesn't it?
  223. */
  224. @Override
  225. public boolean equals(Object o ){
  226. Patch op = (Patch)o;
  227. return op.getX() == this.getX() && op.getY() == this.getY();
  228. }
  229. public double getOccupation() {
  230. if( this.isInhabited() )
  231. return this.getAgentNum() / this.agentCapacity;
  232. else
  233. return 1;
  234. }
  235. public void draw(SimGraphics g) {
  236. //System.out.println("Mode is "+ model.getMode() );
  237. //g.getGraphics().setComposite(null);
  238. if( this.roadUse > 0 && model._displayRoads && !isInhabited() ){
  239. float ratio = (float)(roadUse / MAX_ROADUSE);
  240. //g.getGraphics().setComposite(
  241. // AlphaComposite.getInstance( AlphaComposite.SRC_OVER, ratio ));
  242. float[] compArray = Definitions._ROAD_COLOR.getColorComponents(null);
  243. //float[] hsbvals = Color.RGBtoHSB((int)compArray[0], (int)compArray[1], (int)compArray[2], null);
  244. //g.drawFastRoundRect( Definitions._ROAD_COLOR );
  245. g.drawFastRoundRect( new Color( compArray[0]*ratio ,compArray[1]*ratio,compArray[2]*ratio ) );
  246. }
  247. else if( model.getMode() == MaterKeys._RUSE_DIFF ){
  248. double d =0;
  249. synchronized( roadUseSpace ){
  250. d = (Double)roadUseSpace.getMatrix().get(x, y);
  251. }
  252. if( d > 0 ) {
  253. try {
  254. g.drawFastRect(new Color((float) (d / model
  255. .getRoadUseDiffMax()), 0, 0));
  256. } catch (IllegalArgumentException e) {
  257. ;//FIXME nao devia ser resolvido assim mas enfim...
  258. }
  259. }
  260. }
  261. else if( city != null){
  262. switch( model.getMode() ){
  263. case MaterKeys._ORIGINAL:
  264. if( !isInhabited())
  265. if( !this.getCity().isGrowing() )
  266. g.drawFastRoundRect( Definitions._CITY_COLOR );
  267. else
  268. g.drawFastRoundRect( Definitions._CITY_GROW_COLOR );
  269. else if(this.isInhabited())
  270. g.drawFastRoundRect( Color.black );
  271. break;
  272. case MaterKeys._LEAVE_RATE:
  273. case MaterKeys._RESIDENT_POP:
  274. case MaterKeys._AGE:
  275. case MaterKeys._FLOWS:
  276. case MaterKeys._POP_DENSITY:
  277. g.drawFastRect( city.getColor() );
  278. break;
  279. case MaterKeys._CRAZY:
  280. if(this.isInhabited())
  281. g.drawFastRoundRect( Color.black );
  282. else
  283. g.drawFastRect( city.getCrazyColor() );
  284. break;
  285. case MaterKeys._BLOCKED:
  286. g.drawFastRect( city.isBlocked()? Color.RED : Color.BLACK );
  287. break;
  288. case MaterKeys._CENTERS:
  289. if( this.isInhabited() )
  290. g.drawFastRect( Color.RED );
  291. break;
  292. }
  293. if( model._displayFrontiers && isFrontier() ){
  294. if( this.isBlocked() )
  295. g.drawFastRect(Color.BLACK);
  296. else
  297. g.drawFastRect(Color.WHITE);
  298. }
  299. }
  300. }
  301. public void decrementRoadUse(int i) {
  302. if( (roadUse -= i) < 0)
  303. this.roadUse = 0;
  304. roadUseSpace.getMatrix().put(x,y,
  305. /*(Double)roadUseSpace.getMatrix().get(x,y) +*/ roadUse);
  306. }
  307. public void incrementRoadUse() {
  308. if( (this.roadUse += 1) > MAX_ROADUSE )
  309. roadUse = MAX_ROADUSE;
  310. roadUseSpace.getMatrix().put(x,y,
  311. /*(Double)roadUseSpace.getMatrix().get(x,y) +*/ roadUse);
  312. }
  313. /*
  314. public void preStep(){
  315. this.agentNum = model.getAgentSpace().getObjectsAt( x, y).size();
  316. //if(!MaterModel.quiet && this.city != null)
  317. //MaterModel.pError("Current has capacity: "+getAgentCapacity()+" and stored "+getAgentNum());
  318. }
  319. */
  320. public void postStep( double rd ){
  321. if( roadUse > 0 )
  322. if( (roadUse -= rd) < 0 )
  323. roadUse = 0;
  324. }
  325. public void setFrontier(boolean f ) {
  326. this.frontier = f;
  327. }
  328. public boolean isFrontier() {
  329. return this.frontier;
  330. }
  331. public double getRoadUse() {
  332. return roadUse;
  333. }
  334. /*
  335. public void setRoadUse(double roadUse) {
  336. this.roadUse = roadUse;
  337. }
  338. */
  339. public boolean isInhabited() {
  340. return inhabited;
  341. }
  342. public void setInhabited(boolean inhabited) {
  343. this.inhabited = inhabited;
  344. //setAgentCapacity();
  345. }
  346. public int getSettledAgentNum() {
  347. List<MaterAgent> alist = model.getAgentSpace().getObjectsAt(x,y);
  348. int i = 0;
  349. for( MaterAgent a: alist )
  350. if( !a.isOnRoad() && !a.isGuest() )
  351. i++;
  352. return i;
  353. }
  354. public double getHostingLimit() {
  355. return hostingLimit;
  356. }
  357. public void setHostingLimit() {
  358. this.hostingLimit = calcHostingLimit();
  359. }
  360. public double getCenterDistance() {
  361. return centerDistance;
  362. }
  363. public void setCenterDistance(Patch parent, Point center) {
  364. double cost = 1;
  365. double parent_cost =
  366. ( parent == null? 0: parent.getCenterDistance());
  367. //if( parent.x != x && parent.y != y )
  368. //cost *= Math.sqrt( 2 );
  369. centerDistance = (int)(parent_cost + cost);//this.getCost(parent, true);
  370. //double realDist = Math.sqrt( Math.pow( x - center.x, 2 )+ Math.pow( y - center.y, 2) );
  371. //double corFactor = realDist / vNeumannCost;
  372. //(int)(vNeumannCost * corFactor);
  373. /*
  374. if( parent !=null && parent.getX() != x && parent.getY() != y )
  375. centerDistance = (int)(centerDistance * Math.sqrt( 2 ));*/
  376. }
  377. public boolean isBlocked() {
  378. return blocked;
  379. }
  380. public void setBlocked(boolean blocked) {
  381. this.blocked = blocked;
  382. }
  383. public double getPotential() {
  384. return potential;
  385. }
  386. public void setPotential(double potential) {
  387. this.potential = potential;
  388. }
  389. /*
  390. public double getRoadUseDiffusion() {
  391. return roadUseDiffusion;
  392. }
  393. public void setRoadUseDiffusion(double roadUseDiffusion) {
  394. this.roadUseDiffusion = roadUseDiffusion;
  395. }
  396. */
  397. }