// Friends: Arithmetic (Cluster and Point) const Cluster operator+(const Cluster &c, const Point &p) { Cluster added; added.__cpy(c.__points); added.add(p); return added; }
/* Adds the data point to the space. Attempts to add the point to a * pre-existing cluster, and if it is nearby to multiple clusters, it * will merge those clusters. If it took more than the user defined * number of points to be searched, the cells will be divided into * smaller ones. */ long add(DataPoint<dims, T> &point) { auto p = make_shared<DataPoint<dims, T>>(point); auto key = to_lattice(point.vec); std::unordered_set<Cluster<dims, T>*> found = find_clusters(&point); Cluster<dims, T>* cluster = new Cluster<dims, T>(cluster_num++); switch (found.size()) { case 0: cluster->add(p); p->cluster = cluster; clusters[cluster->id] = cluster; break; case 1: delete cluster; cluster_num--; (*found.begin())->add(p); p->cluster = (*found.begin()); break; default: for (auto it = found.begin(); it != found.end(); it++) { for (auto pt : (*it)->points) { cluster->add(pt); pt->cluster = cluster; } clusters.erase(clusters.find((*it)->id)); delete *it; } p->cluster = cluster; cluster->add(p); clusters[cluster->id] = cluster; } cells[key].add(p); if (last_search_count > search_count_target && actual_cell_side >= 2*cell_side_length) { std::cout << "Search count: " << last_search_count; std::cout << " - Target: " << search_count_target; std::cout << " - Side: " << actual_cell_side; std::cout << " - Dividing..." << std::endl; divide_grid(); } return last_search_count; }
// Containment void test_cluster_contain(ErrorContext &ec, unsigned int numRuns) { bool pass; // Run at least once!! assert(numRuns > 0); ec.DESC("--- Test - Cluster - Containment ---"); for (int run = 0; run < numRuns; run++) { ec.DESC("cluster with one point"); { Point p(10); p[0] = p[2] = p[4] = p[8] = 6.705; Cluster c; c.add(p); pass = c.contains(p); ec.result(pass); } ec.DESC("cluster with several points"); { Point p(10); p[0] = p[2] = p[4] = p[8] = 6.705; Cluster c; for (int i = 0; i < 10; i ++) { Point pp(10); for (int j = 0; j < 10; j ++) { pp[i] = 3.4 + i * 2.1 + i * i; } c.add(pp); } c.add(p); pass = c.contains(p); ec.result(pass); } } }
// subscript (operator[]) void test_cluster_subscript(ErrorContext &ec, unsigned int numRuns) { bool pass; // Run at least once!! assert(numRuns > 0); ec.DESC("--- Test - Cluster - Subscript ---"); for (int run = 0; run < numRuns; run++) { ec.DESC("cluster with one point"); { Cluster c; Point p(10); p[5] = 3.14; c.add(p); Point p1 = c[0]; pass = p1[5] == 3.14; ec.result(pass); } ec.DESC("cluster with several point"); { Cluster c; for (int i = 0; i < 10; i ++) { Point p(10); p[5] = 3.14; c.add(p); } pass = true; for (int i = 0; i < 10; i ++) { Point p1 = c[i]; pass = pass && (p1[5] == 3.14); } ec.result(pass); } } }
// Friends: Arithmetic (two Clusters) const Cluster operator+(const Cluster &one, const Cluster &two) // union { Cluster allTogether; allTogether.__cpy(one.__points); int size = two.getSize(); // This isn't going to work, it will pass the first point and nothing more. allTogether.add(two.__points->point); }
/* * make sure compute clustering score works, debug it * * point ID generation doesn't work. Has something to do with the way they're being initialized in operator>> of Cluster, I think. * - and + functions create a new cluster, which increments the cluster ID generator... Cluster cont working on fwd listing change centroid to Point instead of PointPtr??? this returns NULL if points is empty. That would be a problem for the move.perfrom func, I'm sure. test: void pickPoints(int &, Point*&); // selects points from point_space cluster double intraClusterDistance(); // inner distance of a single cluster void pickPoints(int &, Point*&); // selects points from point_space cluste double interClusterDistance(const Cluster &lhs, const Cluster &rhs) Reimplement operator+(C, P) and operator-(C, P) to take a const Point & instead of PointPtr. Reuse already implemented operators to get a very short and straightforward implementation Point Reteset all these for Point class: std::fstream seed; seed.open("C:\\Users\\Folio\\Desktop\\School\\intPA2\\ucd-csi2312-pa2\\numbers.csv"); seed.clear(); for(int i = 0; i < 6; i++) { double num = rand() % 100; seed << num; seed << ","; num = rand() % 100; seed << num; seed << ","; num = rand() % 100; seed << num; seed << ","; num = rand() % 100; seed << num; seed << ","; num = rand() % 100; seed << num; seed << std::endl; } seed.close(); std::ifstream csv; csv.open("C:\\Users\\Folio\\Desktop\\School\\intPA2\\ucd-csi2312-pa2\\numbers.csv", std::ifstream::in); Point >> operator Point<T>& operator[](int); // done // Overloaded operators CLUSTER // IO friend std::ostream &operator<<(std::ostream &, const Cluster &); friend std::istream &operator>>(std::ifstream &, Cluster &); friend double interClusterDistance(const Cluster &lhs, const Cluster &rhs); double intraClusterDistance(); // inner distance of a single cluster void pickPoints(int &, Point<T>*&); // selects points from point_space cluster double getClusterEdges(); // cluster edges of one cluster, the calling obj // do last Cluster &operator-=(const Cluster &rhs); // (asymmetric) difference Cluster &operator+=(const Cluster &rhs); // union Cluster &operator+=(const Point<T> &rhs); // add point Cluster &operator-=(const Point<T> &rhs); // remove point // Set-destructive operators (duplicate points in the space) // - Friends friend const Cluster operator+(const Cluster &lhs, const Cluster &rhs); //done friend const Cluster operator-(const Cluster &lhs, const Cluster &rhs); friend const Cluster operator+(const Cluster &lhs, const Point<T> &rhs); // c2 = c1 + p1 // done friend const Cluster operator-(const Cluster &lhs, const Point<T> &rhs); // c2 = c1 - p1 // done //end do last friend double interClusterEdges(const Cluster &, const Cluster &); //CENTROID void calcCent(); void setCent(Point<T> &); */ int main (){ Cluster<double, 5> c; Point<double> p(5); Cluster<double, 5> d; Cluster<double, 5>::Move m; c.add(p); c.remove(p); c.add(p); cout << c[0] << endl; cout << p; //(const Point<T> &pt, Cluster *to, Cluster *from) m.perform(p, d, c); d.calcCent(); cout << d; return 0; }
// ascending pseudo-lexicographic order void test_cluster_order(ErrorContext &ec, unsigned int numRuns) { bool pass; // Run at least once!! assert(numRuns > 0); ec.DESC("--- Test - Cluster - Order ---"); for (int run = 0; run < numRuns; run++) { ec.DESC("points in a cluster are sorted"); { Point p1(5), p2(5), p3(5), p4(5), p5(5); p1[0] = 1; p2[1] = 1; p3[2] = 1; p4[3] = 1; p5[4] = 1; Cluster c; c.add(p1); c.add(p2); c.add(p4); c.add(p3); c.add(p5); pass = (c[0] == p5) && (c[1] == p4) && (c[2] == p3) && (c[3] == p2) && (c[4] == p1); if (!pass) { std::cout << std::endl; std::cout << c << std::endl; std::cout << std::endl; } ec.result(pass); } ec.DESC("ascending pseudo-lexicographic order"); { Point p1(5), p2(5), p3(5), p4(5), p5(5); p1[0] = 1; p2[1] = 1; p3[2] = -1; p4[3] = 1; p5[4] = -1; Cluster c; c.add(p1); c.add(p2); c.add(p4); c.add(p3); c.add(p5); pass = (c[0] == p3) && (c[1] == p5) && (c[2] == p4) && (c[3] == p2) && (c[4] == p1); if (!pass) { std::cout << std::endl; std::cout << c << std::endl; std::cout << std::endl; } ec.result(pass); } } }
const Cluster operator+(const Cluster &lhs, const PointPtr &rhs){ Cluster tempCluster = lhs; tempCluster.add(rhs); return tempCluster; }