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.
 

145 lines
4.5 KiB

<?php
declare(strict_types=1);
/**
* WeightedSumStrategy.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;
/**
* 多维度加权制
* 设计说明:
* - 与累加制的唯一差别:允许一道题同时影响多个极点(option.effect)
* 用于「一道题既反映开放性也反映进取心」这类跨维度题
* - 与累加制的关系:effect 为空时退化为累加制(权重 1、只影响本极点)
* 也就是说 —— 当前题库(所有 effect 均为 null)下,本策略与 dimension_sum 结果完全相同
* 这是有意的:等题库真的加入跨维度题之后,本策略才有独立价值,届时两者才会分化
*/
class WeightedSumStrategy extends AbstractScoreStrategy
{
/**
* 获取策略标识
* getName
* @return string
*/
public function getName()
{
return 'weighted_sum';
}
/**
* 获取策略版本
* getVersion
* @return string
*/
public function getVersion()
{
return '1.0.0';
}
/**
* 聚合各极点得分(支持一题多极点)
* aggregate
* @param ScoreContext $context 评分输入
* @return array 含 raw_list / max_list / part_list
*/
protected function aggregate(ScoreContext $context)
{
$raw_list = $this->createEmptyPoleData($context);
$max_list = $this->createEmptyPoleData($context);
$part_list = [];
foreach ($raw_list as $pole_code => $value) {
$part_list[$pole_code] = [];
}
foreach ($context->question_list as $q_no => $question) {
$q_max_list = [];
foreach ($question['options'] as $option) {
$contribution_list = $this->getContributionList($context, $question, $option);
foreach ($contribution_list as $pole_code => $weight) {
$score = (float) $option['score'] * $weight;
$old_score = 0.0;
if (!empty($q_max_list[$pole_code])) {
$old_score = $q_max_list[$pole_code];
}
if ($score > $old_score) {
$q_max_list[$pole_code] = $score;
}
}
}
foreach ($q_max_list as $pole_code => $score) {
$max_list[$pole_code] += $score;
}
$answer_key = $context->answer_list[$q_no];
$option = $question['options'][$answer_key];
$contribution_list = $this->getContributionList($context, $question, $option);
foreach ($contribution_list as $pole_code => $weight) {
$score = (float) $option['score'] * $weight;
if ($question['polarity'] === -1) {
$score = $q_max_list[$pole_code] - $score;
}
$raw_list[$pole_code] += $score;
$part_list[$pole_code][] = [$score, $q_max_list[$pole_code]];
}
}
$data = [];
$data['raw_list'] = $raw_list;
$data['max_list'] = $max_list;
$data['part_list'] = $part_list;
return $data;
}
/**
* 取一个选项影响的极点与权重
* getContributionList
* @param ScoreContext $context 评分输入
* @param array $question 题目
* @param array $option 选项
* @return array 键为极点码,值为权重
*
* effect 形如 {"N": 0.6, "T": 0.4};为空时只看该选项显式指向的极点,权重 1
* 反向题的翻极在这里统一处理,避免两处各翻一次
*/
private function getContributionList(ScoreContext $context, $question, $option)
{
$contribution_list = [];
if (!empty($option['effect'])) {
foreach ($option['effect'] as $pole_code => $weight) {
$contribution_list[$pole_code] = floatval($weight);
}
}
if (empty($contribution_list)) {
$contribution_list[$option['dimension_code']] = 1.0;
}
if ($question['polarity'] === -1) {
$flip_list = [];
foreach ($contribution_list as $pole_code => $weight) {
$flip_list[$context->getPairPole($pole_code)] = $weight;
}
$contribution_list = $flip_list;
}
return $contribution_list;
}
}