如何在32个二进制选项之间进行迭代?

ken*_*tik 7 c# algorithm

我有一个优化问题,我有5个变量:A,B1,B2,C1,C2.我试图优化这5个变量,以获得最小的根和平方值.我有一些工作正常的优化技术,但这个特别给我带来了一些麻烦.

我想探索更改变量的所有32个选项并选择最小的RSS值.我的意思是每个变量可以是+/-增量.每个选择导致2个选择,5个变量有32个选择.(2 ^ 5)

为了澄清,我没有添加我的变量:A,B1,B2等彼此,我正在递增/递减任意数量.A +/- X,B1 +/- X2等.我试图找出我的5个变量的增量/减量的哪个组合将返回最低的根和平方值.

                                   A
                                 +/ \-
                                B1   B1
                               +/\- +/\-
                              B2 B2 B2 B2
Run Code Online (Sandbox Code Playgroud)

等等,直到所有5个级别都完成.我不确定哪里开始尝试解决这个问题.什么样的数据结构最适合存储?它是迭代的还是递归的解决方案.我不需要问题的答案,而是需要寻找或在某个地方开始.再次感谢您抽出宝贵时间来看看这个.

为了澄清进一步的混淆,这是我的优化方法.我有5个变量和5个增量,每个增量匹配一个变量.(a,b,c,d,e,f)--->(incA,incB,incC,indD,incE,incF)

我想找到+/- incX到X的最佳组合(x是5个变量中的一个),即:解决方案可能类似于:a + incA,B-incB,c + incC,d + incD,e + incE,f-incF.有32种组合可能性,在阅读了下面的所有答案后,我已经确定了这种可能的算法.(请参阅下面的答案)根据需要进行编辑和提问.这不是一个完美的算法,它是为了澄清和易于理解,我知道它可以被浓缩.

//Settign all possible solutions to be iterated through later.
double[] levelA = new double[2];
double[] levelB = new double[2];
double[] levelC = new double[2];
double[] levelD = new double[2];
double[] levelE = new double[2];

levelA[0] = a + incA;
levelA[1] = a - incA;
levelB[0] = b + incB;
levelB[1] = b - incB;
levelC[0] = c + incC;
levelC[1] = c - incC;
levelD[0] = d + incD;
levelD[1] = d - incD;
levelE[0] = e + incE;
levelE[1] = e - incE;

double[] rootSumAnswers = new double[32];
int count = 0;

for(int i = 0; i < 2; i ++)
{
    for(int k = 0; k < 2; k++)
    {
        for(int j = 0; j < 2; j ++)
        {
            for(int n = 0; n < 2; n++)
            {
                for(int m = 0; m < 2; m++)
                {
                    rootSumAnswers[count++] = calcRootSum(levelA[i], levelB[k], levelC[j], levelD[n], levelE[m]);

                }
            }
        }
    }
}

//Finally, i find the minimum of my root sum squares and make that change permanent, and do this again.
Run Code Online (Sandbox Code Playgroud)

Iri*_*ium 3

您可以使用以下函数生成一个包含所有可能的操作集(例如 {-,-,-,-}, {-,-,-,+}, {-,-,+,-} 等)的集合,其中将输出布尔数组列表,其中 true 和 false 代表 + 和 -。该vars参数指示被组合的变量的数量。请注意,对于 5 个变量,只有 16 种组合(而不是您所说的 32 种),因为组合 5 个变量时只有 4 个运算符(假设第一个变量不能被否定):

public List<bool[]> GetOperators(int vars)
{
    var result = new List<bool[]>();

    for (var i = 0; i < 1 << vars-1; i++)
    {
        var item = new bool[vars - 1];
        for (var j = 0; j < vars-1; j++)
        {
            item[j] = ((i >> j) & 1) != 0;
        }
        result.Add(item);
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

获得此列表后,您可以使用它以所有可能的方式组合变量。首先,我们定义一个辅助函数来获取一组变量和一个bool[]组合并应用它(它假设组合中的元素数量与传递的变量数量相同):

private double Combine(double[] vars, bool[] combination)
{
    var sum = vars[0];
    for (var i = 1; i< vars.Length; i++)
    {
        sum = combination[i - 1] ? sum + vars[i] : sum - vars[i];
    }
    return sum;
}
Run Code Online (Sandbox Code Playgroud)

您还可以使用以下方法很好地格式化组合:

private string FormatCombination(double[] vars, bool[] combination)
{
    var result = vars[0].ToString("0.00##");
    for (var i = 1; i < vars.Length; i++)
    {
        result = string.Format("{0} {1} {2:0.00##}", result, combination[i - 1] ? "+" : "-", vars[i]);
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

将它们放在一起来测试所有可能的组合:

var vars = new []
{
    1.23, // A
    0.02, // B1
    0.11, // B2
    0.05, // C1
    1.26  // C2
};

foreach (var combination in GetOperators(vars.Length))
{
    var combined = Combine(vars, combination);

    // Perform your operations on "combined" here...

    Console.WriteLine("{0} = {1}", FormatCombination(vars, combination), combined);
}
Run Code Online (Sandbox Code Playgroud)

这将输出:

1.23 - 0.02 - 0.11 - 0.05 - 1.26 = -0.21
1.23 + 0.02 - 0.11 - 0.05 - 1.26 = -0.17
1.23 - 0.02 + 0.11 - 0.05 - 1.26 = 0.01
1.23 + 0.02 + 0.11 - 0.05 - 1.26 = 0.05
1.23 - 0.02 - 0.11 + 0.05 - 1.26 = -0.11
1.23 + 0.02 - 0.11 + 0.05 - 1.26 = -0.07
1.23 - 0.02 + 0.11 + 0.05 - 1.26 = 0.11
1.23 + 0.02 + 0.11 + 0.05 - 1.26 = 0.15
1.23 - 0.02 - 0.11 - 0.05 + 1.26 = 2.31
1.23 + 0.02 - 0.11 - 0.05 + 1.26 = 2.35
1.23 - 0.02 + 0.11 - 0.05 + 1.26 = 2.53
1.23 + 0.02 + 0.11 - 0.05 + 1.26 = 2.57
1.23 - 0.02 - 0.11 + 0.05 + 1.26 = 2.41
1.23 + 0.02 - 0.11 + 0.05 + 1.26 = 2.45
1.23 - 0.02 + 0.11 + 0.05 + 1.26 = 2.63
1.23 + 0.02 + 0.11 + 0.05 + 1.26 = 2.67

编辑:

根据您问题的更改,我更新了我的答案。正如其他人所提到的,可能没有必要使用这样的完整搜索,但您可能会发现该方法无论如何都很有用。

GetOperators()稍作更改以返回 2 n组合(而不是之前的2 n-1组合):

public List<bool[]> GetOperators(int vars)
{
    var result = new List<bool[]>();

    for (var i = 0; i < 1 << vars; i++)
    {
        var item = new bool[vars];
        for (var j = 0; j < vars; j++)
        {
            item[j] = ((i >> j) & 1) != 0;
        }
        result.Add(item);
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

该Combine()方法被更改为除了要使用的变量和组合之外还采用一组增量。对于组合的每个元素,如果为true,则将增量添加到变量中,如果为 false,则将其减去:

private double[] Combine(double[] vars, double[] increments, bool[] combination)
{
    // Assuming here that vars, increments and combination all have the same number of elements
    var result = new double[vars.Length];
    for (var i = 0; i < vars.Length; i++)
    {
        result[i] = combination[i] ? vars[i] + increments[i] : vars[i] - increments[i];
    }

    // Returns an array of the vars and increments combined per the given combination
    // E.g. if there are 5 vars and the combination is: {true, false, true, true, false}
    // The result will be {var1 + inc1, var2 - inc2, var3 + inc3, var4 + inc4, var 5 - inc5}
    return result;
}
Run Code Online (Sandbox Code Playgroud)

并且FormatCombination()还更新了显示新的组合样式:

private string FormatCombination(double[] vars, double[] increments, bool[] combination)
{
    var result = new List<string>(vars.Length);

    var combined = Combine(vars, increments, combination);

    for (var i = 0; i < vars.Length; i++)
    {
        result.Add(string.Format("{0:0.00##} {1} {2:0.00##} = {3:0.00##}", vars[i], combination[i] ? "+" : "-", increments[i], combined[i]));
    }
    return string.Join(", ", result.ToArray());
}
Run Code Online (Sandbox Code Playgroud)

把它们放在一起:

var vars = new[]
{
    1.23, // A
    0.02, // B
    0.11, // C
    0.05, // D
    1.26, // E
};

var increments = new[]
{
    0.04, // incA
    0.11, // incB
    0.01, // incC
    0.37, // incD
    0.85, // incD
};

foreach (var combination in GetOperators(vars.Length))
{
    var combined = Combine(vars, increments, combination);

    // Perform operation on combined here...

    Console.WriteLine(FormatCombination(vars, increments, combination));
}
Run Code Online (Sandbox Code Playgroud)

输出(删节):

1.23 - 0.04 = 1.19, 0.02 - 0.11 = -0.09, 0.11 - 0.01 = 0.10, 0.05 - 0.37 = -0.32, 1.26 - 0.85 = 0.41
1.23 + 0.04 = 1.27、0.02 - 0.11 = -0.09、0.11 - 0.01 = 0.10、0.05 - 0.37 = -0.32、1.26 - 0.85 = 0.41
1.23 - 0.04 = 1.19、0.02 + 0.11 = 0.13、0.11 - 0.01 = 0.10、0.05 - 0.37 = -0.32、1.26 - 0.85 = 0.41
1.23 + 0.04 = 1.27、0.02 + 0.11 = 0.13、0.11 - 0.01 = 0.10、0.05 - 0.37 = -0.32、1.26 - 0.85 = 0.41
...