Beispiel #1
0
Feet Feet::operator/(const Distance& rhs) const
{
	if(rhs.asFeet() == 0)
		throw std::logic_error("Divide by zero");
	return Feet(this->asFeet() / rhs.asFeet());
}
Beispiel #2
0
Feet::Feet(const Distance& other) :
	Distance(other.asFeet())
{
}
Beispiel #3
0
const Distance operator/(const Distance& numerator, const double denominator)
{
  return Distance(numerator.GetDistance() / denominator);
}
Beispiel #4
0
Feet Feet::operator*(const Distance& rhs) const
{
	return Feet(this->asFeet() * rhs.asFeet());
}
Beispiel #5
0
#include <pelopia/Distance.h>

#include <test/catch.hpp>

using namespace Mapzen :: Pelopia;
using namespace std;

TEST_CASE ( "Distance Construct Miles" ) 
{
    Distance d (Distance::Miles, 1);
    REQUIRE ( 1.0 == d.GetMiles() );
    REQUIRE ( 1.609344 == d.GetKilometers() );
}

TEST_CASE ( "Distance Construct Km" ) 
{
    Distance d (Distance::Kilometers, 2);
    REQUIRE ( Approx(1.24274) == d.GetMiles() );
    REQUIRE ( 2.0 == d.GetKilometers() );
}

Beispiel #6
0
const Distance operator+(const Distance& lhs, const Distance& rhs)
{
  return Distance( lhs.GetDistance() + rhs.GetDistance() );
}
/**
 * @track:      A Track
 * @iter:       Iterator to given Track
 * @is_route:   Is the track of the trackpoint actually a route?
 *
 * Sets the Trackpoint Edit Window to the values of the current trackpoint given in @tpl.
 */
void PropertiesDialogTP::set_dialog_data(Track * track, const TrackPoints::iterator & current_tp_iter, bool is_route)
{
	const HeightUnit height_unit = Preferences::get_unit_height();
	const DistanceUnit distance_unit = Preferences::get_unit_distance();
	const SpeedUnit speed_unit = Preferences::get_unit_speed();

	Trackpoint * tp = *current_tp_iter;

	this->trkpt_name->setEnabled(true);
	this->trkpt_name->setText(tp->name); /* The name may be empty, but we have to do this anyway (e.g. to overwrite non-empty name of previous trackpoint). */

	/* User can insert only if not at the end of track (otherwise use extend track). */
	this->button_insert_tp_after->setEnabled(std::next(current_tp_iter) != track->end());
	this->button_delete_current_tp->setEnabled(true);

	/* We can only split up a track if it's not an endpoint. */
	this->button_split_track->setEnabled(std::next(current_tp_iter) != track->end() && current_tp_iter != track->begin());

	this->button_go_forward->setEnabled(std::next(current_tp_iter) != track->end());
	this->button_go_back->setEnabled(current_tp_iter != track->begin());


	this->lat_entry->setEnabled(true);
	this->lon_entry->setEnabled(true);
	this->alt->setEnabled(true);
	this->timestamp_widget->setEnabled(tp->timestamp.is_valid());

	this->set_dialog_title(track->name);

	this->timestamp_widget->setEnabled(!is_route);
	if (is_route) {
		/* Remove any data that may have been previously displayed. */
		this->timestamp_widget->clear();
	}

	this->sync_to_current_tp_block = true; /* Don't update while setting data. */

	const LatLon lat_lon = tp->coord.get_latlon();
	this->lat_entry->setValue(lat_lon.lat);
	this->lon_entry->setValue(lat_lon.lon);


	this->alt->set_value_iu(tp->altitude);


	this->update_timestamp_widget(tp);

	this->sync_to_current_tp_block = false; /* Can now update after setting data. */


	if (this->cur_tp) {
		const Distance diff = Coord::distance_2(tp->coord, this->cur_tp->coord);
		this->diff_dist->setText(diff.convert_to_unit(Preferences::get_unit_distance()).to_nice_string());

		if (tp->timestamp.is_valid() && this->cur_tp->timestamp.is_valid()) {
			this->diff_time->setText(tr("%1 s").arg((long) (tp->timestamp.get_value() - this->cur_tp->timestamp.get_value())));
			if (tp->timestamp == this->cur_tp->timestamp) {
				this->diff_speed->setText("--");
			} else {
				const double dist = Coord::distance(tp->coord, this->cur_tp->coord);
				const Time duration = Time::get_abs_diff(tp->timestamp, this->cur_tp->timestamp);
				const Speed tmp_speed(dist / duration.get_value(), SpeedUnit::MetresPerSecond);
				this->diff_speed->setText(tmp_speed.to_string());
			}
		} else {
			this->diff_time->setText("");
			this->diff_speed->setText("");
		}
	}


	this->course->setText(tp->course.to_string());
	this->speed->setText(Speed(tp->speed, SpeedUnit::MetresPerSecond).convert_to_unit(speed_unit).to_string());
	this->hdop->setText(Distance(tp->hdop, SupplementaryDistanceUnit::Meters).convert_to_unit(distance_unit).to_nice_string());
	this->pdop->setText(Distance(tp->pdop, SupplementaryDistanceUnit::Meters).convert_to_unit(distance_unit).to_nice_string());
	this->vdop->setText(Altitude(tp->vdop, HeightUnit::Metres).convert_to_unit(height_unit).to_nice_string());
	this->sat->setText(tr("%1 / %2").arg(tp->nsats).arg((int) tp->fix_mode));


	this->cur_tp = tp;
}
Beispiel #8
0
	FrameCd::FrameCd( const Distance& r1,
			  const Distance& r2,
			  const Distance& w,
			  const Distance& h,
			  const Distance& waste,
			  const QString&  id )
		: Frame(id), mR1(r1), mR2(r2), mW(w), mH(h), mWaste(waste)
	{
		Distance wReal = (mW == 0) ? 2*mR1 : mW;
		Distance hReal = (mH == 0) ? 2*mR1 : mH;

		//
		// Create path
		//
		{
			/*
			 * Construct outer subpath (may be clipped if it's a business card CD)
			 */
			QPainterPath outerPath;
			outerPath.addEllipse( (wReal/2 - r1).pt(), (hReal/2 - r1).pt(), 2*r1.pt(), 2*r1.pt() );

			QPainterPath clipPath;
			clipPath.addRect( 0, 0, wReal.pt(), hReal.pt() );

			mPath.addPath( outerPath & clipPath );
			mPath.closeSubpath();

			/*
			 * Add inner subpath
			 */
			mPath.addEllipse( (wReal/2 - r2).pt(), (hReal/2 - r2).pt(), 2*r2.pt(), 2*r2.pt() );
		}

		//
		// Create clip path
		//
		{
			Distance r1Clip = mR1 + mWaste;
			Distance r2Clip = mR2 - mWaste;
			Distance wClip = (mW == 0) ? 2*r1Clip : mW + 2*mWaste;
			Distance hClip = (mH == 0) ? 2*r1Clip : mH + 2*mWaste;

			/*
			 * Construct outer subpath (may be clipped if it's a business card CD)
			 */
			QPainterPath outerPath;
			outerPath.addEllipse( (wReal/2 - r1Clip).pt(), (hReal/2 - r1Clip).pt(), 2*r1Clip.pt(), 2*r1Clip.pt() );

			QPainterPath clipPath;
			clipPath.addRect( -mWaste.pt(), -mWaste.pt(), wClip.pt(), hClip.pt() );

			mClipPath.addPath( outerPath & clipPath );
			mClipPath.closeSubpath();

			/*
			 * Add inner subpath
			 */
			mClipPath.addEllipse( (wReal/2 - r2Clip).pt(), (hReal/2 - r2Clip).pt(), 2*r2Clip.pt(), 2*r2Clip.pt() );
		}
	}
Beispiel #9
0
/*
 * Runs the GDE3 algorithm.
 * @return a <code>SolutionSet</code> that is a set of non dominated solutions
 * as a result of the algorithm execution
 */
SolutionSet * GDE3::execute() {

  int populationSize;
  int maxIterations;
  int evaluations;
  int iterations;

  SolutionSet * population;
  SolutionSet * offspringPopulation;

  Distance * distance;
  Comparator * dominance;

  Operator * crossoverOperator;
  Operator * selectionOperator;

  distance  = new Distance();
  dominance = new DominanceComparator();

  Solution ** parent;

  //Read the parameters
  populationSize = *(int *) getInputParameter("populationSize");
  maxIterations  = *(int *) getInputParameter("maxIterations");

  //Initialize the variables
  population  = new SolutionSet(populationSize);
  evaluations = 0;
  iterations  = 0;

  //Read the operators
  crossoverOperator = operators_["crossover"];
  selectionOperator = operators_["selection"];

  // Create the initial solutionSet
  Solution * newSolution;
  for (int i = 0; i < populationSize; i++) {
    newSolution = new Solution(problem_);
    problem_->evaluate(newSolution);
    problem_->evaluateConstraints(newSolution);
    evaluations++;
    population->add(newSolution);
  } //for

  // Generations ...
  while (iterations < maxIterations) {
    // Create the offSpring solutionSet
    offspringPopulation  = new SolutionSet(populationSize * 2);

    for (int i = 0; i < populationSize; i++){
      // Obtain parents. Two parameters are required: the population and the
      //                 index of the current individual
      void ** object1 = new void*[2];
      object1[0] = population;
      object1[1] = &i;
      parent = (Solution **) (selectionOperator->execute(object1));
      delete[] object1;

      Solution * child ;
      // Crossover. Two parameters are required: the current individual and the
      //            array of parents
      void ** object2 = new void*[2];
      object2[0] = population->get(i);
      object2[1] = parent;
      child = (Solution *) (crossoverOperator->execute(object2));
      delete[] object2;
      delete[] parent;

      problem_->evaluate(child) ;
      problem_->evaluateConstraints(child);
      evaluations++ ;

      // Dominance test
      int result  ;
      result = dominance->compare(population->get(i), child) ;
      if (result == -1) { // Solution i dominates child
        offspringPopulation->add(new Solution(population->get(i)));
        delete child;
      } // if
      else if (result == 1) { // child dominates
        offspringPopulation->add(child) ;
      } // else if
      else { // the two solutions are non-dominated
        offspringPopulation->add(child) ;
        offspringPopulation->add(new Solution(population->get(i)));
      } // else
    } // for

    // Ranking the offspring population
    Ranking * ranking = new Ranking(offspringPopulation);

    int remain = populationSize;
    int index  = 0;
    SolutionSet * front = NULL;
    for (int i = 0; i < populationSize; i++) {
      delete population->get(i);
    }
    population->clear();

    // Obtain the next front
    front = ranking->getSubfront(index);

    while ((remain > 0) && (remain >= front->size())){
      //Assign crowding distance to individuals
      distance->crowdingDistanceAssignment(front,problem_->getNumberOfObjectives());
      //Add the individuals of this front
      for (int k = 0; k < front->size(); k++ ) {
        population->add(new Solution(front->get(k)));
      } // for

      //Decrement remain
      remain = remain - front->size();

      //Obtain the next front
      index++;
      if (remain > 0) {
        front = ranking->getSubfront(index);
      } // if
    } // while

    // remain is less than front(index).size, insert only the best one
    if (remain > 0) {  // front contains individuals to insert
      while (front->size() > remain) {
         distance->crowdingDistanceAssignment(front,problem_->getNumberOfObjectives());
         Comparator * crowdingComparator = new CrowdingComparator();
         int indexWorst = front->indexWorst(crowdingComparator);
         delete crowdingComparator;
         delete front->get(indexWorst);
         front->remove(indexWorst);
      }
      for (int k = 0; k < front->size(); k++) {
        population->add(new Solution(front->get(k)));
      }

      remain = 0;
    } // if

    delete ranking;
    delete offspringPopulation;

    iterations ++ ;
  } // while

  delete dominance;
  delete distance;

  // Return the first non-dominated front
  Ranking * ranking = new Ranking(population);
  SolutionSet * result = new SolutionSet(ranking->getSubfront(0)->size());
  for (int i=0;i<ranking->getSubfront(0)->size();i++) {
    result->add(new Solution(ranking->getSubfront(0)->get(i)));
  }
  delete ranking;
  delete population;

  return result;

} // execute
double calculateDrivingTime(const Distance& distance, double speed)
{
	return distance.inKilometers() / speed * 3600;
}
Beispiel #11
0
void ClusterTools::computeGlobalDistanceDistribution(
  DRTreeLikelihood& drtl,
  const SequenceSimulator& simulator,
	SubstitutionCount& nijt,
  const Distance& distance,
  AgglomerativeDistanceMethod& clustering,
  size_t sizeOfDataSet,
  size_t nrep,
  size_t maxGroupSize,
  ofstream* out)
{
  size_t nbTypes = nijt.getNumberOfSubstitutionTypes();
  vector<string> siteNames(sizeOfDataSet);
  for (size_t i = 0; i < sizeOfDataSet; i++)
  {
    siteNames[i] = TextTools::toString(i);
  }
  
  if (out)
    *out << "Rep\tGroup\tSize\tDmax\tStat\tNmin" << endl;

  for (size_t k = 0; k < nrep; k++)
  {
    ApplicationTools::displayGauge(k, nrep-1, '>');
    unique_ptr<SiteContainer> sites(simulator.simulate(sizeOfDataSet));
    drtl.setData(*sites);
    drtl.initialize();
    unique_ptr<ProbabilisticSubstitutionMapping> mapping(SubstitutionMappingTools::computeSubstitutionVectors(drtl, nijt, false));
    size_t nbBranches = mapping->getNumberOfBranches();
    
    //Mean vector:
    vector<double> meanVector(nbBranches);
    for (size_t j = 0; j < nbBranches; j++)
    {
		  double sum = 0;
      for (size_t i = 0; i < sizeOfDataSet; i++)
        for (size_t t = 0; t < nbTypes; t++)
          sum += (*mapping)(j, i, t);
      meanVector[j] = sum / static_cast<double>(sizeOfDataSet);
    }

    //Compute distance matrix:
	  unique_ptr<DistanceMatrix> mat(new DistanceMatrix(siteNames));
	  for (size_t i = 0; i < sizeOfDataSet; ++i)
    {
		  (*mat)(i, i) = 0.;
      VVdouble* vec = &((*mapping)[i]);
		  for (size_t j = 0; j < i; ++j)
      {
			  (*mat)(i,j) = (*mat)(j,i) = distance.getDistanceForPair(*vec, (*mapping)[j]);
		  }
	  }

    //Perform clustering:
    try
    {
      dynamic_cast<SumClustering&>(clustering).setMapping(*mapping);
    }
    catch(exception& e) {}
    
    clustering.setDistanceMatrix(*mat);
    clustering.computeTree();
    TreeTemplate<Node> clusteringTree(*clustering.getTree());

    //Add information to tree:
    computeNormProperties(clusteringTree, *mapping);
    distance.setStatisticAsProperty(*clusteringTree.getRootNode(), *mapping);
    
    //Now parse each group:
    vector<Group> groups = getGroups(&clusteringTree);
    for (size_t i = 0; i < groups.size(); i++)
    {
      Group* group = &groups[i];
      if (group->size() > maxGroupSize) continue;
      
      //// Compute distance from mean vector:
      //vector<double> groupMeanVector(mapping->getNumberOfBranches(), 0.);
      //for(size_t j = 0; j < group->size(); j++)
      //{
      //  groupMeanVector += (*mapping)[TextTools::to<size_t>((*group)[j])]; 
      //}
      //double distFromMeanVector = distance.getDistanceForPair(groupMeanVector/group->size(), meanVector);
      
      if (out) *out << k
        << "\t" << group->toString()
        << "\t" << group->size()
        << "\t" << (group->getHeight() * 2.)
        << "\t" << (dynamic_cast<const Number<double> *>(group->getProperty("Stat"))->getValue())
        << "\t" << (dynamic_cast<const Number<double> *>(group->getProperty("Nmin"))->getValue())
        //<< "\t" << distFromMeanVector
        << endl;
    }
  }
  ApplicationTools::message->endLine();
}