- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在优化问题中找到两个数组的非支配值。
我有两个数组,lost_bars_array
和 prices_array
。我想做的是最小化丢失的金条,并最大化价格。我想获得一个名为 no_dominated
的最终数组,其中包含两个数组的非支配值的索引。这很容易理解。让我用一个例子来解释这一点。
我们有两个初始数组:
理论过程是这样的:
1.获取第一个值
2. 获取第二个(索引#1)并查看它是否是比第一个更好的解决方案。这意味着我们必须查看第二个是否有更少的损失柱和更高的价格。事实并非如此,它的酒吧更少,价格也更低,所以我们不能说这是一个更好的解决方案。我们将其保存为可能的解决方案
3. 现在是下一个,索引 #2,这个改进了索引 #1 的解决方案,因为它有更少的损失 brs 和更大的价格。但它并没有改善指数 #0,因为它损失的柱线更少,而且价格也更少。所以我们将索引 #0 和索引 #2 保存为可能的解决方案。
4. 索引 #3 和 #4 有更多丢失的条柱和更少的价格,所以将它们作为可能的解决方案拒绝。
5. 最后,与索引 #0 相比,索引 #5 的损失柱数更少,价格更多,因此,索引 #0 的解被拒绝,最终的非支配值将是索引#2 和索引#5 的那些。
这是我迄今为止尝试过的。
#include <stdio.h>
int main(int argc, char** argv)
{
int lost_bars_array[] = {15, 10, 8, 15, 17, 9};
int prices_array[] = {35, 25, 30, 10, 15, 36};
int no_dominated[6];
int cont = 6;
for (int actual_pos = 0; actual_pos < cont; actual_pos++)
{
if (actual_pos == 0)
{
no_dominated[actual_pos] = actual_pos;
}
else
{
// Here's where I've the problems, I dont know how to handle the arrays
for (int p = 0; p < actual_pos; p++)
{
if (lost_bars_array[p] > lost_bars_array[p + 1] &&
prices_array[p] < prices_array[p + 1])
{
no_dominated[p] = p;
}
}
}
}
printf("\nThe non-dominated solutions index are: %d");
for (int i = 0; i < 6; i++)
{
printf(" %d ", no_dominated[i]);
}
printf("\n");
return 0;
}
代码应输出索引 2 5
作为解决方案。我希望我正确地解释了这个问题。
感谢任何帮助。提前致谢。
最佳答案
下面的程序可以满足您的需求,但我只是针对您的特定情况对其进行了测试;如果只有一个主导解决方案,或者在其他一些特殊情况下,它很可能会失败。但是你可以从这里开始工作。
两个注意事项:
您忘记包含可能的解决方案; bool 值 &&
只会保存保证更好的解决方案
最后一个双循环有点乱;哈希会更容易(解决方案索引将是哈希),但我认为这需要 c++ 或额外的库。
代码:
#include <stdio.h>
int main()
{
int lost_bars_array[] = {15,10,8,15,17,9};
int prices_array[] = {35,25,30,10,15,36};
int no_dominated[6];
int cont = 6;
int ndom = 0;
no_dominated[ndom] = 0;
ndom++;
for (int actual_pos = 1; actual_pos < cont; actual_pos++) {
int ntmp = ndom;
for(int p = 0; p < ntmp; p++) {
if (lost_bars_array[no_dominated[p]] > lost_bars_array[actual_pos] &&
prices_array[no_dominated[p]] < prices_array[actual_pos]) {
// Replace with a better solution
no_dominated[p] = actual_pos;
} else if (lost_bars_array[no_dominated[p]] > lost_bars_array[actual_pos] ||
prices_array[no_dominated[p]] < prices_array[actual_pos]) {
// Save as potential solution
no_dominated[ndom] = actual_pos;
ndom++;
}
}
}
// Remove double solutions
for (int i = 0; i < ndom; i++) {
for (int j = i; j < ndom; j++) {
if (no_dominated[i] == no_dominated[j]) {
ndom--;
}
}
}
printf("\nThe non-dominated solutions index are:");
for(int i = 0; i < ndom; i++)
printf(" %d ", no_dominated[i]);
printf("\n");
return 0;
}
关于c - 试图找到两个数组的非支配解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17421367/
我是 C++ 的新手,我在使用这段代码时遇到了问题: string output_date(int day, int month, int year){ string date; if
所以我这样做了 tar cvzf test.zip FP 为了创建目录 FP 的 zip 但是,它会列出 zip 中的目录 FP/ FP/php/ FP/php/pdf/ FP/php/docs/ F
我正在尝试在 Swift、Xcode 7.3(所以是 Swift 2.2)中创建一个通用类,但我似乎无法让它通过编译器: protocol Struct1Protocol { } struct Str
我的测试用例是这样的: class FooTest extends PHPUnit_Framework_TestCase { /** @covers MyClass::bar */ f
我正在尝试将brew install wine作为使electron-builder工作的一步。但是我所能得到的只是以下响应: ==> Installing dependencies for wine
我这样做: string[,] string1 = {{"one", "0"},{"Two", "5"},{"Three","1"}}; int b = 0; for(int i = 0; i <=
我正在尝试使用 SetWindowsHookEx 键盘 Hook Notepad.exe。 如您所见,工作线程正在将其 ASCII 代码(即 wParam)发送到指定的服务器。 UINT WINAPI
我正在尝试将 ListView 实现到我的 Fragment 中,但无论我尝试什么,我都会得到一个 NullPointerException。我检查对象是否为 null 并记录是否为 null,看起来
我尝试在一行中对齐两个 div。使用 float left 属性,一切顺利。但是当我在 div 中使用图像时,它开始产生问题。 所以这是我的示例代码:- Some headi
我目前正在使用此代码来获取图像的灰度图像表示并以 (512, 370, 1) 的格式表示它大批。 img_instance = cv2.imread(df.iloc[i][x_col]) / 255.
总结 我正在创建一个简单的应用程序,它允许用户选择一个包含顶级窗口的进程。用户首先键入 native DLL(而非托管 DLL)的路径。然后用户键入将在 Hook 过程中调用的方法的名称。该方法不得返
我是一名优秀的程序员,十分优秀!