Commit bf9feaa7 authored by Julian Zobel's avatar Julian Zobel
Browse files

Moved the attraction point assignment in the group forming completely to the...

Moved the attraction point assignment in the group forming completely to the attraction point assignment strategy.
parent 487c457b
......@@ -187,11 +187,13 @@ public abstract class AbstractGroupForming implements IGroupFormingBehavior {
// Guarantee that the new destination of a group is not equal to the current AttractionPoint where the group is created
// This would be the case if the destination of the group leader is the current position of the group.
AttractionPoint leaderTarget = group.getLeader().getCurrentTargetAttractionPoint();
AttractionPoint nextDestination = null;
do {
nextDestination = getRandomAttractionPoint();
} while(nextDestination == leaderTarget);
movementModel.getAttractionAssignmentStrategy().addComponent(group.getLeader());
AttractionPoint nextDestination = movementModel.getAttractionAssignmentStrategy().getAssignment(group.getLeader());
System.out.println(leaderTarget + " vs. " + nextDestination);
// Add Offset to destination
PositionVector destination = movementModel.addGaussianOffsetToPosition(new PositionVector(nextDestination),
nextDestination.getRadius() / 3);
......@@ -209,18 +211,6 @@ public abstract class AbstractGroupForming implements IGroupFormingBehavior {
groupCon.addGroup(group);
}
/**
* Returns a random {@link AttractionPoint} out of the set of all AttractionPoints
*
* @return AttractionPoint
*
* @author Marcel Verst
*/
protected AttractionPoint getRandomAttractionPoint() {
int item = rand.nextInt(IAttractionGenerator.attractionPoints.size());
return IAttractionGenerator.attractionPoints.get(item);
}
/**
* Removes a group.
*
......
......@@ -27,6 +27,7 @@ import java.util.Set;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.api.topology.movement.local.LocalMovementStrategy;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionGenerator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.SocialMovementGroup;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.transition.IAttractionAssigmentStrategy;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
......@@ -96,7 +97,7 @@ public class DefaultGroupForming extends AbstractGroupForming {
// Get attractionPoint with highest amount of hosts
hostCounter.updateHostCount();
AttractionPoint apCandidate = getRandomAttractionPoint();
AttractionPoint apCandidate = IAttractionGenerator.attractionPoints.get(rand.nextInt(IAttractionGenerator.attractionPoints.size()));
if(apCandidate == null) {
break;
}
......
......@@ -104,23 +104,45 @@ public abstract class AbstractAttractionBasedAssignmentStrategy implements IAttr
protected long getPauseTime(AttractionPoint attractionPoint) {
return getRandomUniformDistributionPauseTime(attractionPoint);
}
protected AttractionPoint getNewAttractionPointAssignment(SimLocationActuator component) {
/**
* Get a new assignment for this component, that is not the last assignment
*
* @param host
* @return
*/
protected AttractionPoint getNewAttractionPointAssignment(SimLocationActuator host) {
double score = rnd.nextDouble();
List<AttractionPoint> candidates = new LinkedList<>();
for (AttractionPoint ap : IAttractionGenerator.attractionPoints) {
// skip null values (should never occur!)
if(ap == null) {
continue;
}
if (ap.getWeight() >= score) {
if(lastAssignments.get(component) == null || !ap.equals(lastAssignments.get(component))) {
candidates.add(ap);
}
// fast skip forward if the attraction point would not be considered due to its score
if (ap.getWeight() > score) {
continue;
}
// skip attraction points that are currently assigned to the host
if(assignments.containsKey(host) && ap.equals(assignments.get(host))) {
continue;
}
// add the attraction point to the list of candidates, if no attraction point was
// previously visited, or if currently no attraction point is assigned and the last
// assignment was not this attraction point (this prevents that, if the current attraction
// point was already set to the last assignment, the last visited attraction point is also considered)
if(lastAssignments.get(host) == null ||
(!assignments.containsKey(host) && !ap.equals(lastAssignments.get(host)))) {
candidates.add(ap);
}
}
// fallback: if no attraction point is considered, randomly select one
if (candidates.isEmpty()) {
candidates.addAll(IAttractionGenerator.attractionPoints);
}
......
......@@ -42,7 +42,7 @@ public interface IAttractionAssigmentStrategy {
* @param comp
* @return
*/
public AttractionPoint getAssignment(SimLocationActuator comp);
public AttractionPoint getAssignment(SimLocationActuator host);
public void addAttractionAssignmentListener(AttractionAssignmentListener listener);
......@@ -53,7 +53,7 @@ public interface IAttractionAssigmentStrategy {
*
* @param ms
*/
public void addComponent(SimLocationActuator ms);
public void addComponent(SimLocationActuator host);
/**
* Notify the TransitionStrategy, that the component has reached an
......@@ -61,7 +61,7 @@ public interface IAttractionAssigmentStrategy {
*
* @param ms
*/
public void reachedAttractionPoint(SimLocationActuator ms, AttractionPoint attractionPoint);
public void reachedAttractionPoint(SimLocationActuator host, AttractionPoint attractionPoint);
/**
* Updates the target attraction point of a component
......@@ -80,10 +80,11 @@ public interface IAttractionAssigmentStrategy {
* @param component
* @param newAssignment
*/
public void updatedAttractionAssignment(SimLocationActuator component,
public void updatedAttractionAssignment(SimLocationActuator host,
AttractionPoint newAssignment);
}
public boolean hostInAttractionPointArea(SimLocationActuator host);
}
......@@ -63,7 +63,7 @@ public class InAreaRoamingTransitionStrategy extends AbstractAttractionBasedAssi
@Override
public void addComponent(SimLocationActuator comp) {
this.roamingStates.put(comp, roamingTransitionState.TRANSITION);
this.roamingStates.put(comp, roamingTransitionState.TRANSITION);
AttractionPoint nextAP = getNewAttractionPointAssignment(comp);
updateTargetAttractionPoint(comp, nextAP);
}
......
package de.tud.kom.p2psim.impl.topology.movement.modularosm.transition;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionGenerator;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
import java.util.*;
......@@ -14,7 +13,7 @@ import java.util.*;
*
* @author Clemens Krug
*/
public class ManualAssignmentStrategy implements IAttractionAssigmentStrategy
public class ManualAssignmentStrategy extends AbstractAttractionBasedAssignmentStrategy
{
private LinkedHashSet<AttractionPoint> aPoints = new LinkedHashSet<>();
......@@ -22,33 +21,10 @@ public class ManualAssignmentStrategy implements IAttractionAssigmentStrategy
private List<AttractionAssignmentListener> listeners = new LinkedList<>();
@Override
public AttractionPoint getAssignment(SimLocationActuator comp)
{
return assignments.get(comp);
}
@Override
public void addAttractionAssignmentListener(AttractionAssignmentListener listener)
{
listeners.add(listener);
}
@Override
public void removeAttractionAssignmentListener(AttractionAssignmentListener listener)
{
listeners.remove(listener);
}
public void setAttractionPoints(Collection<AttractionPoint> attractionPoints) {
aPoints.addAll(attractionPoints);
}
public Set<AttractionPoint> getAllAttractionPoints() {
return aPoints;
}
@Override
public void addComponent(SimLocationActuator ms) {
if(!assignments.containsKey(ms))
......@@ -69,15 +45,5 @@ public class ManualAssignmentStrategy implements IAttractionAssigmentStrategy
assignments.put(comp, attractionPoint);
listeners.forEach(listener -> listener.updatedAttractionAssignment(comp, attractionPoint));
}
@Override
public boolean hostInAttractionPointArea(SimLocationActuator host) {
for(AttractionPoint ap : IAttractionGenerator.attractionPoints) {
if(ap.distanceTo(host.getRealPosition()) <= ap.getRadius()) {
return true;
}
}
return false;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment