Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package loadPoles;
import java.util.List;
import repast.simphony.context.Context;
import repast.simphony.engine.schedule.ScheduledMethod;
import repast.simphony.query.space.grid.GridCell;
import repast.simphony.query.space.grid.GridCellNgh;
import repast.simphony.random.RandomHelper;
import repast.simphony.space.graph.Network;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridPoint;
import repast.simphony.util.SimUtilities;
public class Asshole extends Human {
boolean needsCharging;
public Asshole(Context<Object> context) {
super(context);
this.carType = "a";
// TODO Auto-generated constructor stub
}
@ScheduledMethod(start = 1, interval = 1, priority = 2, shuffle = true)
public void parkCar() {
//do we even use a car?
if (!carUser)
return;
System.out.println("Parkingname: " + this.name);
//Find best empty spot
//Get location of home
Grid<Object> grid = (Grid<Object>) context.getProjection("dwellingsGrid");
Network<Object> livingin = (Network<Object>) context.getProjection("livingin");
Iterable<Object> home = livingin.getAdjacent(this);
GridPoint homeLocation = grid.getLocation(home.iterator().next());
//Now look for parking spots
Grid<Object> parkingSpots = (Grid<Object>) context.getProjection("parkingspacesGrid");
//get all cells around the home location
GridCellNgh<ParkingSpace> nghCreator = new GridCellNgh<ParkingSpace>(parkingSpots, homeLocation, ParkingSpace.class, 2, 2);
List<GridCell<ParkingSpace>> gridCells = nghCreator.getNeighborhood(true);
SimUtilities.shuffle(gridCells, RandomHelper.getUniform());
//Check the neighbourhood for the closest empty spot
// The spot has either no agent in it or an agent that isn't parked
double minDistance = Double.MAX_VALUE, parkDistance = 0;
GridCell<ParkingSpace> closestCell = null;
ParkingSpace closestSpot = null;
for (GridCell<ParkingSpace> cell : gridCells) {
//Iterable<Object> spotUsers = parkingSpots.getObjectsAt(cell.getPoint().getX(),cell.getPoint().getY());
//System.out.println("spotUsers: " + spotUsers + " size: "+ spotUsers.spliterator().estimateSize() + " somone parked: " + isSomeoneParked(parkingSpots, cell));
ParkingSpace spot = (ParkingSpace) parkingSpots.getObjectAt(cell.getPoint().getX(),cell.getPoint().getY());
System.out.println("this carType: " + this.carType + " and spot type: " + spot.getType());
//if( spotUser != null) System.out.println("Spot "+ cell.getPoint().getX() + "," + cell.getPoint().getY() + " is not null but first occupant is parked?: " + spotUser.getHasParked());
//if( spotUser == null || isSomeoneParked(parkingSpots, cell) == false) {
if (!spot.getOccupied() && this.carType == spot.getType()) {
parkDistance = parkingSpots.getDistance(homeLocation, cell.getPoint());
if (parkDistance < minDistance) {
minDistance = parkDistance;
if(parkDistance == 0.0) System.out.println("is someone parked: " + spot.getOccupied());
System.out.println("parkDistance: " + parkDistance);
closestCell = cell;
closestSpot = spot;
}
}
}
//If we have found a parking spot: park!
if(closestCell != null) {
System.out.println("I am moving to: " + closestCell.getPoint().getX() + "," + closestCell.getPoint().getY());
parkingSpots.moveTo(this, closestCell.getPoint().getX(),closestCell.getPoint().getY());
closestSpot.setOccupied(true);
this.hasParked = true;
if(closestSpot.getType() == "a" && this.happiness < 1)
this.happiness = (float) (this.happiness + 0.1 - (parkDistance * parkDistance * 0.01));
} else {
System.out.println("wasnt able to find a spot!!!!!!");
if (this.happiness > 0) this.happiness = (float) (this.happiness - 0.3);
}
}
}