gpt4 book ai didi

Java Jung 对象实例化

转载 作者:行者123 更新时间:2023-12-02 07:22:09 26 4
gpt4 key购买 nike

我正在尝试从 Java Jung Graph 包构建以下内容:EdmondsKarp Object

EdmondsKarpMaxFlow<MyNode, MyLink> alg = new EdmondsKarpMaxFlow<MyNode, MyLink>(g, n2, n5, capTransformer, edgeFlowMap, flowEdgeFactory);

我不知道为什么会出现错误,这是来自 NetBeans 的错误消息:
reason: actual argument org.apache.commons.collections15.Transformer<selfsimilarity.MyLink,java.lang.Double> cannot be converted to org.apache.commons.collections15.Transformer<selfsimilarity.MyLink,java.lang.Number> by method invocation conversion

我基本上遵循tutorial .

EdmondsKarpMaxFlow 实例化位于构造函数的底部。在构造函数之后,我将所有参数的代码提供给 EdmondsKarp 对象。这些参数是图、源节点和汇节点、容量变压器、 map 和边缘工厂。

public GraphAlgTests()
{
g = new DirectedSparseMultigraph<MyNode, MyLink>();
// Create some MyNode objects to use as vertices
MyNode n1 = new MyNode(1); MyNode n2 = new MyNode(2); MyNode n3 = new MyNode(3);
MyNode n4 = new MyNode(4); MyNode n5 = new MyNode(5);
// Add some directed edges along with the vertices to the graph
g.addEdge(new MyLink(2.0, 48),n1, n2, EdgeType.DIRECTED); // This method
g.addEdge(new MyLink(2.0, 48),n2, n3, EdgeType.DIRECTED);
g.addEdge(new MyLink(3.0, 192), n3, n5, EdgeType.DIRECTED);
g.addEdge(new MyLink(2.0, 48), n5, n4, EdgeType.DIRECTED); // or we can use
g.addEdge(new MyLink(2.0, 48), n4, n2); // In a directed graph the
g.addEdge(new MyLink(2.0, 48), n3, n1); // first node is the source
g.addEdge(new MyLink(10.0, 48), n2, n5);// and the second the destination

EdmondsKarpMaxFlow<MyNode, MyLink> alg = new EdmondsKarpMaxFlow<MyNode, MyLink>(g, n2, n5, capTransformer, edgeFlowMap, flowEdgeFactory);
}

public class MyNode
{
private int id; // good coding practice would have this as private
public MyNode(int id)
{
this.id = id;
}
public String toString()
{ // Always a good idea for debuging
return "v"+id; // JUNG2 makes good use of these.
}
}


public class MyLink
{
double capacity; // should be private
double weight; // should be private for good practice
int id;
int edgeCount = 0;

public MyLink(double weight, double capacity)
{
this.id = edgeCount++;
this.weight = weight;
this.capacity = capacity;
}
public String toString()
{ // Always good for debugging
return "E"+id;
}
}

Transformer<MyLink, Double> capTransformer = new Transformer<MyLink, Double>()
//INFO: Gives the capacity of an edge, denoted here as a "link"
{
@Override
public Double transform(MyLink link)
{
return link.capacity;
}
};

Map<MyLink, Double> edgeFlowMap = new HashMap<MyLink, Double>();
Factory<MyLink> flowEdgeFactory = new Factory<MyLink>()
{
@Override
public MyLink create()
{
return new MyLink(1.0, 1.0);
}
};

如果还有什么可以帮助的,请告诉我!感谢您的帮助!

最佳答案

它准确地告诉您 Javadocs 是什么对于那个构造函数告诉你:

public EdmondsKarpMaxFlow(DirectedGraph<V,E> directedGraph,
V source,
V sink,
org.apache.commons.collections15.Transformer<E,Number> edgeCapacityTransformer,
Map<E,Number> edgeFlowMap,
org.apache.commons.collections15.Factory<E> edgeFactory)

您的Transformer<MyLink, Double>当需要时<MyLink, Number> (并且需要定义其中的 transform() 方法以返回 Number )

这可能在 API 中的某个时刻发生了更改,并且您正在使用的教程已过时。 API 现在很可能正在调用 doubleValue()关于Number您将返回的对象。

您的 edgeFlowMap 似乎也是如此。以及。

关于Java Jung 对象实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14084958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com