C# 不创建随机结果的随机数
首先,我想说,我知道关于种子的真正随机性以及如何根据Environment.TickCount创建新的Random()的不同主题。 尽管如此,我还是不知道我的问题是什么。 我正在尝试创建一个100个图形的总体,用于图形着色遗传算法。 每一个群体都由一定数量的边和一定数量的节点组成。每个节点都有一个索引号和一个随机的初始颜色。 最初的颜色是我想要的。颜色是介于1和10之间的整数。 但是每个群体看起来都一样,相同的节点有相同的颜色。 我可能错过了一些简单的事情,但我就是想不出来。 任何帮助都将不胜感激 CreatePopulation.csC# 不创建随机结果的随机数,c#,random,C#,Random,首先,我想说,我知道关于种子的真正随机性以及如何根据Environment.TickCount创建新的Random()的不同主题。 尽管如此,我还是不知道我的问题是什么。 我正在尝试创建一个100个图形的总体,用于图形着色遗传算法。 每一个群体都由一定数量的边和一定数量的节点组成。每个节点都有一个索引号和一个随机的初始颜色。 最初的颜色是我想要的。颜色是介于1和10之间的整数。 但是每个群体看起来都一样,相同的节点有相同的颜色。 我可能错过了一些简单的事情,但我就是想不出来。 任何帮助都将不胜感
class CreatePopulation
{
private static Solution CreateRandomSolution(Graph graph, int colorsCount)
{
Solution solution = new Solution(graph, colorsCount);
for (int node = 0; node < graph.Nodes.Count; node++)
{
solution.assignColor(node, Program.RandomNumber(1, 10));
}
return solution;
}
public static List<Solution> CreateRandomPopulation(Graph graph, int populationSize)
{
List<Solution> list = new List<Solution>();
for (int i = 0; i < populationSize; i++)
{
list.Add(CreateRandomSolution(graph, 10));
}
return list;
}
}
Node.cs
namespace Graph
{
class Node
{
public int number { get; set; }
public int color { get; set; }
public Node(int number)
{
this.number = number;
}
public Node() { }
}
}
更新
更改了我在Solution.cs中创建新列表的方式,但想知道是否可以用更好的方式创建
public Solution(Graph graph, int colorsCount)
{
this.edges = graph.Vertices;
this.colorsCount = colorsCount;
this.nodes = new List<Node>(graph.Nodes.Count);
for (int i = 0; i < graph.Nodes.Count; i++)
{
this.nodes.Add(new Node{color = graph.Nodes[i].color, number = graph.Nodes[i].number});
}
}
公共解决方案(图形,int colorscont)
{
this.edges=图形顶点;
this.colorsunt=colorsunt;
this.nodes=新列表(graph.nodes.Count);
对于(int i=0;i
问题在于,您的解决方案中的每一个对象都来自列表
和列表
的同一实例。因此,Random
工作正常,真正的问题是在生成下一个解决方案时覆盖了以前解决方案的值
我看到您使用的唯一构造函数是
Solution solution = new Solution(graph, colorsCount);
你在那里
public Solution(Graph graph, int colorsCount)
{
edges = graph.Vertices;
this.colorsCount = colorsCount;
this.nodes = graph.Nodes;
}
这只是复制图形顶点
和图形节点
的引用,如果任何解决方案
修改集合中的一个项目,则所有解决方案都会被修改
要解决此问题,您需要对这两个列表进行深入复制。您没有显示顶点
或节点
是什么,但是如果它们只是类在做什么
public Solution(Graph graph, int colorsCount)
{
edges = graph.Vertices.ToList();
this.colorsCount = colorsCount;
this.nodes = graph.Nodes.ToList();
}
这还不够,因为这只会创建列表的浅表副本,即调用节点[index].color=color
仍将修改所有其他图形中的节点,因为尽管列表现在是单独的引用,但列表中的元素仍在解决方案
对象之间共享。你需要类似的东西
class Solution
{
private int colorsCount;
private List<Vertex> edges;
private List<Node> nodes;
private Solution(List<Vertex> edges, List<Node> nodes, int colorsCount)
{
this.colorsCount = colorsCount;
this.edges = edges;
this.nodes = nodes.Select(old => old.Clone()).ToList();
}
public Solution(Graph graph, int colorsCount)
{
edges = graph.Vertices;
this.colorsCount = colorsCount;
this.nodes = graph.Nodes.Select(old => old.Clone()).ToList();
}
public void assignColor(int index, int color)
{
nodes[index].color = color;
}
}
class Node
{
public int number { get; set; }
public int color { get; set; }
public Node(int number)
{
this.number = number;
}
public Node() { }
public Node Clone()
{
var newNode = new Node();
newNode.number = this.number;
newNode.color = this.color;
return newNode;
}
}
类解决方案
{
私人int COLORSCONT;
私有列表边缘;
私有列表节点;
专用解决方案(列表边、列表节点、int COLORSCONT)
{
this.colorsunt=colorsunt;
这个。边=边;
this.nodes=nodes.Select(old=>old.Clone()).ToList();
}
公共解决方案(图形、int COLORSCONT)
{
边=图的顶点;
this.colorsunt=colorsunt;
this.nodes=graph.nodes.Select(old=>old.Clone()).ToList();
}
公共颜色(整数索引,整数颜色)
{
节点[索引]。颜色=颜色;
}
}
类节点
{
公共整数{get;set;}
公共int颜色{get;set;}
公共节点(整数)
{
这个数字=数字;
}
公共节点(){}
公共节点克隆()
{
var newNode=新节点();
newNode.number=this.number;
newNode.color=this.color;
返回newNode;
}
}
问题在于,您的解决方案中的每一个对象都来自列表
和列表
的同一实例。因此,Random
工作正常,真正的问题是在生成下一个解决方案时覆盖了以前解决方案的值
我看到您使用的唯一构造函数是
Solution solution = new Solution(graph, colorsCount);
你在那里
public Solution(Graph graph, int colorsCount)
{
edges = graph.Vertices;
this.colorsCount = colorsCount;
this.nodes = graph.Nodes;
}
这只是复制图形顶点
和图形节点
的引用,如果任何解决方案
修改集合中的一个项目,则所有解决方案都会被修改
要解决此问题,您需要对这两个列表进行深入复制。您没有显示顶点
或节点
是什么,但是如果它们只是类在做什么
public Solution(Graph graph, int colorsCount)
{
edges = graph.Vertices.ToList();
this.colorsCount = colorsCount;
this.nodes = graph.Nodes.ToList();
}
这还不够,因为这只会创建列表的浅表副本,即调用节点[index].color=color
仍将修改所有其他图形中的节点,因为尽管列表现在是单独的引用,但列表中的元素仍在解决方案
对象之间共享。你需要类似的东西
class Solution
{
private int colorsCount;
private List<Vertex> edges;
private List<Node> nodes;
private Solution(List<Vertex> edges, List<Node> nodes, int colorsCount)
{
this.colorsCount = colorsCount;
this.edges = edges;
this.nodes = nodes.Select(old => old.Clone()).ToList();
}
public Solution(Graph graph, int colorsCount)
{
edges = graph.Vertices;
this.colorsCount = colorsCount;
this.nodes = graph.Nodes.Select(old => old.Clone()).ToList();
}
public void assignColor(int index, int color)
{
nodes[index].color = color;
}
}
class Node
{
public int number { get; set; }
public int color { get; set; }
public Node(int number)
{
this.number = number;
}
public Node() { }
public Node Clone()
{
var newNode = new Node();
newNode.number = this.number;
newNode.color = this.color;
return newNode;
}
}
类解决方案
{
私人int COLORSCONT;
私有列表边缘;
私有列表节点;
专用解决方案(列表边、列表节点、int COLORSCONT)
{
this.colorsunt=colorsunt;
这个。边=边;
this.nodes=nodes.Select(old=>old.Clone()).ToList();
}
公共解决方案(图形、int COLORSCONT)
{
边=图的顶点;
this.colorsunt=colorsunt;
this.nodes=graph.nodes.Select(old=>old.Clone()).ToList();
}
公共颜色(整数索引,整数颜色)
{
节点[索引]。颜色=颜色;
}
}
类节点
{
公共整数{get;set;}
公共int颜色{get;set;}
公共节点(整数)
{
这个数字=数字;
}
公共节点(){}
公共节点克隆()
{
var newNode=新节点();
newNode.number=this.number;
newNode.color=this.color;
返回newNode;
}
}
不要在问题中添加不准确的标记。问题是您的RandomNumber()
方法每次返回相同的整数值吗?@user1666620在循环中通过单个图中的所有节点它有不同的值,但所有图(在填充中有100个)都是相同的。我希望图形之间的节点颜色不同当您设置breeakpoints并通过代码进行调试时,color
参数是否传递给assignColor