Skip to content

RD-Agent Tutorial - Chapter 3: Advanced Usage Techniques

3.1 Quantitative Finance Agent Deep Dive

Quantitative Finance Scenario Architecture

RD-Agent's quantitative finance agent RD-Agent(Q) is the industry's first data-driven multi-agent quantitative strategy development framework. By coordinating joint factor-model optimization, it achieves full-stack automated quantitative research and development.

Core Architecture Design

graph TB
    A[市场数据] --> B[数据预处理]
    B --> C[因子挖掘代理]
    B --> D[模型演进代理]
    C --> E[因子评估]
    D --> F[模型评估]
    E --> G[联合优化代理]
    F --> G
    G --> H[策略回测]
    H --> I[性能分析]
    I --> J{满足要求?}
    J -->|否| K[知识反馈]
    K --> C
    K --> D
    J -->|是| L[策略部署]

    style C fill:#e1f5fe
    style D fill:#f3e5f5
    style G fill:#e8f5e8
    style H fill:#fff3e0

Qlib Framework Integration

RD-Agent(Q) deeply integrates Microsoft's Qlib quantitative investment library, providing:

Data management: - 🗃️ Standardized financial data formats - 📈 Real-time and historical data interfaces - 🔄 Automatic data update mechanisms - 🧹 Data cleaning and preprocessing

Backtesting engine: - 📊 High-performance vectorized backtesting - 💰 Transaction cost and slippage simulation - 📋 Multiple portfolio construction strategies - 📈 Risk metric computation

Model library: - 🤖 Machine learning model integration - 📊 Traditional quantitative factors - 🧠 Deep learning models - 🔄 Model ensembling strategies

Factor Mining and Optimization

rdagent fin_factor In Depth

The factor mining agent focuses on discovering and optimizing effective quantitative factors.

Launch commands:

# 基本因子挖掘
rdagent fin_factor

# 指定配置文件
rdagent fin_factor --config factor_config.yaml

# 指定迭代次数
export FACTOR_MAX_LOOP=15
rdagent fin_factor

Workflow:

  1. Hypothesis generation stage

    # 基于市场理论和历史经验生成因子假设
    factor_hypotheses = [
        "技术指标动量因子",
        "财务质量因子",
        "市场情绪因子",
        "宏观经济因子"
    ]
    

  2. Factor implementation stage

    # 自动生成因子计算代码
    class MomentumFactor:
        def calculate(self, data):
            # 自动生成的动量因子计算逻辑
            return (data['close'] / data['close'].shift(20) - 1)
    

  3. Factor evaluation stage

    # 多维度因子评估
    evaluation_metrics = {
        'IC': 0.045,          # 信息系数
        'IC_IR': 1.2,         # 信息比率
        'rank_IC': 0.038,     # 排序IC
        'turnover': 0.8,      # 换手率
        'max_drawdown': 0.15  # 最大回撤
    }
    

Factor Generation Strategies

Technical analysis factors:

technical_factors = {
    "momentum": [
        "price_momentum_5d", "price_momentum_20d",
        "volume_momentum", "volatility_momentum"
    ],
    "mean_reversion": [
        "rsi_divergence", "bollinger_position",
        "price_deviation"
    ],
    "trend": [
        "ma_trend", "macd_signal", "trend_strength"
    ]
}

Fundamental factors:

fundamental_factors = {
    "profitability": [
        "roe_trend", "roa_improvement",
        "gross_margin_stability"
    ],
    "growth": [
        "revenue_growth_consistency", "eps_growth_quality",
        "cash_flow_growth"
    ],
    "valuation": [
        "pe_relative", "pb_sector_adjusted",
        "ev_ebitda_normalized"
    ]
}

Alternative factors:

alternative_factors = {
    "sentiment": [
        "news_sentiment_score", "social_media_buzz",
        "analyst_revision_momentum"
    ],
    "network": [
        "supply_chain_strength", "industry_correlation",
        "peer_performance_influence"
    ]
}

Factor Effectiveness Evaluation

Evaluation framework:

class FactorEvaluator:
    def __init__(self, benchmark_data, start_date, end_date):
        self.data = benchmark_data
        self.start_date = start_date
        self.end_date = end_date

    def evaluate_factor(self, factor_values):
        """综合评估因子有效性"""
        metrics = {}

        # 1. 信息系数分析
        metrics['IC'] = self.calculate_IC(factor_values)
        metrics['IC_std'] = self.calculate_IC_stability(factor_values)
        metrics['IC_IR'] = metrics['IC'] / metrics['IC_std']

        # 2. 单调性检验
        metrics['monotonicity'] = self.test_monotonicity(factor_values)

        # 3. 换手率分析
        metrics['turnover'] = self.calculate_turnover(factor_values)

        # 4. 回测表现
        backtest_result = self.backtest_factor(factor_values)
        metrics.update(backtest_result)

        return metrics

Model Evolution and Optimization

rdagent fin_model In Depth

The model evolution agent focuses on automated development and optimization of prediction models.

Supported model types:

Model Category Specific Models Use Cases Characteristics
Linear models LinearRegression, Ridge, Lasso Simple, fast prediction Strong interpretability
Tree models XGBoost, LightGBM, CatBoost Tabular data modeling Feature importance
Neural networks MLP, TabNet, DeepFM Complex relationship modeling Nonlinear fitting
Time-series models LSTM, GRU, Transformer Sequential data prediction Temporal dependency
Ensemble models Stacking, Voting, Blending Improving prediction accuracy Model fusion

Automated architecture search:

class ModelArchitectureSearch:
    def __init__(self):
        self.search_space = {
            "n_layers": [2, 3, 4, 5],
            "hidden_dims": [64, 128, 256, 512],
            "dropout_rate": [0.1, 0.2, 0.3, 0.4],
            "activation": ["relu", "tanh", "gelu"],
            "optimizer": ["adam", "adamw", "sgd"],
            "learning_rate": [0.001, 0.01, 0.1]
        }

    def search_best_architecture(self, train_data, valid_data):
        """搜索最优模型架构"""
        best_config = None
        best_score = float('-inf')

        for config in self.generate_configurations():
            model = self.build_model(config)
            score = self.evaluate_model(model, train_data, valid_data)

            if score > best_score:
                best_score = score
                best_config = config

        return best_config, best_score

Hyperparameter Optimization

Multiple optimization algorithms:

from optuna import create_study
from sklearn.model_selection import cross_val_score

class HyperparameterOptimizer:
    def __init__(self, model_class, param_space):
        self.model_class = model_class
        self.param_space = param_space

    def optimize_with_optuna(self, X_train, y_train, n_trials=100):
        """使用 Optuna 进行超参数优化"""
        def objective(trial):
            params = {}
            for param_name, param_range in self.param_space.items():
                if isinstance(param_range, list):
                    params[param_name] = trial.suggest_categorical(
                        param_name, param_range
                    )
                elif isinstance(param_range, tuple):
                    if isinstance(param_range[0], int):
                        params[param_name] = trial.suggest_int(
                            param_name, param_range[0], param_range[1]
                        )
                    else:
                        params[param_name] = trial.suggest_float(
                            param_name, param_range[0], param_range[1]
                        )

            model = self.model_class(**params)
            scores = cross_val_score(model, X_train, y_train, cv=5)
            return scores.mean()

        study = create_study(direction='maximize')
        study.optimize(objective, n_trials=n_trials)

        return study.best_params, study.best_value

Joint Factor-Model Optimization

rdagent fin_quant In Depth

The joint optimization agent is RD-Agent(Q)'s core innovation, enabling the co-evolution of factors and models.

Joint optimization strategies:

  1. Alternating optimization strategy

    class AlternatingOptimization:
        def __init__(self, factor_agent, model_agent):
            self.factor_agent = factor_agent
            self.model_agent = model_agent
    
        def optimize(self, max_iterations=10):
            for i in range(max_iterations):
                # 固定模型,优化因子
                new_factors = self.factor_agent.evolve_factors(
                    current_model=self.current_model
                )
    
                # 固定因子,优化模型
                new_model = self.model_agent.evolve_model(
                    current_factors=new_factors
                )
    
                # 评估联合性能
                performance = self.evaluate_joint_performance(
                    new_factors, new_model
                )
    
                if performance > self.best_performance:
                    self.update_best_solution(new_factors, new_model)
    

  2. Multi-objective optimization strategy

    class MultiObjectiveOptimization:
        def __init__(self):
            self.objectives = [
                "maximize_sharpe_ratio",
                "minimize_max_drawdown",
                "maximize_information_ratio",
                "minimize_turnover"
            ]
    
        def pareto_optimization(self, population):
            """帕累托前沿优化"""
            pareto_front = []
    
            for individual in population:
                is_dominated = False
                for other in population:
                    if self.dominates(other, individual):
                        is_dominated = True
                        break
    
                if not is_dominated:
                    pareto_front.append(individual)
    
            return pareto_front
    

Multi-Objective Optimization Algorithms

NSGA-II implementation:

class NSGA2Optimizer:
    def __init__(self, population_size=100, generations=50):
        self.population_size = population_size
        self.generations = generations

    def optimize(self, factor_space, model_space):
        # 初始化种群
        population = self.initialize_population()

        for generation in range(self.generations):
            # 评估适应度
            fitness_values = self.evaluate_population(population)

            # 非支配排序
            fronts = self.fast_non_dominated_sort(population, fitness_values)

            # 选择下一代
            next_population = self.select_next_generation(
                fronts, population, fitness_values
            )

            # 交叉和变异
            offspring = self.crossover_and_mutation(next_population)
            population = offspring

        return self.get_pareto_front(population)

Co-Evolution Mechanism

Knowledge sharing mechanism:

class KnowledgeSharing:
    def __init__(self):
        self.factor_knowledge_base = {}
        self.model_knowledge_base = {}
        self.interaction_patterns = {}

    def share_factor_insights(self, factor_id, insights):
        """分享因子洞察"""
        self.factor_knowledge_base[factor_id] = {
            'effectiveness': insights['ic_score'],
            'stability': insights['ic_std'],
            'best_models': insights['compatible_models'],
            'market_regimes': insights['effective_periods']
        }

    def share_model_insights(self, model_id, insights):
        """分享模型洞察"""
        self.model_knowledge_base[model_id] = {
            'architecture': insights['model_config'],
            'performance': insights['validation_score'],
            'best_factors': insights['important_features'],
            'hyperparameters': insights['optimal_params']
        }

    def get_recommendations(self, current_factors, current_model):
        """获取优化建议"""
        recommendations = {
            'factor_suggestions': [],
            'model_suggestions': [],
            'joint_strategies': []
        }

        # 基于历史知识生成建议
        for factor in current_factors:
            if factor in self.factor_knowledge_base:
                knowledge = self.factor_knowledge_base[factor]
                recommendations['model_suggestions'].extend(
                    knowledge['best_models']
                )

        return recommendations

Financial Report Analysis

Using rdagent fin_factor_report

Automatically extracting quantitative factors from financial reports is a unique capability of RD-Agent.

Supported report types: - 📋 Annual reports (10-K, Annual Report) - 📊 Quarterly reports (10-Q, Quarterly Report) - 📰 Press releases (8-K, Press Release) - 📈 Analyst reports (Research Report) - 🏛️ Regulatory filings (SEC Filings)

Usage examples:

# 基本报告分析
rdagent fin_factor_report --report-folder ./financial_reports

# 指定报告类型
rdagent fin_factor_report \
    --report-folder ./reports \
    --report-type annual \
    --language zh

# 批量处理
rdagent fin_factor_report \
    --report-folder ./reports \
    --batch-size 50 \
    --parallel-workers 4

Report Parsing and Feature Extraction

Text preprocessing pipeline:

class ReportPreprocessor:
    def __init__(self):
        self.nlp = spacy.load("en_core_web_sm")
        self.financial_terms = self.load_financial_dictionary()

    def preprocess_report(self, report_text):
        """预处理财务报告"""
        # 1. 文档清理
        cleaned_text = self.clean_document(report_text)

        # 2. 段落分割
        sections = self.extract_sections(cleaned_text)

        # 3. 实体识别
        entities = self.extract_financial_entities(sections)

        # 4. 情感分析
        sentiment_scores = self.analyze_sentiment(sections)

        return {
            'sections': sections,
            'entities': entities,
            'sentiment': sentiment_scores,
            'metadata': self.extract_metadata(report_text)
        }

Factor extraction strategy:

class FinancialFactorExtractor:
    def __init__(self):
        self.factor_extractors = {
            'growth_factors': GrowthFactorExtractor(),
            'quality_factors': QualityFactorExtractor(),
            'sentiment_factors': SentimentFactorExtractor(),
            'risk_factors': RiskFactorExtractor()
        }

    def extract_factors(self, processed_report):
        """从处理后的报告中提取因子"""
        extracted_factors = {}

        for factor_type, extractor in self.factor_extractors.items():
            factors = extractor.extract(processed_report)
            extracted_factors[factor_type] = factors

        return extracted_factors

class GrowthFactorExtractor:
    def extract(self, report_data):
        """提取成长性因子"""
        growth_factors = {}

        # 收入增长质量
        revenue_mentions = self.find_revenue_discussions(report_data)
        growth_factors['revenue_growth_quality'] = self.analyze_growth_quality(
            revenue_mentions
        )

        # 市场扩张策略
        expansion_mentions = self.find_expansion_discussions(report_data)
        growth_factors['expansion_strategy_score'] = self.score_expansion_strategy(
            expansion_mentions
        )

        # 研发投入承诺
        rd_mentions = self.find_rd_discussions(report_data)
        growth_factors['rd_commitment_score'] = self.analyze_rd_commitment(
            rd_mentions
        )

        return growth_factors

Sentiment Analysis and Signal Mining

Multi-dimensional sentiment analysis:

class FinancialSentimentAnalyzer:
    def __init__(self):
        self.sentiment_model = pipeline(
            "sentiment-analysis",
            model="ProsusAI/finbert"
        )
        self.uncertainty_detector = UncertaintyDetector()
        self.forward_looking_detector = ForwardLookingDetector()

    def analyze_comprehensive_sentiment(self, text_sections):
        """综合情感分析"""
        sentiment_scores = {}

        for section_name, text in text_sections.items():
            # 基础情感分析
            basic_sentiment = self.sentiment_model(text)

            # 不确定性检测
            uncertainty_score = self.uncertainty_detector.detect(text)

            # 前瞻性语句检测
            forward_looking = self.forward_looking_detector.detect(text)

            sentiment_scores[section_name] = {
                'polarity': basic_sentiment[0]['score'],
                'label': basic_sentiment[0]['label'],
                'uncertainty': uncertainty_score,
                'forward_looking': forward_looking,
                'confidence': self.calculate_confidence(
                    basic_sentiment, uncertainty_score
                )
            }

        return sentiment_scores

class SignalGenerator:
    def __init__(self):
        self.signal_weights = {
            'management_tone': 0.3,
            'financial_health': 0.4,
            'market_position': 0.2,
            'future_outlook': 0.1
        }

    def generate_trading_signals(self, factor_scores, sentiment_scores):
        """生成交易信号"""
        composite_score = 0.0

        # 加权合成得分
        for factor_type, weight in self.signal_weights.items():
            if factor_type in factor_scores:
                composite_score += factor_scores[factor_type] * weight

        # 情感调整
        sentiment_adjustment = self.calculate_sentiment_adjustment(
            sentiment_scores
        )

        final_score = composite_score * sentiment_adjustment

        # 生成信号
        if final_score > 0.6:
            signal = "STRONG_BUY"
        elif final_score > 0.3:
            signal = "BUY"
        elif final_score > -0.3:
            signal = "HOLD"
        elif final_score > -0.6:
            signal = "SELL"
        else:
            signal = "STRONG_SELL"

        return {
            'signal': signal,
            'confidence': abs(final_score),
            'score': final_score,
            'components': {
                'factor_score': composite_score,
                'sentiment_adjustment': sentiment_adjustment
            }
        }

3.2 CoSTEER Evolution Framework Deep Dive

Core Principles of the Evolution Framework

CoSTEER (Collaborative evolving STrategy for automatic data-cEntric dEvelopment fRamework) is RD-Agent's core evolution engine, enabling autonomous learning and continuous improvement for agents.

Collaborative Evolution Strategy Design

The CoSTEER framework is built on the following core principles:

  1. Multi-agent collaboration - Specialized agents working together
  2. Knowledge accumulation - Learning and improving from historical experience
  3. Adaptive evolution - Dynamically adjusting strategies based on feedback
  4. RAG enhancement - Retrieval-augmented generation improves decision quality
class CoSTEER(Developer[Experiment]):
    """CoSTEER 演进框架核心类"""

    def __init__(
        self,
        settings: CoSTEERSettings,
        eva: RAGEvaluator,
        es: EvolvingStrategy,
        evolving_version: int = 2,
        with_knowledge: bool = True,
        knowledge_self_gen: bool = True,
        max_loop: int | None = None,
    ):
        self.settings = settings
        self.max_loop = settings.max_loop if max_loop is None else max_loop
        self.knowledge_base_path = Path(settings.knowledge_base_path)
        self.with_knowledge = with_knowledge
        self.knowledge_self_gen = knowledge_self_gen
        self.evolving_strategy = es
        self.evaluator = eva

        # 初始化 RAG 系统
        self.rag_strategy = self._init_rag_strategy(evolving_version)

Knowledge Management Mechanism

Multi-level knowledge representation:

class KnowledgeManagementSystem:
    def __init__(self):
        self.knowledge_layers = {
            'factual_knowledge': FactualKnowledgeBase(),
            'procedural_knowledge': ProceduralKnowledgeBase(),
            'experiential_knowledge': ExperientialKnowledgeBase(),
            'meta_knowledge': MetaKnowledgeBase()
        }

    def store_knowledge(self, knowledge_item):
        """分层存储知识"""
        knowledge_type = self.classify_knowledge(knowledge_item)
        self.knowledge_layers[knowledge_type].store(knowledge_item)

    def retrieve_relevant_knowledge(self, query_context):
        """检索相关知识"""
        relevant_knowledge = {}

        for layer_name, knowledge_base in self.knowledge_layers.items():
            relevant_items = knowledge_base.query(
                query_context,
                similarity_threshold=0.7
            )
            relevant_knowledge[layer_name] = relevant_items

        return self.synthesize_knowledge(relevant_knowledge)

Knowledge representation format:

@dataclass
class KnowledgeItem:
    """知识项数据结构"""
    id: str
    content: str
    knowledge_type: KnowledgeType
    domain: str
    confidence: float
    source: str
    timestamp: datetime
    usage_count: int = 0
    success_rate: float = 0.0

    def to_embedding(self) -> np.ndarray:
        """转换为向量表示"""
        return self.embedding_model.encode(self.content)

    def update_effectiveness(self, success: bool):
        """更新知识有效性"""
        self.usage_count += 1
        if success:
            self.success_rate = (
                (self.success_rate * (self.usage_count - 1) + 1.0)
                / self.usage_count
            )
        else:
            self.success_rate = (
                self.success_rate * (self.usage_count - 1)
                / self.usage_count
            )

Adaptive Optimization Algorithms

Multi-strategy evolution algorithm:

class AdaptiveEvolutionStrategy:
    def __init__(self):
        self.evolution_strategies = {
            'genetic_algorithm': GeneticAlgorithmStrategy(),
            'differential_evolution': DifferentialEvolutionStrategy(),
            'particle_swarm': ParticleSwarmStrategy(),
            'simulated_annealing': SimulatedAnnealingStrategy()
        }
        self.strategy_performance = {}
        self.adaptive_weights = {}

    def evolve_population(self, population, generation):
        """自适应演进种群"""
        # 选择演进策略
        selected_strategies = self.select_strategies(generation)

        new_population = []
        for strategy_name, weight in selected_strategies.items():
            strategy = self.evolution_strategies[strategy_name]

            # 按权重分配种群
            sub_population_size = int(len(population) * weight)
            sub_population = population[:sub_population_size]

            # 演进子种群
            evolved_sub_pop = strategy.evolve(sub_population)
            new_population.extend(evolved_sub_pop)

        # 更新策略性能
        self.update_strategy_performance(new_population, generation)

        return new_population

    def select_strategies(self, generation):
        """基于历史性能选择演进策略"""
        if generation < 5:
            # 初期均匀分配
            return {name: 1.0/len(self.evolution_strategies)
                   for name in self.evolution_strategies.keys()}

        # 基于性能调整权重
        total_performance = sum(self.strategy_performance.values())
        return {
            name: performance / total_performance
            for name, performance in self.strategy_performance.items()
        }

Custom Evolution Strategies

Strategy Configuration and Tuning

Configuration file example:

# costeer_config.yaml
costeer_settings:
  max_loop: 10
  knowledge_base_path: "./knowledge_base"
  new_knowledge_base_path: "./new_knowledge"
  evolving_version: 2

evolution_strategy:
  population_size: 50
  mutation_rate: 0.1
  crossover_rate: 0.8
  selection_method: "tournament"
  tournament_size: 5

rag_settings:
  embedding_model: "text-embedding-3-small"
  similarity_threshold: 0.75
  max_retrieved_docs: 10
  rerank_top_k: 5

evaluation_settings:
  metrics: ["accuracy", "f1_score", "auc_roc"]
  cross_validation_folds: 5
  test_ratio: 0.2

knowledge_management:
  auto_cleanup: true
  cleanup_threshold: 0.3
  knowledge_update_frequency: 100
  max_knowledge_items: 10000

Custom evolution strategy implementation:

class CustomEvolutionStrategy(EvolvingStrategy):
    """自定义演进策略"""

    def __init__(self, config: dict):
        super().__init__()
        self.config = config
        self.population_size = config.get('population_size', 50)
        self.mutation_rate = config.get('mutation_rate', 0.1)
        self.crossover_rate = config.get('crossover_rate', 0.8)

    def initialize_population(self, size: int = None):
        """初始化种群"""
        size = size or self.population_size
        population = []

        for _ in range(size):
            individual = self.create_random_individual()
            population.append(individual)

        return population

    def evolve_generation(self, population, fitness_scores):
        """演进一代"""
        new_population = []

        # 精英选择 - 保留最优个体
        elite_count = int(len(population) * 0.1)
        elite_indices = np.argsort(fitness_scores)[-elite_count:]
        for idx in elite_indices:
            new_population.append(population[idx])

        # 交叉和变异生成新个体
        while len(new_population) < len(population):
            # 选择父代
            parent1 = self.tournament_selection(population, fitness_scores)
            parent2 = self.tournament_selection(population, fitness_scores)

            # 交叉
            if random.random() < self.crossover_rate:
                child1, child2 = self.crossover(parent1, parent2)
            else:
                child1, child2 = parent1, parent2

            # 变异
            if random.random() < self.mutation_rate:
                child1 = self.mutate(child1)
            if random.random() < self.mutation_rate:
                child2 = self.mutate(child2)

            new_population.extend([child1, child2])

        return new_population[:len(population)]

    def crossover(self, parent1, parent2):
        """交叉操作"""
        # 实现具体的交叉逻辑
        # 这里以参数交叉为例
        child1_params = {}
        child2_params = {}

        for key in parent1.parameters.keys():
            if random.random() < 0.5:
                child1_params[key] = parent1.parameters[key]
                child2_params[key] = parent2.parameters[key]
            else:
                child1_params[key] = parent2.parameters[key]
                child2_params[key] = parent1.parameters[key]

        child1 = parent1.copy()
        child2 = parent2.copy()
        child1.parameters = child1_params
        child2.parameters = child2_params

        return child1, child2

    def mutate(self, individual):
        """变异操作"""
        mutated = individual.copy()

        for key, value in mutated.parameters.items():
            if random.random() < self.mutation_rate:
                if isinstance(value, (int, float)):
                    # 高斯变异
                    noise = np.random.normal(0, 0.1 * abs(value))
                    mutated.parameters[key] = value + noise
                elif isinstance(value, str):
                    # 字符串变异(如模型名称)
                    mutated.parameters[key] = self.mutate_string(value)

        return mutated

Evaluator Customization

Multi-metric evaluator:

class MultiMetricEvaluator(RAGEvaluator):
    """多指标评估器"""

    def __init__(self, metrics_config: dict):
        super().__init__()
        self.metrics_config = metrics_config
        self.metric_weights = metrics_config.get('weights', {})
        self.metric_calculators = self._init_metric_calculators()

    def evaluate(self, experiment: Experiment) -> Dict[str, float]:
        """多指标评估"""
        metrics = {}

        for metric_name, calculator in self.metric_calculators.items():
            try:
                score = calculator.calculate(experiment)
                metrics[metric_name] = score
            except Exception as e:
                logger.warning(f"计算指标 {metric_name} 失败: {e}")
                metrics[metric_name] = 0.0

        # 计算加权综合得分
        weighted_score = self._calculate_weighted_score(metrics)
        metrics['weighted_score'] = weighted_score

        return metrics

    def _calculate_weighted_score(self, metrics: Dict[str, float]) -> float:
        """计算加权综合得分"""
        total_score = 0.0
        total_weight = 0.0

        for metric_name, score in metrics.items():
            if metric_name in self.metric_weights:
                weight = self.metric_weights[metric_name]
                total_score += score * weight
                total_weight += weight

        return total_score / total_weight if total_weight > 0 else 0.0

class CustomMetricCalculator:
    """自定义指标计算器"""

    def __init__(self, metric_name: str, calculation_func: callable):
        self.metric_name = metric_name
        self.calculation_func = calculation_func

    def calculate(self, experiment: Experiment) -> float:
        """计算指标值"""
        return self.calculation_func(experiment)

# 使用示例
def sharpe_ratio_calculator(experiment):
    """计算夏普比率"""
    returns = experiment.get_returns()
    if len(returns) == 0 or returns.std() == 0:
        return 0.0
    return returns.mean() / returns.std() * np.sqrt(252)

def max_drawdown_calculator(experiment):
    """计算最大回撤"""
    cumulative_returns = (1 + experiment.get_returns()).cumprod()
    running_max = cumulative_returns.expanding().max()
    drawdown = (cumulative_returns - running_max) / running_max
    return -drawdown.min()  # 返回正值

# 创建自定义评估器
metrics_config = {
    'weights': {
        'sharpe_ratio': 0.4,
        'max_drawdown': 0.3,
        'information_ratio': 0.3
    }
}

evaluator = MultiMetricEvaluator(metrics_config)
evaluator.add_metric_calculator('sharpe_ratio', sharpe_ratio_calculator)
evaluator.add_metric_calculator('max_drawdown', max_drawdown_calculator)

Knowledge Base Management

Knowledge base optimization strategy:

class KnowledgeBaseManager:
    """知识库管理器"""

    def __init__(self, config: dict):
        self.config = config
        self.knowledge_store = {}
        self.embedding_cache = {}
        self.access_frequency = defaultdict(int)
        self.last_access_time = {}

    def add_knowledge(self, knowledge_item: KnowledgeItem):
        """添加知识项"""
        # 检查知识库大小限制
        if len(self.knowledge_store) >= self.config.get('max_items', 10000):
            self._cleanup_knowledge()

        self.knowledge_store[knowledge_item.id] = knowledge_item
        self.embedding_cache[knowledge_item.id] = knowledge_item.to_embedding()
        self.last_access_time[knowledge_item.id] = datetime.now()

    def query_knowledge(
        self,
        query: str,
        top_k: int = 10,
        similarity_threshold: float = 0.7
    ) -> List[KnowledgeItem]:
        """查询相关知识"""
        query_embedding = self._get_query_embedding(query)
        similarities = {}

        for item_id, item_embedding in self.embedding_cache.items():
            similarity = cosine_similarity(
                query_embedding.reshape(1, -1),
                item_embedding.reshape(1, -1)
            )[0][0]

            if similarity >= similarity_threshold:
                similarities[item_id] = similarity
                self.access_frequency[item_id] += 1
                self.last_access_time[item_id] = datetime.now()

        # 按相似度排序并返回top-k
        sorted_items = sorted(
            similarities.items(),
            key=lambda x: x[1],
            reverse=True
        )[:top_k]

        return [self.knowledge_store[item_id] for item_id, _ in sorted_items]

    def _cleanup_knowledge(self):
        """清理知识库"""
        cleanup_count = int(len(self.knowledge_store) * 0.1)  # 清理10%

        # 基于访问频率和时间的综合评分
        scores = {}
        current_time = datetime.now()

        for item_id, knowledge_item in self.knowledge_store.items():
            frequency_score = self.access_frequency[item_id]
            time_score = (
                current_time - self.last_access_time[item_id]
            ).days
            effectiveness_score = knowledge_item.success_rate

            # 综合评分(频率高、时间近、效果好的得分高)
            composite_score = (
                frequency_score * 0.4 +
                effectiveness_score * 0.4 -
                time_score * 0.2
            )
            scores[item_id] = composite_score

        # 删除评分最低的项目
        items_to_remove = sorted(
            scores.items(),
            key=lambda x: x[1]
        )[:cleanup_count]

        for item_id, _ in items_to_remove:
            del self.knowledge_store[item_id]
            del self.embedding_cache[item_id]
            del self.access_frequency[item_id]
            del self.last_access_time[item_id]

        logger.info(f"清理了 {cleanup_count} 个知识项")

Multi-Agent Collaboration Mechanism

Research Agent Responsibilities

class ResearchAgent:
    """研究代理 - 负责假设生成和策略规划"""

    def __init__(self, knowledge_base: KnowledgeBase):
        self.knowledge_base = knowledge_base
        self.hypothesis_generator = HypothesisGenerator()
        self.strategy_planner = StrategyPlanner()

    def generate_hypotheses(self, problem_context: dict) -> List[Hypothesis]:
        """生成研究假设"""
        # 从知识库检索相关理论
        relevant_knowledge = self.knowledge_base.query(
            problem_context['description']
        )

        # 基于领域知识生成假设
        hypotheses = []
        for knowledge_item in relevant_knowledge:
            hypothesis = self.hypothesis_generator.generate_from_knowledge(
                knowledge_item, problem_context
            )
            hypotheses.append(hypothesis)

        # 生成创新假设
        novel_hypotheses = self.hypothesis_generator.generate_novel(
            problem_context, existing_hypotheses=hypotheses
        )
        hypotheses.extend(novel_hypotheses)

        return self.rank_hypotheses(hypotheses)

    def plan_research_strategy(self, hypothesis: Hypothesis) -> ResearchPlan:
        """规划研究策略"""
        return self.strategy_planner.create_plan(
            hypothesis=hypothesis,
            available_resources=self.get_available_resources(),
            time_constraints=self.get_time_constraints()
        )

Development Agent Capabilities

class DevelopmentAgent:
    """开发代理 - 负责代码实现和测试验证"""

    def __init__(self, code_generator: CodeGenerator, validator: CodeValidator):
        self.code_generator = code_generator
        self.validator = validator
        self.implementation_cache = {}

    def implement_hypothesis(self, hypothesis: Hypothesis) -> Implementation:
        """实现研究假设"""
        # 生成实现代码
        implementation_code = self.code_generator.generate_code(
            specification=hypothesis.specification,
            constraints=hypothesis.constraints
        )

        # 验证代码正确性
        validation_result = self.validator.validate(implementation_code)

        if not validation_result.is_valid:
            # 修复代码问题
            fixed_code = self.code_generator.fix_issues(
                implementation_code,
                validation_result.issues
            )
            implementation_code = fixed_code

        # 创建实现对象
        implementation = Implementation(
            code=implementation_code,
            hypothesis=hypothesis,
            validation_result=validation_result
        )

        # 缓存实现
        self.implementation_cache[hypothesis.id] = implementation

        return implementation

    def optimize_implementation(
        self,
        implementation: Implementation,
        feedback: Feedback
    ) -> Implementation:
        """基于反馈优化实现"""
        optimization_suggestions = self.analyze_feedback(feedback)

        optimized_code = self.code_generator.optimize_code(
            implementation.code,
            optimization_suggestions
        )

        return Implementation(
            code=optimized_code,
            hypothesis=implementation.hypothesis,
            parent_implementation=implementation
        )

Inter-Agent Communication Protocol

class AgentCommunicationProtocol:
    """代理间通信协议"""

    def __init__(self):
        self.message_queue = {}
        self.subscriptions = defaultdict(list)
        self.message_handlers = {}

    def register_agent(self, agent_id: str, agent: Agent):
        """注册代理"""
        self.message_queue[agent_id] = []
        agent.set_communication_protocol(self)

    def subscribe(self, agent_id: str, message_type: str):
        """订阅消息类型"""
        self.subscriptions[message_type].append(agent_id)

    def publish(self, sender_id: str, message_type: str, message: dict):
        """发布消息"""
        # 添加消息元数据
        full_message = {
            'sender': sender_id,
            'type': message_type,
            'timestamp': datetime.now(),
            'content': message
        }

        # 发送给所有订阅者
        for subscriber_id in self.subscriptions[message_type]:
            if subscriber_id != sender_id:  # 不发送给自己
                self.message_queue[subscriber_id].append(full_message)

    def get_messages(self, agent_id: str) -> List[dict]:
        """获取代理的消息"""
        messages = self.message_queue.get(agent_id, [])
        self.message_queue[agent_id] = []  # 清空队列
        return messages

class CollaborativeWorkflow:
    """协作工作流"""

    def __init__(self, agents: List[Agent], protocol: AgentCommunicationProtocol):
        self.agents = {agent.id: agent for agent in agents}
        self.protocol = protocol
        self.workflow_state = WorkflowState.INITIALIZED

    def execute_collaborative_task(self, task: CollaborativeTask):
        """执行协作任务"""
        # 分解任务
        subtasks = self.decompose_task(task)

        # 分配子任务给合适的代理
        task_assignments = self.assign_tasks(subtasks)

        # 执行子任务
        results = {}
        for agent_id, assigned_tasks in task_assignments.items():
            agent = self.agents[agent_id]
            agent_results = agent.execute_tasks(assigned_tasks)
            results[agent_id] = agent_results

        # 协调和整合结果
        integrated_result = self.integrate_results(results)

        return integrated_result

    def coordinate_agents(self):
        """协调代理活动"""
        # 处理代理间消息
        for agent_id, agent in self.agents.items():
            messages = self.protocol.get_messages(agent_id)
            if messages:
                agent.process_messages(messages)

        # 检查是否需要协调
        coordination_needed = self.check_coordination_needs()
        if coordination_needed:
            self.perform_coordination()

This chapter has showcased RD-Agent's advanced features and deep customization capabilities, including the specialized functionality of the quantitative finance agent, the core mechanisms of the CoSTEER evolution framework, and implementation approaches for multi-agent collaboration. In the next chapter, we will demonstrate the practical application of these advanced features through real-world case studies.