微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

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

A
                                 +/ \-
                                B1   B1
                               +/\- +/\-
                              B2 B2 B2 B2

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

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

我想找到/ – incX到X的最佳组合(x是5个变量之一),即:
解决方案可能是这样的:
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.

解决方法

您可以使用以下函数生成包含所有可能的操作集(例如{ –,–,– },{ –,},– }等)的集合. bool数组列表,其中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;
}

获得此列表后,您可以使用它以所有可能的方式组合变量.首先,我们定义一个辅助函数获取一组变量和一个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;
}

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

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

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

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

这将输出

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()稍微改变以返回2n个组合(而不是之前的2n-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;
}

除了要使用的变量和组合之外,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,false}
    // The result will be {var1 + inc1,var2 - inc2,var3 + inc3,var4 + inc4,var 5 - inc5}
    return result;
}

并且FormatCombination()也会更新以显示新的组合样式:

private string FormatCombination(double[] vars,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());
}

把它们放在一起:

var vars = new[]
{
    1.23,// 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,combination);

    // Perform operation on combined here...

    Console.WriteLine(FormatCombination(vars,combination));
}

输出(删节):

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,1.26 - 0.85 = 0.41
1.23 - 0.04 = 1.19,0.02 + 0.11 = 0.13,1.26 - 0.85 = 0.41
...

原文地址:https://www.jb51.cc/csharp/100088.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐