<commit_before>/*
 * SilhouetteValue.cpp
 *
 *  Created on: Jan 15, 2018
 *      Author: <NAME>
 */

#include "SilhouetteValue.h"

using namespace std;
using namespace lemon;

namespace ysk{

//TODO: Experimental Stuff, makes 0 sense right now as edges are not a distance function

	SilhouetteValue::~SilhouetteValue(){};

	double SilhouetteValue::getValue(){
		//Calculate average dissimilarities to own clusters (a(i))
		map<int,double> _averageDissimilaritiesOwn = map<int,double>();
		for(auto const &cluster : _solution){
			for (auto const &node : cluster){
				double dissim = 1.0/cluster.size();
				//Sum Factor
				double sum = 0.0;
				for (auto const &otherNode : cluster){
					if (node == otherNode) continue; //Not of interest
					FullGraph::Edge edge = _instance->getOrig().findEdge(
							_instance->getOrig().nodeFromId(node),
							_instance->getOrig().nodeFromId(otherNode),
					INVALID);
					sum += _instance->getWeight(edge);
				}
				//cout << dissim << " " << sum << endl;
				dissim *= sum;
				_averageDissimilaritiesOwn[node]=dissim;

			}
		}

//		if (verbosity > 5){
//			cout << "Average Dissimilarities to own clusters" << endl << endl;
//			for (auto const &cluster : _solution){
//				for (auto const &node: cluster){
//					cout << node << " : " << _averageDissimilaritiesOwn[node]<< endl;
//				}
//			}
//		}

		//Calculate average dissimilarities to other clusters (b(i))
		map<int,double> _averageDissimilaritiesForeign= map<int,double>();
		for(auto const &cluster : _solution){
			for (auto const &node : cluster){
				double dissim = 1.0/(_instance->getSize()-cluster.size());
				//Sum Factor
				double sum = 0.0;
				for (auto const &otherCluster : _solution){
					for (auto const &otherNode : otherCluster){
						FullGraph::Edge edge = _instance->getOrig().findEdge(
								_instance->getOrig().nodeFromId(node),
								_instance->getOrig().nodeFromId(otherNode),
						INVALID);
						sum += _instance->getWeight(edge);
					}
				}
				dissim *= sum;
				_averageDissimilaritiesForeign[node]=dissim;

			}
		}

//		if (verbosity > 4){
//			cout << "Average Dissimilarities to other clusters" << endl << endl;
//			for (auto const &cluster : _solution){
//				for (auto const &node: cluster){
//					cout << node << " : " << _averageDissimilaritiesForeign[node]<< endl;
//				}
//			}
//		}

		//Calculate silhouettes
		map<int,double> _silhouettes= map<int,double>();
		for (auto const &cluster: _solution){
			for (auto const &node : cluster){
				_silhouettes[node] =
						(1.0/_instance->getSize())*
						(
							(_averageDissimilaritiesForeign[node] -_averageDissimilaritiesOwn[node])
							/
							(std::max(_averageDissimilaritiesForeign[node],_averageDissimilaritiesOwn[node]))
						);
			}
		}

		//Calculate silhouette value

		double factor = 1.0/_instance->getSize();
		int sum = 0.0;

		for (auto const &cluster : _solution){
			for (auto const &node : cluster){
				sum += _silhouettes[node];
			}
		}

		return factor*sum;
	}
}<commit_msg>Some tweaks for silhouette value calculation and some really basic distance transformations<commit_after>/*
 * SilhouetteValue.cpp
 *
 *  Created on: Jan 15, 2018
 *      Author: <NAME>
 */

#include "SilhouetteValue.h"

using namespace std;
using namespace lemon;

namespace ysk{

//TODO: Experimental Stuff, makes 0 sense right now as edges are not a distance function

	SilhouetteValue::~SilhouetteValue(){};

	double SilhouetteValue::getValue(){


		//Treat negative values as offsets OR zeros?
		const bool negativeToZero = true;
		const bool negativeOffset = true;

		double minimumEdgeWeight = 0;

		if (negativeOffset){
			for (FullGraph::EdgeIt it(_instance->getOrig()); it != INVALID; ++it){
				if (_instance->getWeight(it) < minimumEdgeWeight){
					minimumEdgeWeight = _instance->getWeight(it);
				}
			}
			if (verbosity > 4) cout << "Minimum edge weight: " << minimumEdgeWeight <<endl;
		}

		//Calculate average dissimilarities to own clusters (a(i))
		map<int,double> _averageDissimilaritiesOwn = map<int,double>();
		for(auto const &cluster : _solution){
			for (auto const &node : cluster){
				double dissim = 1.0/cluster.size();
				//Sum Factor
				double sum = 0.0;
				for (auto const &otherNode : cluster){
					if (node == otherNode) continue; //Not of interest
					FullGraph::Edge edge = _instance->getOrig().findEdge(
							_instance->getOrig().nodeFromId(node),
							_instance->getOrig().nodeFromId(otherNode),
					INVALID);
					double edgeWeight = negativeOffset ? _instance->getWeight(edge) - minimumEdgeWeight : _instance->getWeight(edge) ;
					if (negativeToZero){
						sum += edgeWeight > 0 ? edgeWeight : 0.0;
					}
					else{
						sum += edgeWeight;
					}
				}
				//cout << dissim << " " << sum << endl;
				dissim *= sum;
				_averageDissimilaritiesOwn[node]=dissim;

			}
		}

//		if (verbosity > 5){
//			cout << "Average Dissimilarities to own clusters" << endl << endl;
//			for (auto const &cluster : _solution){
//				for (auto const &node: cluster){
//					cout << node << " : " << _averageDissimilaritiesOwn[node]<< endl;
//				}
//			}
//		}

		//Calculate average dissimilarities to other clusters (b(i))
		map<int,double> _averageDissimilaritiesForeign= map<int,double>();
		for(auto const &cluster : _solution){
			for (auto const &node : cluster){
				double dissim = 1.0/(_instance->getSize()-cluster.size());
				//Sum Factor
				double sum = 0.0;
				for (auto const &otherCluster : _solution){
					for (auto const &otherNode : otherCluster){
						FullGraph::Edge edge = _instance->getOrig().findEdge(
								_instance->getOrig().nodeFromId(node),
								_instance->getOrig().nodeFromId(otherNode),
						INVALID);
						double edgeWeight = negativeOffset ? _instance->getWeight(edge)- minimumEdgeWeight: _instance->getWeight(edge) ;
						if (negativeToZero){
							sum += edgeWeight > 0 ? edgeWeight : 0.0;
						}
						else{
							sum += edgeWeight;
						}
					}
				}
				dissim *= sum;
				_averageDissimilaritiesForeign[node]=dissim;

			}
		}

//		if (verbosity > 4){
//			cout << "Average Dissimilarities to other clusters" << endl << endl;
//			for (auto const &cluster : _solution){
//				for (auto const &node: cluster){
//					cout << node << " : " << _averageDissimilaritiesForeign[node]<< endl;
//				}
//			}
//		}

		//Calculate silhouettes
		map<int,double> _silhouettes= map<int,double>();
		for (auto const &cluster: _solution){
			for (auto const &node : cluster){
				_silhouettes[node] =
						(1.0/_instance->getSize())*
						(
							(_averageDissimilaritiesForeign[node] -_averageDissimilaritiesOwn[node])
							/
							(std::max(_averageDissimilaritiesForeign[node],_averageDissimilaritiesOwn[node]))
						);
			}
		}

		//Calculate silhouette value

		double factor = 1.0/_instance->getSize();
		double sum = 0.0;

		for (auto const &cluster : _solution){
			for (auto const &node : cluster){
				sum += _silhouettes[node];
			}
		}

		return factor*sum;
	}
}
{'fraction_non_alphanumeric': 0.14422801489544543, 'fraction_numerical': 0.007734173589229447, 'mean_word_length': 3.144213649851632, 'pattern_counts': {'":': 0, '<': 96, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '348093', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2240, 'n_words': 543}