Commit 4b8eaefe authored by Julian Zobel's avatar Julian Zobel
Browse files

Smart Merge Group Encounter: Groups will merge instead of wait, if they go the...

Smart Merge Group Encounter: Groups will merge instead of wait, if they go the same direction (same AP)
parent 3ac36ae8
......@@ -30,6 +30,7 @@ import de.tud.kom.p2psim.impl.topology.movement.modularosm.SocialGroupMovementMo
import de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.MovementGroupContainer;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.SocialMovementGroup;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tud.kom.p2psim.impl.util.Tuple;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.common.datastructures.Pair;
......@@ -302,4 +303,45 @@ public abstract class AbstractGroupEncounter implements IGroupEncounterBehavior
}
/**
* Assigns two groups the role to wait for a specified amount of time. After waiting for this time,
* the groups are released from the waiting role and can continue their trip.
*
* @param SocialMovementGroup The first group.
* @param SocialMovementGroup The second group.
*/
protected void wait(SocialMovementGroup group1, SocialMovementGroup group2, long timeToWait) {
// Do not apply waiting process if one of both groups is already waiting
if(groupCon.waitingGroups.containsKey(group1) ^ groupCon.waitingGroups.containsKey(group2)) {
return;
}
// If both groups are not waiting, declare them as waiting groups for a duration of timeToWait
if(!groupCon.waitingGroups.containsKey(group1) && !groupCon.waitingGroups.containsKey(group2)) {
groupCon.waitingGroups.put(group1, new Tuple<Long, Long>(timeToWait, Time.getCurrentTime()));
groupCon.waitingGroups.put(group2, new Tuple<Long, Long>(timeToWait, Time.getCurrentTime()));
}
// If both groups are already waiting
if(groupCon.waitingGroups.containsKey(group1) && groupCon.waitingGroups.containsKey(group2)) {
long timerGroup1 = groupCon.waitingGroups.get(group1).getA();
long timerGroup2 = groupCon.waitingGroups.get(group2).getA();
// Remove groups if they have waited long enough
if(timerGroup1 <= 0 && timerGroup2 <= 0) {
groupCon.updateMergeVars(group1, group2);
groupCon.waitingGroups.remove(group1);
groupCon.waitingGroups.remove(group2);
}
// Decrement timer of both groups if they have to wait more
else {
long deltaGroup1 = Time.getCurrentTime() - groupCon.waitingGroups.get(group1).getB();
groupCon.waitingGroups.put(group1, new Tuple<Long, Long>(timerGroup1 - deltaGroup1, Time.getCurrentTime()));
long deltaGroup2 = Time.getCurrentTime() - groupCon.waitingGroups.get(group2).getB();
groupCon.waitingGroups.put(group2, new Tuple<Long, Long>(timerGroup2 - deltaGroup2, Time.getCurrentTime()));
}
}
}
}
......@@ -22,8 +22,6 @@ package de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.groupencounte
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.SocialMovementGroup;
import de.tud.kom.p2psim.impl.util.Tuple;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
......@@ -51,51 +49,11 @@ public class Wait extends AbstractGroupEncounter{
@Override
public void handleGroupEncounter(SocialMovementGroup group1, SocialMovementGroup group2) {
if(rand.nextDouble() <= probabilityToWait) {
wait(group1, group2);
}
}
/**
* Assigns two groups the role to wait for a specified amount of time. After waiting for this time,
* the groups are released from the waiting role and can continue their trip.
*
* @param SocialMovementGroup The first group.
* @param SocialMovementGroup The second group.
*/
private void wait(SocialMovementGroup group1, SocialMovementGroup group2) {
// Do not apply waiting process if one of both groups is already waiting
if(groupCon.waitingGroups.containsKey(group1) ^ groupCon.waitingGroups.containsKey(group2)) {
return;
}
// If both groups are not waiting, declare them as waiting groups for a duration of timeToWait
if(!groupCon.waitingGroups.containsKey(group1) && !groupCon.waitingGroups.containsKey(group2)) {
groupCon.waitingGroups.put(group1, new Tuple<Long, Long>(timeToWait, Time.getCurrentTime()));
groupCon.waitingGroups.put(group2, new Tuple<Long, Long>(timeToWait, Time.getCurrentTime()));
}
// If both groups are already waiting
if(groupCon.waitingGroups.containsKey(group1) && groupCon.waitingGroups.containsKey(group2)) {
long timerGroup1 = groupCon.waitingGroups.get(group1).getA();
long timerGroup2 = groupCon.waitingGroups.get(group2).getA();
// Remove groups if they have waited long enough
if(timerGroup1 <= 0 && timerGroup2 <= 0) {
groupCon.updateMergeVars(group1, group2);
groupCon.waitingGroups.remove(group1);
groupCon.waitingGroups.remove(group2);
}
// Decrement timer of both groups if they have to wait more
else {
long deltaGroup1 = Time.getCurrentTime() - groupCon.waitingGroups.get(group1).getB();
groupCon.waitingGroups.put(group1, new Tuple<Long, Long>(timerGroup1 - deltaGroup1, Time.getCurrentTime()));
long deltaGroup2 = Time.getCurrentTime() - groupCon.waitingGroups.get(group2).getB();
groupCon.waitingGroups.put(group2, new Tuple<Long, Long>(timerGroup2 - deltaGroup2, Time.getCurrentTime()));
}
wait(group1, group2, timeToWait);
}
}
/*
* GETTERS AND SETTERS
*/
......
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.groupencounter;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.groups.SocialMovementGroup;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
/**
*
* @author Julian Zobel
* @version 1.0, 04.02.2020
*/
public class WaitSmartMerge extends AbstractGroupEncounter{
//--- VARIABLES TO BE DECLARED IN CONFIG ---
private double probabilityToWait;
private long timeToWait;
//------------------------------------------
@XMLConfigurableConstructor({"enableGroupEncounters","groupEncounterMeetingDistance","groupReencounterWaitTime"})
public WaitSmartMerge(boolean enableGroupEncounters,
double groupEncounterMeetingDistance,
long groupReencounterWaitTime) {
super(enableGroupEncounters, groupEncounterMeetingDistance,
groupReencounterWaitTime);
}
@Override
public void handleGroupEncounter(SocialMovementGroup group1, SocialMovementGroup group2) {
if(group1.getLeader().getCurrentTargetAttractionPoint() == group2.getLeader().getCurrentTargetAttractionPoint()) {
mergeGroupsFully(group1, group2);
}
else if(rand.nextDouble() <= probabilityToWait) {
wait(group1, group2, timeToWait);
}
}
/*
* GETTERS AND SETTERS
*/
public void setProbabilityToWait(double probabilityToWait) {
if (probabilityToWait < 0 || probabilityToWait > 1.0) {
throw new ConfigurationException(
"probabilityToWait should be between 0.0 and 1.0!");
}
this.probabilityToWait = probabilityToWait;
}
public void setTimeToWait(long timeToWait) {
if(timeToWait < 0) {
throw new ConfigurationException(
"timeToWait should be greater or equal than 0!");
}
this.timeToWait = timeToWait;
}
}
......@@ -218,17 +218,20 @@ public abstract class AbstractGroupForming implements IGroupFormingBehavior {
public void removeGroup(SocialMovementGroup group) {
groupCon.removeGroup(group);
long delay = 0;
long delay = 0;
if(groupFormationDelay == -1) {
delay = movementModel.getAttractionAssignmentStrategy().getPauseTime(group.getLeader().getCurrentTargetAttractionPoint());
if(group.getLeader() == null) {
delay = movementModel.getAttractionAssignmentStrategy().getPauseTime(null);
}
else {
delay = movementModel.getAttractionAssignmentStrategy().getPauseTime(group.getLeader().getCurrentTargetAttractionPoint());
}
}
else {
delay = rndGroupFormationDelay();
}
System.out.println(Time.getFormattedTime(delay));
Event.scheduleWithDelay(delay, new EventHandler() {
@Override
public void eventOccurred(Object content, int type) {
......
......@@ -101,7 +101,7 @@ public abstract class AbstractAttractionBasedAssignmentStrategy implements IAttr
private long getRandomUniformDistributionPauseTime(AttractionPoint attractionPoint) {
// if attraction point has own pause time use it, otherwise use default values
if(attractionPoint.hasPauseTime()) {
if(attractionPoint != null && attractionPoint.hasPauseTime()) {
return (long) (rnd.nextDouble() * (attractionPoint.getPauseTimeMax() - attractionPoint.getPauseTimeMin())) + attractionPoint.getPauseTimeMin();
}
else {
......
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