void
MSTLLogicControl::WAUTSwitchProcedure::switchToPos(SUMOTime simStep, MSSimpleTrafficLightLogic *givenLogic, unsigned int pos) {
    MSSimpleTrafficLightLogic *myLogic = givenLogic;
    unsigned int posTo = pos;
    unsigned int stepTo = myLogic->getIndexFromOffset(posTo);
    unsigned int diff = getDiffToStartOfPhase(myLogic, posTo);
    MSPhaseDefinition myPhase = myLogic->getPhase(stepTo);
    unsigned int dur = (unsigned int)myPhase.duration - diff;
    myLogic->changeStepAndDuration(myControl ,simStep, stepTo, dur);
}
void
MSTLLogicControl::WAUTSwitchProcedure_Stretch::cutLogic(SUMOTime step, unsigned int pos, size_t deltaToCut) {
    MSSimpleTrafficLightLogic *LogicTo = (MSSimpleTrafficLightLogic*) myTo;
    unsigned int startPos = pos;
    unsigned int actStep = LogicTo->getIndexFromOffset(startPos);
    size_t allCutTime = deltaToCut;
    // switches to startPos and cuts this phase, if there is a "Bereich"
    int noBereiche = getStretchBereicheNo(myTo);
    size_t toCut = 0;
    for (int i=0; i<noBereiche; i++) {
        StretchBereichDef def = getStretchBereichDef(myTo, i+1);
        unsigned int begin = (unsigned int) def.begin;
        unsigned int end = (unsigned int) def.end;
        size_t stepOfBegin = LogicTo->getIndexFromOffset(begin);
        if (stepOfBegin == actStep)	{
            if (begin < startPos) {
                toCut = end - startPos;
            } else {
                toCut = end - begin;
            }
            if (allCutTime < toCut) {
                toCut = allCutTime;
            }
            allCutTime = allCutTime - toCut;
        }
    }
    unsigned int remainingDur = LogicTo->getPhase(actStep).duration - getDiffToStartOfPhase(LogicTo, startPos);
    unsigned int newDur = remainingDur - toCut;
    myTo->changeStepAndDuration(myControl,step,actStep,newDur);

    // changes the duration of all other phases
    int currStep = actStep + 1;
    if (currStep == (int) LogicTo->getPhases().size()) {
        currStep = 0;
    }
    while (allCutTime > 0) {
        for (int i=currStep; i<(int) LogicTo->getPhases().size(); i++) {
            size_t beginOfPhase = LogicTo->getOffsetFromIndex(i);
            unsigned int durOfPhase = LogicTo->getPhase(i).duration;
            size_t endOfPhase = beginOfPhase + durOfPhase;
            for (int i=0; i<noBereiche; i++) {
                StretchBereichDef def = getStretchBereichDef(myTo, i+1);
                size_t begin = (size_t) def.begin;
                size_t end = (size_t) def.end;
                if ((beginOfPhase <= begin) && (endOfPhase >= end)) {
                    size_t maxCutOfPhase = end - begin;
                    if (allCutTime< maxCutOfPhase) {
                        maxCutOfPhase = allCutTime;
                    }
                    allCutTime = allCutTime - maxCutOfPhase;
                    durOfPhase = durOfPhase - maxCutOfPhase;
                }
            }
            LogicTo->addOverridingDuration(durOfPhase);
        }
        currStep = 0;
    }
}
unsigned int
MSTLLogicControl::WAUTSwitchProcedure::getDiffToStartOfPhase(MSSimpleTrafficLightLogic *givenLogic, unsigned int pos) {
    MSSimpleTrafficLightLogic *myLogic = givenLogic;
    unsigned int myPos = pos;
    unsigned int stepOfMyPos = myLogic->getIndexFromOffset(myPos);
    unsigned int startOfPhase = myLogic->getOffsetFromIndex(stepOfMyPos);
    MSPhaseDefinition myPhase = myLogic->getPhase(stepOfMyPos);
    unsigned int durOfPhase = (unsigned int)myPhase.duration;

    assert(myPos >= startOfPhase);
    unsigned int diff = myPos - startOfPhase;
    assert(diff <= durOfPhase);
    return diff;
}
bool
MSTLLogicControl::WAUTSwitchProcedure::isPosAtGSP(SUMOTime step, MSSimpleTrafficLightLogic *testLogic) {
    MSSimpleTrafficLightLogic *givenLogic = (MSSimpleTrafficLightLogic*) testLogic;
    size_t CycleTime = givenLogic->getDefaultCycleTime();
    SUMOReal gspFrom = getGSPValue(givenLogic);
    ///get the position of the given signalprogramm at the actual simulationsecond
    size_t posFrom = givenLogic -> getPhaseIndexAtTime(step);

    if (gspFrom == CycleTime)	{
        gspFrom = 0;
    }
    ///compare the position of the given programm with the GSP (GSP = "GuenstigerSchaltPunkt")
    if (gspFrom == posFrom) {
        return true;
    } else {
        return false;
    }
}
Esempio n. 5
0
void
GUIViewTraffic::onGamingClick(Position pos) {
    MSTLLogicControl& tlsControl = MSNet::getInstance()->getTLSControl();
    const std::vector<MSTrafficLightLogic*>& logics = tlsControl.getAllLogics();
    MSTrafficLightLogic* minTll = 0;
    SUMOReal minDist = std::numeric_limits<SUMOReal>::infinity();
    for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
        // get the logic
        MSTrafficLightLogic* tll = (*i);
        if (tlsControl.isActive(tll)) {
            // get the links
            const MSTrafficLightLogic::LaneVector& lanes = tll->getLanesAt(0);
            if (lanes.size() > 0) {
                const Position& endPos = lanes[0]->getShape().back();
                if (endPos.distanceTo(pos) < minDist) {
                    minDist = endPos.distanceTo(pos);
                    minTll = tll;
                }
            }
        }
    }
    if (minTll != 0) {
        const MSTLLogicControl::TLSLogicVariants& vars = tlsControl.get(minTll->getID());
        const std::vector<MSTrafficLightLogic*> logics = vars.getAllLogics();
        if (logics.size() > 1) {
            MSSimpleTrafficLightLogic* l = (MSSimpleTrafficLightLogic*) logics[0];
            for (unsigned int i = 0; i < logics.size() - 1; ++i) {
                if (minTll->getProgramID() == logics[i]->getProgramID()) {
                    l = (MSSimpleTrafficLightLogic*) logics[i + 1];
                    tlsControl.switchTo(minTll->getID(), l->getProgramID());
                }
            }
            if (l == logics[0]) {
                tlsControl.switchTo(minTll->getID(), l->getProgramID());
            }
            l->changeStepAndDuration(tlsControl, MSNet::getInstance()->getCurrentTimeStep(), 0, l->getPhase(0).duration);
            update();
        }
    }
}
void
MSTLLogicControl::WAUTSwitchProcedure_Stretch::adaptLogic(SUMOTime step, SUMOReal position) {
    MSSimpleTrafficLightLogic *LogicTo = (MSSimpleTrafficLightLogic*) myTo;
    size_t cycleTime = LogicTo->getDefaultCycleTime();
    // the position, in which the logic has to be switched
    unsigned int startPos = (unsigned int) position;
    // this is the position, where the Logic have to be after synchronisation
    size_t posAfterSyn = LogicTo->getPhaseIndexAtTime(step);

    // switch the toLogic to the startPosition
    // fehlt!!!!
    // erfolgt in cutLogic und/oder stretchLogic!


    // calculate the difference, that has to be equalized
    size_t deltaToCut = 0;
    if (posAfterSyn < startPos) {
        deltaToCut = posAfterSyn + cycleTime - startPos;
    } else deltaToCut =  posAfterSyn - startPos;
    // test, wheter cutting of the Signalplan is possible
    size_t deltaPossible = 0;
    int noBereiche = getStretchBereicheNo(myTo);
    for (int i=0; i<noBereiche; i++) {
        StretchBereichDef def = getStretchBereichDef(myTo, i+1);
        assert(def.end >= def.begin) ;
        deltaPossible = deltaPossible + (size_t)(def.end - def.begin);
    }
    int stretchUmlaufAnz = (int) TplConvert<char>::_2SUMOReal(LogicTo->getParameterValue("StretchUmlaufAnz").c_str());
    deltaPossible = stretchUmlaufAnz * deltaPossible;
    if ((deltaPossible > deltaToCut)&&(deltaToCut < (cycleTime / 2))) {
        cutLogic(step, startPos, deltaToCut);
    } else {
        size_t deltaToStretch = cycleTime - deltaToCut;
        if (deltaToStretch == cycleTime) {
            deltaToStretch = 0;
        }
        stretchLogic(step, startPos, deltaToStretch);
    }

}
void
MSTLLogicControl::WAUTSwitchProcedure_GSP::adaptLogic(SUMOTime step) {
    SUMOTime simStep = step;
    MSSimpleTrafficLightLogic *LogicTo = (MSSimpleTrafficLightLogic*) myTo;
    SUMOReal gspTo = getGSPValue(myTo);
    unsigned int stepTo = LogicTo->getIndexFromOffset((unsigned int) gspTo);
    size_t cycleTimeTo = LogicTo->getDefaultCycleTime();
    // gets the actual position from the myToLogic
    size_t actPosTo = LogicTo->getPhaseIndexAtTime(simStep);
    size_t deltaToStretch= 0;
    if (gspTo == cycleTimeTo) {
        gspTo=0;
    }
    unsigned int diff = getDiffToStartOfPhase(LogicTo, (unsigned int) gspTo);
    if (gspTo >= actPosTo) {
        deltaToStretch = (size_t)(gspTo - actPosTo);
    } else {
        deltaToStretch = (size_t)(cycleTimeTo - actPosTo + gspTo);
    }
    unsigned int newdur = (unsigned int) LogicTo->getPhase(stepTo).duration - diff + deltaToStretch;
    LogicTo->changeStepAndDuration(myControl, simStep, stepTo, newdur);
}
void
MSTLLogicControl::WAUTSwitchProcedure_Stretch::stretchLogic(SUMOTime step, unsigned int startPos, size_t deltaToStretch) {
    MSSimpleTrafficLightLogic *LogicTo = (MSSimpleTrafficLightLogic*) myTo;
    unsigned int currPos = startPos;
    unsigned int currStep = LogicTo->getIndexFromOffset(currPos);
    unsigned int durOfPhase = LogicTo->getPhase(currStep).duration;
    size_t allStretchTime = deltaToStretch;
    size_t remainingStretchTime = allStretchTime;
    int StretchTimeOfPhase = 0;
    size_t stretchUmlaufAnz = (size_t) TplConvert<char>::_2SUMOReal(LogicTo->getParameterValue("StretchUmlaufAnz").c_str());
    SUMOReal facSum = 0;
    int noBereiche = getStretchBereicheNo(myTo);
    int x;
    for (x=0; x<noBereiche; x++) {
        StretchBereichDef def = getStretchBereichDef(myTo, x+1);
        facSum += def.fac;
    }
    facSum *= stretchUmlaufAnz;


    //switch to startPos and stretch this phase, if there is a end of "bereich" between startpos and end of phase
    size_t diffToStart = getDiffToStartOfPhase(LogicTo, currPos);
    for (x=0; x<noBereiche; x++) {
        StretchBereichDef def = getStretchBereichDef(myTo, x+1);
        size_t end = (size_t) def.end;
        size_t endOfPhase = (size_t)(currPos + durOfPhase - diffToStart);
        if (end <= endOfPhase && end >= currPos) {
            SUMOReal fac = def.fac;
            SUMOReal actualfac = fac / facSum;
            facSum = facSum - fac;
            StretchTimeOfPhase = (int)((float)remainingStretchTime * actualfac + 0.5);
            remainingStretchTime = allStretchTime - StretchTimeOfPhase;
        }
    }
    durOfPhase = durOfPhase - diffToStart + StretchTimeOfPhase;
    myTo->changeStepAndDuration(myControl,step,currStep,durOfPhase);

    currStep ++;
    if (currStep >= LogicTo->getPhases().size()) {
        currStep = 0;
    }

    // stretch all other phases, if there is a "bereich"
    while (remainingStretchTime > 0) {
        for (unsigned int i=currStep; i<LogicTo->getPhases().size() && remainingStretchTime > 0; i++) {
            durOfPhase = LogicTo->getPhase(i).duration;
            size_t beginOfPhase = LogicTo->getOffsetFromIndex(i);
            size_t endOfPhase = beginOfPhase + durOfPhase;
            for (int j=0; j<noBereiche && remainingStretchTime > 0; j++) {
                StretchBereichDef def = getStretchBereichDef(myTo, j+1);
                size_t end = (size_t) def.end;
                SUMOReal fac = def.fac;
                if ((beginOfPhase <= end) && (endOfPhase >= end)) {
                    SUMOReal actualfac = fac / facSum;
                    StretchTimeOfPhase = (int)((float)remainingStretchTime * actualfac + 0.5) ;
                    facSum -= fac;
                    durOfPhase += StretchTimeOfPhase;
                    remainingStretchTime -= StretchTimeOfPhase;
                }
            }
            LogicTo->addOverridingDuration(durOfPhase);
        }
        currStep = 0;
    }
}