You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

218 lines
6.2 KiB

<?php
declare(strict_types=1);
/**
* CentroidDistanceStrategy.php
* Created by PhpStorm.
* Date: 2026/10/17
* Time: 09:40
* Author: qian.guo
* E-mail: yiyezhiqiutel@gmail.com
*/
namespace app\service\score\strategy;
use app\service\score\AbstractScoreStrategy;
use app\service\score\ScoreContext;
/**
* 原型向量距离制
* 设计说明:
* - 把作答结果压成一条轴向量(每轴取首极占比 0..1),再与各类型原型向量算欧氏距离,取最近者
* - 与前三者的本质区别:它不是逐轴独立判定
* 累加制可能给出「E 略胜、S 略胜、F 明显胜、J 略胜」这种三轴都勉强的组合
* 而原型距离会倾向落在整体更自洽的类型上,适合答复「我好像四轴都不明显」的用户
* 代价是可解释性差,且强依赖原型向量的设定质量
* - 依赖 types[*].centroid,长度必须等于轴数。缺失时回退到逐轴判定,不抛异常
* 这是一条可降级的可选策略,不该因为没配原型向量就让用户答完题拿不到结果
*/
class CentroidDistanceStrategy extends AbstractScoreStrategy
{
// 最近若干候选,仅供调试输出
private $candidate_list = [];
/**
* 获取策略标识
* getName
* @return string
*/
public function getName()
{
return 'centroid_distance';
}
/**
* 获取策略版本
* getVersion
* @return string
*/
public function getVersion()
{
return '1.0.0';
}
/**
* 取最近原型的类型码
* resolveTypeCode
* @param array $axis_result 各轴判定结果
* @param ScoreContext $context 评分输入
* @return string
*/
protected function resolveTypeCode($axis_result, ScoreContext $context)
{
$axis_count = $context->getAxisCount();
$usable_list = [];
foreach ($context->type_list as $type_code => $type_info) {
if (empty($type_info['centroid'])) {
continue;
}
if (count($type_info['centroid']) !== $axis_count) {
continue;
}
$usable_list[$type_code] = $type_info;
}
$this->candidate_list = [];
// 无可用原型向量:降级为逐轴判定,并在调试里留痕
if (empty($usable_list)) {
return parent::resolveTypeCode($axis_result, $context);
}
$vector = $this->getUserVector($axis_result, $context);
$scored_list = [];
foreach ($usable_list as $type_code => $type_info) {
$sum = 0.0;
for ($i = 0; $i < $axis_count; $i++) {
$delta = $vector[$i] - (float) $type_info['centroid'][$i];
$sum += $delta * $delta;
}
$item = [];
$item['code'] = (string) $type_code;
$item['distance'] = sqrt($sum);
$scored_list[] = $item;
}
// 距离升序、类型码升序兜底,保证同分时结果稳定可复现
usort($scored_list, [$this, 'compareCandidate']);
$this->candidate_list = array_slice($scored_list, 0, 3);
return $scored_list[0]['code'];
}
/**
* 候选排序比较
* compareCandidate
* @param array $left 左项
* @param array $right 右项
* @return int
*/
private function compareCandidate($left, $right)
{
if ($left['distance'] < $right['distance']) {
return -1;
}
if ($left['distance'] > $right['distance']) {
return 1;
}
if ($left['code'] < $right['code']) {
return -1;
}
if ($left['code'] > $right['code']) {
return 1;
}
return 0;
}
/**
* 取用户轴向量
* getUserVector
* @param array $axis_result 各轴判定结果
* @param ScoreContext $context 评分输入
* @return array 每轴取首极占比,0.5 表示完全对半
*/
private function getUserVector($axis_result, ScoreContext $context)
{
$vector = [];
foreach ($context->axis_list as $axis) {
$axis_info = $axis_result[$axis['code']];
$a_pct = (float) $axis_info['a_pct'];
$b_pct = (float) $axis_info['b_pct'];
$total = $a_pct + $b_pct;
$value = 0.5;
if ($total > 0) {
$value = $a_pct / $total;
}
$vector[] = $value;
}
return $vector;
}
/**
* 把各轴主导极对齐到最近原型
* alignAxesToType
* @param array $axis_result 各轴判定结果
* @param string $type_code 类型码
* @param ScoreContext $context 评分输入
* @return array
*
* 不对齐会出现「类型码是 INFP 但 axes 里 E 取胜」的自相矛盾结果
* a_pct / b_pct / diff 保持实测值不动 —— 那是测量结果,只有最终判定按原型走
*/
protected function alignAxesToType($axis_result, $type_code, ScoreContext $context)
{
$index = 0;
foreach ($context->axis_list as $axis) {
$axis_code = $axis['code'];
if (!empty($axis_result[$axis_code])) {
$pole_code = '';
if (isset($type_code[$index])) {
$pole_code = $type_code[$index];
}
if ($pole_code === '') {
$pole_code = $axis_result[$axis_code]['winner'];
}
$axis_result[$axis_code]['winner'] = $pole_code;
$axis_result[$axis_code]['winner_name'] = $context->getPoleName($pole_code);
}
$index++;
}
return $axis_result;
}
/**
* 组装调试数据
* debugPayload
* @param array $agg_data 聚合结果
* @param array $pct_list 各极点百分比
* @param string $type_code 类型码
* @return array
*/
protected function debugPayload($agg_data, $pct_list, $type_code)
{
$data = parent::debugPayload($agg_data, $pct_list, $type_code);
if (empty($data)) {
return $data;
}
$data['candidates'] = $this->candidate_list;
return $data;
}
}