我有两个类,一个用于定义算法参数,另一个用于实现算法:
1级(算法参数):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VM_Placement
{
public static class AlgorithmParameters
{
public static int pop_size = 100;
public static double crossover_rate = 0.7;
public static double mutation_rate = 0.001;
public static int chromo_length = 300;
public static int gene_length = 4;
public static int max_allowable_generations = 400;
static Random rand = new Random();
public static double random_num = rand.NextDouble();
}
}
Run Code Online (Sandbox Code Playgroud)
2级(实现算法):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VM_Placement
{ …Run Code Online (Sandbox Code Playgroud) 我在一个类中有2个函数,并且在调用ParseBits()函数时出现错误即" int num_elements = ParseBits(bits, buffer);"因为我传递的"缓冲区"争论" public int ParseBits(string bits, int* buffer)":
功能1:
public float AssignFitness(string bits, int target_value)
{
//holds decimal values of gene sequence
int[] buffer = new int[(VM_Placement.AlgorithmParameters.chromo_length / VM_Placement.AlgorithmParameters.gene_length)];
int num_elements = ParseBits(bits, buffer);
// ok, we have a buffer filled with valid values of: operator - number - operator - number..
// now we calculate what this represents.
float result = 0.0f;
for (int i=0; i < num_elements-1; i+=2)
{
switch (buffer[i])
{ …Run Code Online (Sandbox Code Playgroud) 我想在我的程序中使用一些全局变量.我们有什么可以帮助直接定义全局变量,因为我们在C++中有#define.
例如:假设我在C++中有下面提到的全局变量:
#define CROSSOVER_RATE 0.7
#define MUTATION_RATE 0.001
#define POP_SIZE 100
#define CHROMO_LENGTH 300
#define GENE_LENGTH 4
#define MAX_ALLOWABLE_GENERATIONS 400
Run Code Online (Sandbox Code Playgroud)
我希望在我的C#程序中将它们定义为全局变量.请让我知道我该怎么办?
当我执行以下代码时,我得到map/set迭代器不可递增的错误.
typedef std::multimap<int, int> MapType;
assgnt::MapType my_map;
assgnt::MapType::iterator it;
for(it = my_map.begin(); it != my_map.end(); )
{
my_map = obj1.addGoodNeighbours(it->first, it->second, my_map);
++it;
}
Run Code Online (Sandbox Code Playgroud)
请帮忙
我有一个类定义了几个全局变量,如下所示:
namespace Algo
{
public static class AlgorithmParameters
{
public int pop_size = 100;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的另一个csharp文件中,它也包含main(),在main()中我声明了一个类型结构数组,数组大小为pop_size,但是我收到了一些错误"chromo_typ Population[AlgorithmParameters.pop_size];".请在下面找到代码.我是否使用不正确的语法进行可变长度大小的数组声明?
namespace Algo
{
class Program
{
struct chromo_typ
{
string bits;
float fitness;
chromo_typ() {
bits = "";
fitness = 0.0f;
}
chromo_typ(string bts, float ftns)
{
bits = bts;
fitness = ftns;
}
};
static void Main(string[] args)
{
while (true)
{
chromo_typ Population[AlgorithmParameters.pop_size];
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
Bad array declarator: To declare a managed array the rank …Run Code Online (Sandbox Code Playgroud) 我在我的程序中使用了一个结构如下:
public struct chromo_typ
{
public string bits;
public float fitness;
chromo_typ(string bts, float ftns)
{
bits = bts;
fitness = ftns;
}
};
Run Code Online (Sandbox Code Playgroud)
我正在使用结构中定义的构造函数,即我的main()中的chromo_typ(string bts,float ftns).我的main()包含以下代码:
chromo_typ[] temp = new chromo_typ[VM_Placement.AlgorithmParameters.pop_size];
chromo_typ ct = new chromo_typ();
int cPop = 0;
//loop until we have created POP_SIZE new chromosomes
while (cPop < VM_Placement.AlgorithmParameters.pop_size)
{
// we are going to create the new population by grabbing members of the old population
// two at a time via roulette wheel selection.
string …Run Code Online (Sandbox Code Playgroud)