【问题标题】:Brute Force Optimization蛮力优化
【发布时间】:2011-11-29 09:27:08
【问题描述】:

我正在开发一个使用插件的应用程序,插件是由独立开发人员开发的。我需要使用蛮力优化插件的参数。

public class Parameter
{
    public double Start { get; set; }
    public double Step { get; set; }
    public double Stop { get; set; }
    public double Value { get; set; }
}

public class Plugin
{
    public List<Parameter> Parameters { get; private set; }

    public Plugin()
    {
        Parameters = new List<Parameter>()
        {
            new Parameter() { Start = 1, Step = 1, Stop = 2 },
            new Parameter() { Start = 3, Step = 1, Stop = 4 }
        };
    }

    public void Execute()
    {
        double sum = 0;
        foreach (var parameter in Parameters)
        {
            sum += parameter.Value;
        }
        Console.WriteLine("Sum = " + sum);
    }
}

class Program
{
    static void Main(string[] args)
    {
        var test = new Plugin();
        test.Parameters[0].Value = test.Parameters[0].Start;
        test.Parameters[1].Value = test.Parameters[1].Start;
        test.Execute();
        Console.ReadLine();
    }
}

但不幸的是,我无法发明参数优化算法。 在蛮力下,我的意思是

Parameter1 = 1
Parameter2 = 3

Parameter1 = 1
Parameter2 = 4

Parameter1 = 2
Parameter2 = 3

Parameter1 = 2
Parameter2 = 4

主要问题在于插件中的参数数量可以是任意的。 也许您可以建议在这种情况下使用哪种算法。

P.S.:对不起我的英语不好。

-----加法--------

换句话说。

我做了一个新的例子来说明我想要做什么。

class Program
{
    static void Main(string[] args)
    {
        var test = new Plugin();

        for (double parameter_0 = test.Parameters[0].Start; parameter_0 <= test.Parameters[0].Stop; parameter_0+= test.Parameters[0].Step)
        {
            for (double parameter_1 = test.Parameters[1].Start; parameter_1 <= test.Parameters[1].Stop; parameter_1 += test.Parameters[1].Step)
            {
                test.Parameters[0].Value = parameter_0;
                test.Parameters[1].Value = parameter_1;
                test.Execute();
            }
        }
        Console.ReadLine();
    }
}

public class Plugin
{
    public List<Parameter> Parameters { get; private set; }

    public Plugin()
    {
        Parameters = new List<Parameter>()
        {
            new Parameter() { Start = 1, Value = 1, Step = 1, Stop = 2 },
            new Parameter() { Start = 3, Value = 3, Step = 1, Stop = 4 }
        };
    }

    public void Execute()
    {
        Console.WriteLine(Convert.ToString(Parameters[0].Value) + " " + Convert.ToString(Parameters[1].Value));
    }
}

此代码正常工作,但不幸的是,如果插件添加新参数,代码将中断。我需要发明一种对参数数量不敏感的算法。 我知道我可以使用递归,但是我不能使用多线程。

【问题讨论】:

  • 你的问题不是很清楚。你想知道什么?其他优化方法?如何使用所有可能的参数进行暴力测试?如何找到所有可能的参数?
  • 您要测试插件吗?

标签: c# optimization plugins brute-force


【解决方案1】:

如果我理解正确,您需要参数列表的笛卡尔积。

做一个笛卡尔积可以使用这个方法:

static IEnumerable<IEnumerable<T>> 
              CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) 
{ 
  IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() }; 
  return sequences.Aggregate( 
    emptyProduct, 
    (accumulator, sequence) => 
      from accseq in accumulator 
      from item in sequence 
      select accseq.Concat(new[] {item})); 
}

(来自 Eric Lippert 的 Computing a Cartesian Product with LINQ

然后您需要从每个参数中创建参数值列表并对其应用笛卡尔积。由于您似乎只对 Start 和 Stop 值感兴趣,您可以执行以下操作:

var combinations = 
    CartesianProduct<double>(parameters.Select(x=> new [] {x.Start, x.Stop});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    相关资源
    最近更新 更多