/RepastMATer/src/mater/agents/Patch.java
Java | 467 lines | 289 code | 68 blank | 110 comment | 55 complexity | 77bb0b6cfb09de414b15663cadbb0ead MD5 | raw file
- /*$$
- * Copyright (c) 2007, Centre of Informatics and Systems of the University of Coimbra
- * All rights reserved.
- *
- * Rui Lopes
- *$$*/
- package mater.agents;
- import mater.MaterModel;
- import mater.data.Definitions;
- import mater.space.Diffuse2D;
- import mater.space.MaterKeys;
- import uchicago.src.sim.gui.Drawable;
- import uchicago.src.sim.gui.SimGraphics;
- import java.awt.*;
- import java.util.List;
- public abstract class Patch implements Comparable, Drawable {
- private static final double MAX_ROADUSE = Diffuse2D.MAX;
- private int x = 0,
- y = 0;
- private double elevation = 0,
- baseCapacity = 0,
- agentCapacity = 0,
- hostingLimit = 0,
- roadUse = 0,
- potential=0;
- private boolean water,
- frontier = false,
- blocked = false,
- inhabited = false;
- //TODO:isto nao deve ser aqui!!!
- //public static final double MAX_E = 610;
- protected MaterModel model;
- private City city = null;
- private int centerDistance = 0;
- //private double roadUseDiffusion = 0;
- //to be used by the path finder
- //private Patch parent = null;
- private Diffuse2D roadUseSpace;
- public Patch( int x, int y, MaterModel model ){
- this.x = x;
- this.y = y;
- if( model != null ){
- //this.settleRate = model.getSettleRate();
- this.model = model;
- roadUseSpace = model.getRoadUseSpace();
- }
- }
- /*public void setAgentNum(){
- this.agentNum = model.getAgentSpace().getObjectsAt( x, y).size();
- }
- */
- private double calcAgentCapacity(boolean isInhabited) {
- double e = 0;
- /*if( isInhabited )
- e = (1 - this.elevation / MAX_E) * (model.getMaxPopDensity() - model.getMinPopDensity())
- + model.getMinPopDensity();
- else*/
- //TODO: commented to experiment
- //e = (1 - this.elevation / MAX_E) * (model.getOverallMaxPopDensity() - model.getOverallMinPopDensity())
- //+ model.getOverallMinPopDensity();
- //
- //(OverallMaxPopDensity?-OverallMinPopDensity?)/max(altitude)k-(min(altitude)k) x
- //((max(altitude)-patchAltitude)k) + OverallMinPopDensity?
- int expoente = 9;
- double exp_max_elevation = Math.pow( MaterModel._max_elevation , expoente );
- double exp_elevation = Math.pow( this.elevation , expoente );
- e =
- (( model.getOverallMaxPopDensity() - model.getOverallMinPopDensity() ) / exp_max_elevation ) *
- ( Math.pow( MaterModel._max_elevation - elevation, expoente) ) + model.getOverallMinPopDensity();
- //e = ( 1 - this.elevation / model._max_elevation ) * (model.getOverallMaxPopDensity() - model.getOverallMinPopDensity())
- //+ model.getOverallMinPopDensity();
- //System.out.print("WME = "+ world.getMeanElevation() + " ; "+ "elevation = " + e +" ; ");
- //return this.baseCapacity + ( this.baseCapacity + (world.getMeanElevation() / e ) );
- //return this.baseCapacity + ( this.baseCapacity * (world.getMeanElevation() / e ) );
- //System.out.print("ADD = "+ new Double((610 / e) )+" ; ");
- //MaterModel.pError("model.getMaxPopDensity = "+ model.getMaxPopDensity() + " model.getMinPopDensity = " + model.getMinPopDensity() + " model.getOverallMaxPopDensity = "+ model.getOverallMaxPopDensity() + " model.getOverallMinPopDensity = "+ model.getOverallMinPopDensity());
- //MaterModel.pError("**********************");
- //MaterModel.pError("elevation = "+ this.elevation + "elevation / MAX_E = " + this.elevation / MAX_E);
- //MaterModel.pError("RESULT: "+ e);
- /*if( e < 0 )
- MaterModel.pError("BUG: valor de capacidade de suporte de uma c?lula inferior a 0!!! elevation is "+this.elevation+ " and isWater "+this.water);*/
- return e;
- }
- private double calcHostingLimit() {
- double e = 0;
- e = (1 - this.elevation / model._max_elevation ) * (model.getMaxPopDensity() - model.getMinPopDensity())
- + model.getMinPopDensity();
- /*else
- e = (1 - this.elevation / MAX_E) * (model.getOverallMaxPopDensity() - model.getOverallMinPopDensity())
- + model.getOverallMinPopDensity();*/
- //System.out.print("WME = "+ world.getMeanElevation() + " ; "+ "elevation = " + e +" ; ");
- //return this.baseCapacity + ( this.baseCapacity + (world.getMeanElevation() / e ) );
- //return this.baseCapacity + ( this.baseCapacity * (world.getMeanElevation() / e ) );
- //System.out.print("ADD = "+ new Double((610 / e) )+" ; ");
- //MaterModel.pError("model.getMaxPopDensity = "+ model.getMaxPopDensity() + " model.getMinPopDensity = " + model.getMinPopDensity() + " model.getOverallMaxPopDensity = "+ model.getOverallMaxPopDensity() + " model.getOverallMinPopDensity = "+ model.getOverallMinPopDensity());
- //MaterModel.pError("**********************");
- //MaterModel.pError("elevation = "+ this.elevation + "elevation / MAX_E = " + this.elevation / MAX_E);
- //MaterModel.pError("RESULT: "+ e);
- /*if( e < 0 )
- MaterModel.pError("BUG: valor de capacidade de suporte de uma c?lula inferior a 0!!! elevation is "+this.elevation+ " and isWater "+this.water);*/
- return e;
- }
- public double getAgentCapacity() {
- return agentCapacity;
- }
- public void setAgentCapacity() {
- if( this.water || this.elevation == 1000000)
- agentCapacity = 0;
- else{
- agentCapacity = this.calcAgentCapacity( isInhabited() );
- //MaterModel.pError("Agent Capacity: "+agentCapacity);
- }
- baseCapacity = agentCapacity;
- }
-
- public void updateAgentCapacity(){
- double temp = agentCapacity;
- agentCapacity = baseCapacity * (1 + (roadUseSpace.getValueAt(x, y) / Diffuse2D.MAX) );
- if( city != null )
- city.updateAgentCapacity( agentCapacity - temp );
- }
- public double getElevation() {
- return elevation;
- }
- public void setElevation(int e) {
- this.elevation = e;
- }
- public boolean isWater() {
- return water;
- }
- public void setWater(boolean is_water) {
- this.water = is_water;
- }
- /*
- public double getSettleRate() {
- return settleRate;
- }
- public void setSettleRate(double settleRate) {
- this.settleRate = settleRate;
- }
- */
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public City getCity() {
- return city;
- }
- public void setCity(City city) {
- this.city = city;
- if( city == null && isInhabited() )
- throw new NullPointerException();
- //this.roadUse = 0;
- }
- public boolean hasAgents(){
- List o = model.getAgentSpace().getObjectsAt(x,y);
- return !o.isEmpty() ;
- }
- public void addAgent( MaterAgent a ){
- //fazer outras coisas e tb se for patch de cidade
- //if( !a.isOnRoad() && !this.isInhabited() )
- //System.out.println("agent not on road added to uninhabited patch");
- model.getAgentSpace().putObjectAt( x, y, a );
- //agentNum++;
- }
- public void removeAgent( MaterAgent a ){
- model.getAgentSpace().removeObjectAt( x, y, a );
- }
- public int getAgentNum(){
- return model.getAgentSpace().getObjectsAt(x,y).size();
- }
- public double getCost( Patch parent, boolean ignoreRoadUse ) {
- double d = 0;
- if( parent != null )
- if( parent.isWater() )
- d = ( this.isWater()? model.getCostWaterToWater() : model.getCostWaterToLandVV() );
- else{
- if( this.isWater() )
- d = model.getCostWaterToLandVV();
- else{
- d = parent.getElevation() - this.getElevation();
- if( d < 0 )
- //FIXME parameterizar o incremento de subir?
- d *= (-2);
- }
- }
- if( this.isInhabited() )
- d += model.getCostInCity();
- d += model.getBaseCost();
- if( parent !=null && parent.getX() != x && parent.getY() != y )
- d = d * Math.sqrt( 2 );
- //if( !ignoreRoadUse )
- d -= this.roadUse;
- return d >= 0? d : 0;
- //return d;
- }
- public Point getXY() {
- return new Point( this.x, this.y );
- }
- public String getID(){
- return getX()+":"+getY();
- }
- public int compareTo( Object arg0 ) {
- //esta invertido para usar c priorityQueue
- Patch p = ( Patch )arg0;
- if( p.getAgentCapacity() > getAgentCapacity() )
- return -1;
- else if( p.getAgentCapacity() < getAgentCapacity() )
- return 1;
- else
- return 0;
- }
-
- /*
- * This method was implemented because the PriorityQueue does not remove the instances well.
- * However, if the references to the instances are used it should remove correctly. why doesn't it?
- */
- @Override
- public boolean equals(Object o ){
- Patch op = (Patch)o;
- return op.getX() == this.getX() && op.getY() == this.getY();
- }
- public double getOccupation() {
- if( this.isInhabited() )
- return this.getAgentNum() / this.agentCapacity;
- else
- return 1;
- }
-
- public void draw(SimGraphics g) {
- //System.out.println("Mode is "+ model.getMode() );
- //g.getGraphics().setComposite(null);
- if( this.roadUse > 0 && model._displayRoads && !isInhabited() ){
- float ratio = (float)(roadUse / MAX_ROADUSE);
- //g.getGraphics().setComposite(
- // AlphaComposite.getInstance( AlphaComposite.SRC_OVER, ratio ));
- float[] compArray = Definitions._ROAD_COLOR.getColorComponents(null);
- //float[] hsbvals = Color.RGBtoHSB((int)compArray[0], (int)compArray[1], (int)compArray[2], null);
-
- //g.drawFastRoundRect( Definitions._ROAD_COLOR );
- g.drawFastRoundRect( new Color( compArray[0]*ratio ,compArray[1]*ratio,compArray[2]*ratio ) );
- }
- else if( model.getMode() == MaterKeys._RUSE_DIFF ){
- double d =0;
- synchronized( roadUseSpace ){
- d = (Double)roadUseSpace.getMatrix().get(x, y);
- }
-
- if( d > 0 ) {
- try {
- g.drawFastRect(new Color((float) (d / model
- .getRoadUseDiffMax()), 0, 0));
- } catch (IllegalArgumentException e) {
- ;//FIXME nao devia ser resolvido assim mas enfim...
- }
- }
- }
- else if( city != null){
- switch( model.getMode() ){
- case MaterKeys._ORIGINAL:
- if( !isInhabited())
- if( !this.getCity().isGrowing() )
- g.drawFastRoundRect( Definitions._CITY_COLOR );
- else
- g.drawFastRoundRect( Definitions._CITY_GROW_COLOR );
- else if(this.isInhabited())
- g.drawFastRoundRect( Color.black );
- break;
- case MaterKeys._LEAVE_RATE:
- case MaterKeys._RESIDENT_POP:
- case MaterKeys._AGE:
- case MaterKeys._FLOWS:
- case MaterKeys._POP_DENSITY:
- g.drawFastRect( city.getColor() );
- break;
- case MaterKeys._CRAZY:
- if(this.isInhabited())
- g.drawFastRoundRect( Color.black );
- else
- g.drawFastRect( city.getCrazyColor() );
- break;
- case MaterKeys._BLOCKED:
- g.drawFastRect( city.isBlocked()? Color.RED : Color.BLACK );
- break;
- case MaterKeys._CENTERS:
- if( this.isInhabited() )
- g.drawFastRect( Color.RED );
- break;
- }
- if( model._displayFrontiers && isFrontier() ){
- if( this.isBlocked() )
- g.drawFastRect(Color.BLACK);
- else
- g.drawFastRect(Color.WHITE);
- }
- }
- }
- public void decrementRoadUse(int i) {
- if( (roadUse -= i) < 0)
- this.roadUse = 0;
- roadUseSpace.getMatrix().put(x,y,
- /*(Double)roadUseSpace.getMatrix().get(x,y) +*/ roadUse);
-
- }
- public void incrementRoadUse() {
- if( (this.roadUse += 1) > MAX_ROADUSE )
- roadUse = MAX_ROADUSE;
- roadUseSpace.getMatrix().put(x,y,
- /*(Double)roadUseSpace.getMatrix().get(x,y) +*/ roadUse);
- }
- /*
- public void preStep(){
- this.agentNum = model.getAgentSpace().getObjectsAt( x, y).size();
- //if(!MaterModel.quiet && this.city != null)
- //MaterModel.pError("Current has capacity: "+getAgentCapacity()+" and stored "+getAgentNum());
- }
- */
- public void postStep( double rd ){
- if( roadUse > 0 )
- if( (roadUse -= rd) < 0 )
- roadUse = 0;
- }
- public void setFrontier(boolean f ) {
- this.frontier = f;
- }
- public boolean isFrontier() {
- return this.frontier;
- }
- public double getRoadUse() {
- return roadUse;
- }
- /*
- public void setRoadUse(double roadUse) {
- this.roadUse = roadUse;
- }
- */
- public boolean isInhabited() {
- return inhabited;
- }
- public void setInhabited(boolean inhabited) {
- this.inhabited = inhabited;
- //setAgentCapacity();
- }
- public int getSettledAgentNum() {
- List<MaterAgent> alist = model.getAgentSpace().getObjectsAt(x,y);
- int i = 0;
- for( MaterAgent a: alist )
- if( !a.isOnRoad() && !a.isGuest() )
- i++;
- return i;
- }
- public double getHostingLimit() {
- return hostingLimit;
- }
- public void setHostingLimit() {
- this.hostingLimit = calcHostingLimit();
- }
- public double getCenterDistance() {
- return centerDistance;
- }
- public void setCenterDistance(Patch parent, Point center) {
- double cost = 1;
- double parent_cost =
- ( parent == null? 0: parent.getCenterDistance());
- //if( parent.x != x && parent.y != y )
- //cost *= Math.sqrt( 2 );
-
- centerDistance = (int)(parent_cost + cost);//this.getCost(parent, true);
- //double realDist = Math.sqrt( Math.pow( x - center.x, 2 )+ Math.pow( y - center.y, 2) );
- //double corFactor = realDist / vNeumannCost;
- //(int)(vNeumannCost * corFactor);
- /*
- if( parent !=null && parent.getX() != x && parent.getY() != y )
- centerDistance = (int)(centerDistance * Math.sqrt( 2 ));*/
- }
- public boolean isBlocked() {
- return blocked;
- }
- public void setBlocked(boolean blocked) {
- this.blocked = blocked;
- }
- public double getPotential() {
- return potential;
- }
- public void setPotential(double potential) {
- this.potential = potential;
- }
- /*
- public double getRoadUseDiffusion() {
- return roadUseDiffusion;
- }
- public void setRoadUseDiffusion(double roadUseDiffusion) {
- this.roadUseDiffusion = roadUseDiffusion;
- }
- */
- }