You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中40圈赛车单/双停最优轮胎组合自动实现方案问询

双停策略最优耗时自动化计算方案

核心思路

双停策略的本质是枚举合法分段组合+轮胎类型组合,由于总圈数固定为40,且单段最大圈数限制为25,实际需要枚举的组合数非常有限,完全可以通过程序化遍历实现自动化求解。

具体实现步骤

  1. 生成合法三段分段
    遍历第一段圈数x(125)、第二段圈数`y`(125),计算第三段圈数z=40-x-y,仅保留满足1≤z≤25的组合,过滤掉非法分段。

  2. 遍历所有轮胎组合
    每段可选软胎/中性胎,3段共8种组合,直接枚举所有可能性即可。

  3. 计算总耗时并追踪最优解
    对每个合法的分段+轮胎组合,调用已实现的SoftSummation/MedSummation计算单段耗时,累加得到总耗时,同时记录最小总耗时及对应的策略。

C#代码实现

// 假设已实现的轮胎退化计算函数(保持decimal类型适配需求)
decimal SoftSummation(int lapCount);
decimal MedSummation(int lapCount);

// 用于缓存计算结果,避免重复调用退化函数
private readonly Dictionary<int, decimal> _softTimeCache = new();
private readonly Dictionary<int, decimal> _medTimeCache = new();

private decimal GetSoftTime(int laps)
{
    if (_softTimeCache.TryGetValue(laps, out var time)) return time;
    var calcTime = SoftSummation(laps);
    _softTimeCache[laps] = calcTime;
    return calcTime;
}

private decimal GetMedTime(int laps)
{
    if (_medTimeCache.TryGetValue(laps, out var time)) return time;
    var calcTime = MedSummation(laps);
    _medTimeCache[laps] = calcTime;
    return calcTime;
}

public void CalculateOptimalDoubleStopStrategy()
{
    decimal minTotalTime = decimal.MaxValue;
    string bestStrategy = string.Empty;

    // 遍历第一段圈数x
    for (int x = 1; x <= 25; x++)
    {
        // 遍历第二段圈数y,提前过滤z可能非法的情况
        for (int y = 1; y <= Math.Min(25, 39 - x); y++)
        {
            int z = 40 - x - y;
            if (z < 1 || z > 25) continue;

            // 枚举所有8种轮胎组合
            var tireConfigs = new[]
            {
                ("软胎", "软胎", "软胎"), ("软胎", "软胎", "中性胎"),
                ("软胎", "中性胎", "软胎"), ("软胎", "中性胎", "中性胎"),
                ("中性胎", "软胎", "软胎"), ("中性胎", "软胎", "中性胎"),
                ("中性胎", "中性胎", "软胎"), ("中性胎", "中性胎", "中性胎")
            };

            foreach (var (t1, t2, t3) in tireConfigs)
            {
                decimal time1 = t1 == "软胎" ? GetSoftTime(x) : GetMedTime(x);
                decimal time2 = t2 == "软胎" ? GetSoftTime(y) : GetMedTime(y);
                decimal time3 = t3 == "软胎" ? GetSoftTime(z) : GetMedTime(z);
                decimal totalTime = time1 + time2 + time3;

                if (totalTime < minTotalTime)
                {
                    minTotalTime = totalTime;
                    bestStrategy = $"分段:{x}圈({t1}) → {y}圈({t2}) → {z}圈({t3}),总耗时:{minTotalTime:F4}";
                }
            }
        }
    }

    Console.WriteLine("双停策略最优解:");
    Console.WriteLine(bestStrategy);
}

性能优化说明

  • 结果缓存:通过字典缓存每段圈数的轮胎耗时,避免重复调用SoftSummation/MedSummation函数,提升计算效率。
  • 提前过滤:在第二层循环中限制y的最大值为Math.Min(25, 39 - x),直接跳过会导致z非法的情况,减少无效循环次数。

内容的提问来源于stack exchange,提问作者Kee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 14:58:26