cofounderella/lib/utils/math.dart

299 lines
8.8 KiB
Dart
Raw Permalink Normal View History

import 'dart:math';
2024-06-12 00:39:29 +02:00
import '../enumerations.dart';
2024-06-24 04:25:23 +02:00
import '../models/language.dart';
2024-05-30 16:37:34 +02:00
import '../models/user_profile.dart';
/// Approximate determination of age
int calcAge(int? birthYear) {
if (birthYear != null) {
return (DateTime.now().year - birthYear);
}
return 0;
}
/// Returns the approximate age in parentheses,
/// or an empty string if [birthYear] is the current year or null.
String ageInfo(int? birthYear) {
int age = calcAge(birthYear);
String ageInfo = age > 0 ? '($age)' : '';
return ageInfo;
}
/// Convert decimal coordinate to degrees minutes seconds (DMS).
String convertDecimalToDMS(double decimalValue, {required bool isLatitude}) {
bool isNegative = decimalValue < 0;
double absoluteValue = decimalValue.abs();
int degrees = absoluteValue.toInt();
double minutesDecimal = (absoluteValue - degrees) * 60;
int minutes = minutesDecimal.toInt();
double secondsDecimal = (minutesDecimal - minutes) * 60;
double seconds = double.parse(secondsDecimal.toStringAsFixed(2));
String direction;
if (isLatitude) {
direction = isNegative ? 'S' : 'N';
} else {
direction = isNegative ? 'W' : 'E';
}
// return formatted string
return '${degrees.abs()}° ${minutes.abs()}\' ${seconds.abs().toStringAsFixed(2)}" $direction';
}
/// Distance in kilometers between two location coordinates
double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
const R = 6371; // earth radius in kilometers
// Haversine formula to get distance between latitudes and longitudes
// https://en.wikipedia.org/wiki/Haversine_formula
final dLat = _degreesToRadians(lat2 - lat1);
final dLon = _degreesToRadians(lon2 - lon1);
final a = sin(dLat / 2) * sin(dLat / 2) +
cos(_degreesToRadians(lat1)) *
cos(_degreesToRadians(lat2)) *
sin(dLon / 2) *
sin(dLon / 2);
final c = 2 * atan2(sqrt(a), sqrt(1 - a));
return R * c;
}
double _degreesToRadians(double degrees) {
return degrees * pi / 180;
}
2024-05-30 16:37:34 +02:00
/// Shortest distance between two users locations
double shortestDistanceBetweenUsers(
UserProfile currentUser, UserProfile otherUser) {
try {
if (currentUser.locations.isEmpty || otherUser.locations.isEmpty) {
return double.nan;
}
double shortestDistance = double.nan;
// locations currentUser
for (var loc1 in currentUser.locations.values) {
if (loc1 != null && loc1.latitude != null && loc1.longitude != null) {
for (var loc2 in otherUser.locations.values) {
if (loc2 != null && loc2.latitude != null && loc2.longitude != null) {
double distance = calculateDistance(loc1.latitude!, loc1.longitude!,
loc2.latitude!, loc2.longitude!);
if (shortestDistance.isNaN || distance < shortestDistance) {
shortestDistance = distance;
}
}
}
}
}
return shortestDistance;
} catch (e) {
return double.nan;
}
}
2024-06-12 00:39:29 +02:00
/// Calculates the matching score of [otherUser] for [currentUser].
double calculateMatchScore(UserProfile currentUser, UserProfile otherUser) {
// Score on locations distance
double distance = shortestDistanceBetweenUsers(currentUser, otherUser);
2024-06-24 04:25:23 +02:00
double distanceScore = _distanceToPoints(distance);
// Score on common languages
List<Language> currentLang = currentUser.languages;
List<Language> otherLang = otherUser.languages;
int matchingLanguages =
currentLang.toSet().intersection(otherLang.toSet()).length;
int langScore = 0;
if (matchingLanguages >= 3) {
langScore = 10;
} else if (matchingLanguages == 2) {
langScore = 9;
} else if (matchingLanguages == 1) {
langScore = 8;
}
// Score on common Sectors
List<SectorOption> currentSectors = currentUser.sectors;
List<SectorOption> otherSectors = otherUser.sectors;
int matchingSectors =
currentSectors.toSet().intersection(otherSectors.toSet()).length;
int sectorScore = 0;
if (matchingSectors >= 4) {
sectorScore = 10;
} else if (matchingSectors == 3) {
sectorScore = 9;
} else if (matchingSectors == 2) {
sectorScore = 7;
} else if (matchingSectors == 1) {
sectorScore = 5;
}
2024-06-12 00:39:29 +02:00
// Score on skills
int matchingSkillsSought = currentUser.skillsSought
.toSet()
.intersection(otherUser.skills.toSet())
.length;
2024-06-24 04:25:23 +02:00
int matchingSkillsOffered = currentUser.skills
2024-06-12 00:39:29 +02:00
.toSet()
2024-06-24 04:25:23 +02:00
.intersection(otherUser.skillsSought.toSet())
2024-06-12 00:39:29 +02:00
.length;
int skillsOffered = otherUser.skills.length;
2024-06-24 04:25:23 +02:00
int scoreSought = 0;
if (matchingSkillsSought >= 3) {
scoreSought = 15;
} else if (matchingSkillsSought == 2) {
scoreSought = 12;
} else if (matchingSkillsSought == 1) {
scoreSought = 9;
2024-06-12 00:39:29 +02:00
}
double valueOffered = matchingSkillsOffered / skillsOffered;
int scoreOffered;
if (valueOffered == 1) {
2024-06-24 04:25:23 +02:00
scoreOffered = 15;
} else if (valueOffered == 4 / 5) {
scoreOffered = 14;
} else if (valueOffered == 3 / 4) {
scoreOffered = 13;
2024-06-12 00:39:29 +02:00
} else if (valueOffered == 2 / 3) {
2024-06-24 04:25:23 +02:00
scoreOffered = 12;
} else if (valueOffered == 3 / 5) {
scoreOffered = 11;
2024-06-12 00:39:29 +02:00
} else if (valueOffered == 1 / 2) {
2024-06-24 04:25:23 +02:00
scoreOffered = 10;
} else if (valueOffered == 2 / 5) {
scoreOffered = 9;
2024-06-12 00:39:29 +02:00
} else if (valueOffered == 1 / 3) {
2024-06-24 04:25:23 +02:00
scoreOffered = 8;
} else if (valueOffered == 1 / 4) {
scoreOffered = 7;
} else if (valueOffered == 1 / 5) {
scoreOffered = 6;
2024-06-12 00:39:29 +02:00
} else {
scoreOffered = 0;
}
2024-06-24 04:25:23 +02:00
double skillsScore = (scoreSought + scoreOffered) / 1.5;
2024-06-12 00:39:29 +02:00
// Score on availability
int availabilityScore;
AvailabilityOption currentAvail = currentUser.availability;
AvailabilityOption otherAvail = otherUser.availability;
2024-06-24 04:25:23 +02:00
if (currentAvail != otherAvail &&
(currentAvail == AvailabilityOption.flexible ||
otherAvail == AvailabilityOption.flexible)) {
availabilityScore = 4;
2024-06-12 00:39:29 +02:00
} else {
int availabilityDifference = (currentAvail.index - otherAvail.index).abs();
2024-06-24 04:25:23 +02:00
// map possible values [0 1 2 3] to points {0 1 3 5]
availabilityScore = (3 - availabilityDifference) * 2 - 1;
2024-06-12 00:39:29 +02:00
}
// Score on Vision
List<VisionOption> currentVisions = currentUser.visions;
List<VisionOption> otherVisions = otherUser.visions;
int matchingVisions =
currentVisions.toSet().intersection(otherVisions.toSet()).length;
int visionScore;
if (matchingVisions == 4 ||
((currentVisions.length == otherVisions.length) &&
(matchingVisions == currentVisions.length))) {
visionScore = 4; // full match, max score
} else if (matchingVisions > 1) {
visionScore = 3; // some match
} else if (matchingVisions == 1) {
visionScore = 2; // one match
} else {
visionScore = 0; // no match
}
// Score on WorkValues
List<WorkValueOption> currentWorks = currentUser.workValues;
List<WorkValueOption> otherWorks = otherUser.workValues;
int matchingWorks =
currentWorks.toSet().intersection(otherWorks.toSet()).length;
int workScore;
if (matchingWorks == 2 ||
((currentWorks.length == otherWorks.length) && (matchingWorks == 1))) {
workScore = 4; // full match, max score
} else if (matchingWorks == 1) {
workScore = 2; // semi match
} else {
workScore = 0; // no match
}
// Score on Risk
2024-06-24 04:25:23 +02:00
RiskTolerance currentRisk = currentUser.risk;
RiskTolerance otherRisk = currentUser.risk;
2024-06-12 00:39:29 +02:00
int riskScore = 0;
if (currentUser.risk == otherUser.risk) {
2024-06-24 04:25:23 +02:00
riskScore = 3;
} else {
int riskDifference = (currentRisk.index - otherRisk.index).abs();
riskScore = 3 - riskDifference;
2024-06-12 00:39:29 +02:00
}
// Score on CorporateCulture
int cultureScore = 0;
if (currentUser.culture == otherUser.culture) {
2024-06-24 04:25:23 +02:00
cultureScore = 2;
2024-06-12 00:39:29 +02:00
}
// Score on Communication
int communicationScore = 0;
if (currentUser.communication == otherUser.communication) {
communicationScore = 1;
}
// Calc total score. Sum of each score divided by its own max score value,
// multiplied with its own weight.
2024-06-24 04:25:23 +02:00
double totalScore = distanceScore +
langScore +
sectorScore +
skillsScore +
availabilityScore +
riskScore +
visionScore +
workScore +
cultureScore +
communicationScore;
return min(totalScore, 99.9);
2024-06-12 00:39:29 +02:00
}
2024-06-24 04:25:23 +02:00
double _distanceToPoints(double distance) {
2024-06-12 00:39:29 +02:00
// Self predefined data points
2024-06-24 04:25:23 +02:00
final List<double> distances = [0, 10, 100, 200, 500, 700, 1000];
final List<double> points = [60, 55, 40, 20, 10, 5, 0];
2024-06-12 00:39:29 +02:00
if (distance.isNaN || distance >= distances.last) {
2024-06-24 04:25:23 +02:00
return points.last;
2024-06-12 00:39:29 +02:00
} else if (distance <= distances[0]) {
2024-06-24 04:25:23 +02:00
return points[0];
2024-06-12 00:39:29 +02:00
}
// Linear interpolation
for (int i = 1; i < distances.length; i++) {
if (distances[i] >= distance) {
double x0 = distances[i - 1];
2024-06-24 04:25:23 +02:00
double y0 = points[i - 1];
2024-06-12 00:39:29 +02:00
double x1 = distances[i];
2024-06-24 04:25:23 +02:00
double y1 = points[i];
2024-06-12 00:39:29 +02:00
// Interpolate
2024-06-24 04:25:23 +02:00
double result = y0 + (y1 - y0) * (distance - x0) / (x1 - x0);
return result;
2024-06-12 00:39:29 +02:00
}
}
// Fallback return value, though code should never reach here
2024-06-24 04:25:23 +02:00
return points.last; // 50
2024-06-12 00:39:29 +02:00
}