暴力优化

发布于 2024-12-18 16:04:55 字数 2667 浏览 0 评论 0原文

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

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

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

PS:抱歉我的英语不好。

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

换句话说。

我做了一个新的例子来说明我想做的事情。

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));
    }
}

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

I am developing an application which uses a plug-ins, the plug-ins are developed by independent developers. I need to optimize the parameters for the plug-in using brute force.

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();
    }
}

But unfortunately I can't invent algorithm of optimization of parameters.
Under brute force, I mean

enter image description here

Parameter1 = 1
Parameter2 = 3

Parameter1 = 1
Parameter2 = 4

Parameter1 = 2
Parameter2 = 3

Parameter1 = 2
Parameter2 = 4

The main problem consists in that that the quantity of parameters in a plug-in can be any.
Maybe you could suggest which algorithm to use in this case.

P.S.: sorry for my bad English.

-----addition--------

In other words.

I made a new example that illustrates what I want to do.

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));
    }
}

This code works correctly but unfortunately if the plugin to add a new parameter the code will break. I need to invent an algorithm which is not sensitive to the number of parameters.
I know that I can use recursion but then I can not use multi-threading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪装你 2024-12-25 16:04:55

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

要进行笛卡尔积,您可以使用以下方法:(

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})); 
}

来自 使用 LINQ 计算笛卡尔积,作者:Eric Lippert)

然后,您需要根据每个参数创建参数值列表,并将笛卡尔积应用于它。由于您看起来只对 Start 和 Stop 值感兴趣,因此您可以执行以下操作:

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

If I understand correctly you need a cartesian products of your the parameters lists.

To do a cartesian product you can use this method:

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})); 
}

(from Computing a Cartesian Product with LINQ by Eric Lippert)

Then you need to create out of each parameters the lists of parameters values and apply the cartesian product to it. Since it looks like you are only interested in the Start and Stop values you can do the following:

var combinations = 
    CartesianProduct<double>(parameters.Select(x=> new [] {x.Start, x.Stop});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文