// accept a time step into the solution history
void e_trsolver::acceptstep_sync()
{
    statIterations += iterations;
    if (--convError < 0) convHelper = 0;

    // Now advance in time or not...
    if (running > 1)
    {
        adjustDelta_sync (current);
//        deltaOld = delta;
//        stepDelta = deltaOld;
//        nextStates ();
//        rejected = 0;
        adjustOrder ();
    }
    else
    {
        fillStates ();
        nextStates ();
        rejected = 0;
    }

    saveCurrent = current;
    current += delta;
    running++;
    converged++;

    // Tell integrators to be running.
    setMode (MODE_NONE);

    // Initialize or update history.
    if (running > 1)
    {
        // update the solution history with the new results
        updateHistory (current);
    }
    else
    {
        // we have just solved the first transient state
        initHistory (current);
    }

    // store the current time
    lastsynctime = current;
}
// asynchronous step solver
int e_trsolver::stepsolve_async(nr_double_t steptime)
{
    // Start to sweep through time.
    int error = 0;
    convError = 0;

    time = steptime;
    // update the interpolation time of any externally controlled
    // components which require it.
    updateExternalInterpTime(time);
    // make the stored histories for all ircuits that have
    // requested them at least as long as the next major time
    // step so we can reject the step later if needed and
    // restore all the histories to their previous state
    updateHistoryAges (time - lastasynctime);

    //delta = (steptime - time) / 10;
    //if (progress) logprogressbar (i, swp->getSize (), 40);
#if DEBUG && 0
    messagefcn (LOG_STATUS, "NOTIFY: %s: solving netlist for t = %e\n",
              getName (), (double) time);
#endif

    do
    {
#if STEPDEBUG
        if (delta == deltaMin)
        {
            messagefcn (LOG_ERROR,
                      "WARNING: %s: minimum delta h = %.3e at t = %.3e\n",
                      getName (), (double) delta, (double) current);
        }
#endif
        // update the integration coefficients
        updateCoefficients (delta);

        // Run predictor to get a start value for the solution vector for
        // the successive iterative corrector process
        error += predictor ();

        // restart Newton iteration
        if (rejected)
        {
            restart ();      // restart non-linear devices
            rejected = 0;
        }

        // Run corrector process with appropriate exception handling.
        // The corrector iterates through the solutions of the integration
        // process until a certain error tolerance has been reached.
        try_running () // #defined as:    do {
        {
            error += corrector ();
        }
        catch_exception () // #defined as:   } while (0); if (estack.top ()) switch (estack.top()->getCode ())
        {
        case EXCEPTION_NO_CONVERGENCE:
            pop_exception ();

            // Reduce step-size (by half) if failed to converge.
            if (current > 0) current -= delta;
            delta /= 2;
            if (delta <= deltaMin)
            {
                delta = deltaMin;
                adjustOrder (1);
            }
            if (current > 0) current += delta;

            // Update statistics.
            statRejected++;
            statConvergence++;
            rejected++;
            converged = 0;
            error = 0;

            // Start using damped Newton-Raphson.
            convHelper = CONV_SteepestDescent;
            convError = 2;
#if DEBUG
            messagefcn (LOG_ERROR, "WARNING: delta rejected at t = %.3e, h = %.3e "
                      "(no convergence)\n", (double) saveCurrent, (double) delta);
#endif
            break;
        default:
            // Otherwise return.
            estack.print ();
            error++;
            break;
        }
        if (error) return -1;
        if (rejected) continue;

        // check whether Jacobian matrix is still non-singular
        if (!A->isFinite ())
        {
            messagefcn (LOG_ERROR, "ERROR: %s: Jacobian singular at t = %.3e, "
                      "aborting %s analysis\n", getName (), (double) current,
			getDescription ().c_str());
            return -1;
        }

        // Update statistics and no more damped Newton-Raphson.
        statIterations += iterations;
        if (--convError < 0) convHelper = 0;

        // Now advance in time or not...
        if (running > 1)
        {
            adjustDelta (time);
            adjustOrder ();
        }
        else
        {
            fillStates ();
            nextStates ();
            rejected = 0;
        }

        saveCurrent = current;
        current += delta;
        running++;
        converged++;

        // Tell integrators to be running.
        setMode (MODE_NONE);

        // Initialize or update history.
        if (running > 1)
        {
            updateHistory (saveCurrent);
        }
        else
        {
            initHistory (saveCurrent);
        }
    }
    while (saveCurrent < time); // Hit a requested time point?

    return 0;
}
/* This function tries to adapt the current time-step according to the
   global truncation error. */
void e_trsolver::adjustDelta_sync (nr_double_t t)
{
    deltaOld = delta;

    // makes a new delta based on truncation error
//    delta = checkDelta ();

    if (delta > deltaMax)
    {
        delta = deltaMax;
    }

    if (delta < deltaMin)
    {
        delta = deltaMin;
    }

    // delta correction in order to hit exact breakpoint
    int good = 0;
//    if (!relaxTSR)   // relaxed step raster?
//    {
//        if (!statConvergence || converged > 64)   /* Is this a good guess? */
//        {
//            // check next breakpoint
//            if (stepDelta > 0.0)
//            {
//                // restore last valid delta
//                delta = stepDelta;
//                stepDelta = -1.0;
//            }
//            else
//            {
//                if (delta > (t - current) && t > current)
//                {
//                    // save last valid delta and set exact step
//                    stepDelta = deltaOld;
//                    delta = t - current;
//                    good = 1;
//                }
//                else
//                {
//                    stepDelta = -1.0;
//                }
//            }
//            if (delta > deltaMax) delta = deltaMax;
//            if (delta < deltaMin) delta = deltaMin;
//        }
//    }

    stepDelta = -1;
    good = 1;

    // usual delta correction
    if (delta > 0.9 * deltaOld || good)   // accept current delta
    {
        nextStates ();
        rejected = 0;
#if STEPDEBUG
        logprint (LOG_STATUS,
                  "DEBUG: delta accepted at t = %.3e, h = %.3e\n",
                  (double) current, (double) delta);
#endif
    }
    else if (deltaOld > delta)   // reject current delta
    {
        rejected++;
        statRejected++;
#if STEPDEBUG
        logprint (LOG_STATUS,
                  "DEBUG: delta rejected at t = %.3e, h = %.3e\n",
                  (double) current, (double) delta);
#endif
        if (current > 0) current -= deltaOld;
    }
    else
    {
        nextStates ();
        rejected = 0;
    }
}
Exemple #4
0
bool Entity::updateState(cv::Mat image, int timeMS) {
	cv::Mat result;
	cv::Point minLoc;
	cv::Point maxLoc;

	double minVal;
	double maxVal;
	int method = cv::TM_SQDIFF;

	int x_margin, y_margin;
	int x, y, width, height;

	if (type == EntityType::MARIO_SMALL_L ||
		type == EntityType::MARIO_SMALL_R ||
		type == EntityType::MARIO_BIG_L ||
		type == EntityType::MARIO_BIG_R ||
		type == EntityType::MARIO_FIRE_L ||
		type == EntityType::MARIO_FIRE_R) {
		x_margin = 10;
		y_margin = 10;
	}
	else if (type == EntityType::GOOMBA || type == EntityType::BEAM) {
		x_margin = 6;
		y_margin = 10;
	}
	else {
		x_margin = 5;
		y_margin = 5;
	}

	if (!isInFrame && (
		type == EntityType::MARIO_SMALL_L ||
		type == EntityType::MARIO_SMALL_R ||
		type == EntityType::MARIO_BIG_L   ||
		type == EntityType::MARIO_BIG_R   ||
		type == EntityType::MARIO_FIRE_L  ||
		type == EntityType::MARIO_FIRE_R)) {
		x = 0;
		y = 0;
		width = image.cols;
		height = image.rows;
	}
	else {
		x = std::max(0, bbox.x + loc.x - x_margin);
		width = std::min(bbox.width + x_margin * 2, image.cols - x);
		y = std::max(0, bbox.y + loc.y - y_margin);
		height = std::min(bbox.height + y_margin * 2, image.rows - y);
	}
	cv::Mat roi = image(cv::Rect(x, y, width, height));
	// cv::Mat roi = image.clone();
	if (type == EntityType::MARIO_SMALL_L ||
		type == EntityType::MARIO_SMALL_R ||
		type == EntityType::MARIO_BIG_L ||
		type == EntityType::MARIO_BIG_R ||
		type == EntityType::MARIO_FIRE_L ||
		type == EntityType::MARIO_FIRE_R) {
	}
	std::vector<EntityType> possStates = nextStates();

	for (EntityType tmpType : possStates) {
		// Create the result matrix
		int result_cols = roi.cols - spriteTable[tmpType].cols + 1;
		int result_rows = roi.rows - spriteTable[tmpType].rows + 1; 
		result.create(result_rows, result_cols, CV_32FC1);
		cv::Mat resultImg = result.clone();

		// Do the Matching and Normalize
		cv::matchTemplate(roi, spriteTable[tmpType], result, method);

		// Try and find the first enemy template
		cv::minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());

		if (minVal < getDetThresh(tmpType)) { // We found it
			if (!isInFrame && (
				type == EntityType::MARIO_SMALL_L ||
				type == EntityType::MARIO_SMALL_R ||
				type == EntityType::MARIO_BIG_L ||
				type == EntityType::MARIO_BIG_R ||
				type == EntityType::MARIO_FIRE_L ||
				type == EntityType::MARIO_FIRE_R)) {
				loc = minLoc;
			}
			else {
				loc = cv::Point(minLoc.x + loc.x + bbox.x - x_margin, minLoc.y + loc.y + bbox.y - y_margin);
			}
			type = tmpType;

			setBoundingBox();
			isInFrame = true;
			msLastSeen = timeMS;

			return true;
		}
	}

	isInFrame = false;
	return false;
}