siafun
Version:
A collection of structure induction algorithms
90 lines (89 loc) • 4.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
var HEURISTICS;
(function (HEURISTICS) {
/**number of points in pattern*/
HEURISTICS.POINT_COUNT = function (pattern, allPoints) {
return pattern.points.length;
};
/**proportion of points in composition covered by pattern*/
HEURISTICS.COVERAGE = function (pattern, allPoints) {
var stringOcc = pattern.occurrences.map(occ => occ.map(p => JSON.stringify(p)));
var uniquePoints = _.uniq(_.flatten(stringOcc));
return uniquePoints.length / allPoints.length;
};
/**proportion of points in pattern bounding box involved in pattern*/
HEURISTICS.COMPACTNESS = function (pattern, allPoints) {
return pattern.points.length
/ getPointsInBoundingBox(pattern.points, allPoints).length;
};
/**proportion of points in pattern bounding box involved in pattern*/
HEURISTICS.SIZE_AND_COMPACTNESS = function (pattern, allPoints) {
return Math.pow(pattern.points.length, 1.8)
/ getPointsInBoundingBox(pattern.points, allPoints).length;
};
HEURISTICS.SIZE_AND_1D_COMPACTNESS = function (dimIndex, power = 1.8) {
return (pattern, allPoints) => Math.pow(pattern.points.length, power)
/ getPointsInBoundingBox(pattern.points, allPoints, dimIndex).length;
};
HEURISTICS.SIZE_1D_COMPACTNESS_AND_REGULARITY = function (dimIndex, power = 1.8) {
return (pattern, allPoints) => Math.pow(pattern.points.length, power)
* getVectorsRegularity(pattern.vectors, dimIndex)
/ getPointsInBoundingBox(pattern.points, allPoints, dimIndex).length;
};
HEURISTICS.SIZE_AND_1D_COMPACTNESS_AXIS = function (dimIndex, power = 1.8) {
return (pattern, allPoints) => Math.pow(pattern.points.length, power)
/ getPointsInBoundingBox(pattern.points, allPoints, dimIndex).length
* getAxisStrictParallelism(pattern.vectors, dimIndex);
};
HEURISTICS.SIZE_AND_1D_COMPACTNESS_AXIS2 = function (dimIndex, power = 1.8) {
return (pattern, allPoints) => Math.pow(pattern.points.length, power)
/ getPointsInBoundingBox(pattern.points, allPoints, dimIndex).length
* getAxisParallelism(pattern.vectors, dimIndex);
};
HEURISTICS.SIZE_AND_1D_COMPACTNESS_NOAXIS = function (dimIndex, power = 1.8) {
return (pattern, allPoints) => Math.pow(pattern.points.length, power)
/ getPointsInBoundingBox(pattern.points, allPoints, dimIndex).length
* getAxisNonParallelism(pattern.vectors, dimIndex);
};
//avg regularity of dimIndex components of vectors with non-0 value at dimIndex
//0 = irregular, > 0 more regular
function getVectorsRegularity(vectors, dimIndex) {
const nonZero = vectors.filter(v => v[dimIndex]);
if (nonZero.length > 0) {
return _.sum(nonZero.map(v =>
//% 4 => 2, %2 => 1, none => 0
(v[dimIndex] % 2 == 0 ? 1 : 0) * (v[dimIndex] % 4 == 0 ? 2 : 1))) / nonZero.length;
}
return 0;
}
HEURISTICS.getVectorsRegularity = getVectorsRegularity;
function getVectorsRegularityDefectiveButGoodForJohan(vectors, dimIndex) {
return _.reduce(vectors.map(v => (v[dimIndex] % 2 == 0 ? 2 : 1) * (v[dimIndex] % 4 == 0 ? 2 : 1)), _.multiply);
}
//1 if all vectors are parallel to the axis of the given dimension, 0 otherwise
function getAxisStrictParallelism(vectors, dimIndex) {
return _.reduce(vectors.map(v => v.every((d, i) => i == dimIndex || d == 0) ? 1 : 0), _.multiply);
}
HEURISTICS.getAxisStrictParallelism = getAxisStrictParallelism;
//proportion of vectors that are parallel to the axis of the given dimension
function getAxisParallelism(vectors, dimIndex) {
return _.mean(vectors.map(v => v.every((d, i) => i == dimIndex || d == 0) ? 1 : 0));
}
HEURISTICS.getAxisParallelism = getAxisParallelism;
//1 if not all vectors are parallel to the axis of the given dimension, 0 otherwise
function getAxisNonParallelism(vectors, dimIndex) {
return 1 - getAxisParallelism(vectors, dimIndex);
}
HEURISTICS.getAxisNonParallelism = getAxisNonParallelism;
function getPointsInBoundingBox(pattern, allPoints, dimIndex) {
var maxes = _.zip(...pattern).map(c => _.max(c));
var mins = _.zip(...pattern).map(c => _.min(c));
if (dimIndex != null) {
return allPoints.filter(p => mins[dimIndex] <= p[dimIndex] && p[dimIndex] <= maxes[dimIndex]);
}
return allPoints.filter(p => p.every((e, i) => mins[i] <= e && e <= maxes[i]));
}
HEURISTICS.getPointsInBoundingBox = getPointsInBoundingBox;
})(HEURISTICS = exports.HEURISTICS || (exports.HEURISTICS = {}));