void TestIStockDataFile::test3()
{
  const char* fname = "testdata/stockdata3.dat";
  const double delta = 0.01;

  IStockDataFile f(m_ctx);

  CPPUNIT_ASSERT_EQUAL(std::string(""), f.getFileName());

  CPPUNIT_ASSERT(f.open(fname));

  CPPUNIT_ASSERT_EQUAL(std::string(fname), f.getFileName());

  RangeData data;
  CPPUNIT_ASSERT(!f.read(data));
  CPPUNIT_ASSERT_EQUAL(1, int(data.size()));    

  const RangeData::Point& pt1 = data.get(0);
  
  CPPUNIT_ASSERT_DOUBLES_EQUAL(1.00, pt1.open, delta);
  CPPUNIT_ASSERT_DOUBLES_EQUAL(2.00, pt1.close, delta);
  CPPUNIT_ASSERT_DOUBLES_EQUAL(3.00, pt1.min, delta);
  CPPUNIT_ASSERT_DOUBLES_EQUAL(4.00, pt1.max, delta);
  CPPUNIT_ASSERT_DOUBLES_EQUAL(5.00, pt1.volume, delta);
  CPPUNIT_ASSERT_EQUAL(std::string("20031231T010203"),
                       boost::posix_time::to_iso_string(pt1.tradeTime));
}
void TestIStockDataFile::test2()
{
  const char* fname = "testdata/stockdata2.dat";

  IStockDataFile f(m_ctx);

  CPPUNIT_ASSERT_EQUAL(std::string(""), f.getFileName());

  CPPUNIT_ASSERT(f.open(fname));

  CPPUNIT_ASSERT_EQUAL(std::string(fname), f.getFileName());

  RangeData data;
  CPPUNIT_ASSERT(!f.read(data));
  CPPUNIT_ASSERT_EQUAL(0, int(data.size()));    
}
Exemple #3
0
  bool RangeData::operator == (const RangeData& other) const
  {
    if (size() != other.size())
    {
      return false;
    }

    size_type n = size();
    for (size_type i = 0; i < n; ++i)
    {
      if (get(i) != other.get(i))
      {
        return false;
      }
    }

    return true;
  }
int mapper(Robot *robot, Point *at, Vector *velocity) {
	RangeData *sensorData = robot->GetRangeData();
	
	redisplay();
	
	for ( int i = 0; i < sensorData->size(); i++ ) {
		Vector v = (*sensorData)[i];
		v.direction -= .5*3.14159;
		v.direction += velocity->direction;
		
		v.print();
		
    	if ( v.direction > 3.14159 ) v.direction -= 2*3.14159;                                          
    	if ( v.direction < -3.14159 ) v.direction += 2*3.14159;
    	
    	for ( double j = dtor(15); j >= 0.0; j -= .01 ) {
    		mapVector( *at, (Vector){v.direction + j, v.magnitude} );
    		mapVector( *at, (Vector){v.direction - j, v.magnitude} );
		}
	}
}
Exemple #5
0
  void RangeData::summarizeMonthly(RangeData& newData) const
  {
    newData.clear();

    // nothing to do if we have no data!
    if (size() <= 0)
    {
      return;
    }

    size_type totalPoints = size();
    size_type startIdx = 0;
    size_type endIdx = 1;
    int currMonth = get(0).tradeTime.date().month();

    while (endIdx < totalPoints)
    {
      int newMonth = get(endIdx).tradeTime.date().month();

      // if we are in a new month, then we create a point for up to the
      // previous end index
      if (newMonth != currMonth)
      {
        // the new data point
        Point point = getSummaryPoint(startIdx, endIdx - 1);

        // add the data point!
        newData.add(point);

        // update startIdx to consider next window
        startIdx = endIdx;
        currMonth = newMonth;
      }

      ++endIdx;
    }

    newData.add(getSummaryPoint(startIdx, endIdx - 1));
  }
Exemple #6
0
  void RangeData::summarize(RangeData& newData, int points) const
  {
    assert(points > 0);

    newData.clear();

    // nothing to do if we have no data!
    if (size() <= 0)
    {
      return;
    }

    size_type totalPoints = size();

    size_type startIdx = 0;

    while (startIdx < totalPoints)
    {
      // [startIdx, endIdx] are the data points we're summarizing
      size_type endIdx = startIdx + points - 1;
      if (endIdx >= totalPoints)
      {
        endIdx = totalPoints - 1;
      }

      // actual number of data points (may be less than points for last
      // batch)
      
      // the new data point
      Point point = getSummaryPoint(startIdx, endIdx);

      // add the data point!
      newData.add(point);

      // update startIdx to consider next window
      startIdx = endIdx + 1;
    }
  }
int obstacleAvoidance(Robot *robot, Point *at, Vector *velocity) {
	RangeData *rdata = robot->GetRangeData();
	RangeData::iterator it;
	Vector rForce = (Vector){ 0.0, 0.0 }, force;
	const Vector currentVelocity = *robot->GetVelocity();
	double ignoreAngle = dtor(45);
	
	if ( robot->Ranger() ) {
		ignoreAngle = dtor(35);
	}
	
	// Calculate a repelling force (rForce) to push our robot away
	// from obstacles.
	for ( it = rdata->begin(); it < rdata->end(); it++ ) {
		if ( it->magnitude == it->magnitude && it->magnitude > 0.0 && 
			 it->magnitude < 1.0 && it->direction > ignoreAngle &&
			 it->direction < (dtor(180) - ignoreAngle) ) {
			//std::cout << "Obstacle: ";
			//it->print();
			if (rForce.magnitude == 0.0) rForce = *it;
			else rForce = rForce + (*it);
		}
	}
	
	rForce.direction -= (.5 * PI);
	rForce.direction += robot->GetVelocity()->direction;
	
	force = (-rForce) + *robot->GetVelocity();
	
	
	if ( fabs(force.direction - robot->GetVelocity()->direction) > MIN_TURNRATE && 
		 velocity->magnitude > 0.0 && rForce.magnitude > 0.0) {
		 
		velocity->direction = force.direction;
		
		turning = true;
	}
}
Exemple #8
0
  void RangeData::merge(const RangeData& other, bool removeDups)
  {
    RangeData mergedData;
    mergedData.resize(size() + other.size());

    std::merge(begin(), end(),
               other.begin(), other.end(),
               mergedData.begin(), earlier);

    RangeData::iterator newEnd = mergedData.end();

    if (removeDups)
    {
      newEnd = std::unique(mergedData.begin(), mergedData.end());
    }

    m_data.resize(std::distance(mergedData.begin(), newEnd));
    std::copy(mergedData.begin(), newEnd, m_data.begin());
  }
void TestIStockDataFile::test1()
{
  const char* fname = "testdata/stockdata1.dat";
  const double delta = 0.01;

  IStockDataFile f(m_ctx);

  CPPUNIT_ASSERT_EQUAL(std::string(""), f.getFileName());

  CPPUNIT_ASSERT(f.open(fname));

  CPPUNIT_ASSERT_EQUAL(std::string(fname), f.getFileName());

  {
    RangeData::Point pt1;
    RangeData::Point pt2;
    RangeData::Point pt3;

    CPPUNIT_ASSERT(f.read(pt1));

    CPPUNIT_ASSERT_DOUBLES_EQUAL(0.00, pt1.open, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(1.00, pt1.close, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(2.00, pt1.min, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(3.00, pt1.max, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(4.00, pt1.volume, delta);
    CPPUNIT_ASSERT_EQUAL(std::string("20020131T235959"),
                         boost::posix_time::to_iso_string(pt1.tradeTime));

    CPPUNIT_ASSERT(f.read(pt2));

    CPPUNIT_ASSERT_DOUBLES_EQUAL(10.00, pt2.open, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(11.05, pt2.close, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(12.10, pt2.min, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(13.15, pt2.max, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(14.20, pt2.volume, delta);
    CPPUNIT_ASSERT_EQUAL(std::string("20031231T010203"),
                         boost::posix_time::to_iso_string(pt2.tradeTime));

    CPPUNIT_ASSERT(!f.read(pt3));
  }


  CPPUNIT_ASSERT(f.rewind());

  {
    RangeData data;
    CPPUNIT_ASSERT(f.read(data));
    CPPUNIT_ASSERT_EQUAL(2, int(data.size()));    

    const RangeData::Point& pt1 = data.get(0);
    const RangeData::Point& pt2 = data.get(1);

    CPPUNIT_ASSERT_DOUBLES_EQUAL(0.00, pt1.open, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(1.00, pt1.close, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(2.00, pt1.min, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(3.00, pt1.max, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(4.00, pt1.volume, delta);
    CPPUNIT_ASSERT_EQUAL(std::string("20020131T235959"),
                         boost::posix_time::to_iso_string(pt1.tradeTime));

    CPPUNIT_ASSERT_DOUBLES_EQUAL(10.00, pt2.open, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(11.05, pt2.close, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(12.10, pt2.min, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(13.15, pt2.max, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(14.20, pt2.volume, delta);
    CPPUNIT_ASSERT_EQUAL(std::string("20031231T010203"),
                         boost::posix_time::to_iso_string(pt2.tradeTime));

    data.clear();

    CPPUNIT_ASSERT(f.read(data));
    CPPUNIT_ASSERT_EQUAL(0, int(data.size()));
  }


  f.close();
  CPPUNIT_ASSERT_EQUAL(std::string(""), f.getFileName());
}