8000 How can I print the result using Python API? · Issue #215 · kahypar/kahypar · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

How can I print the result using Python API? #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gzzyyxh opened this issue Nov 21, 2024 · 6 comments
Open

How can I print the result using Python API? #215

gzzyyxh opened this issue Nov 21, 2024 · 6 comments

Comments

@gzzyyxh
Copy link
gzzyyxh commented Nov 21, 2024

Is there anything missing from the code shown on the website?

import os
import kahypar as kahypar

num_nodes = 7
num_nets = 4

hyperedge_indices = [0,2,6,9,12]
hyperedges = [0,2,0,1,3,4,3,4,6,2,5,6]

node_weights = [1,2,3,4,5,6,7]
edge_weights = [11,22,33,44]

k=2

hypergraph = kahypar.Hypergraph(num_nodes, num_nets, hyperedge_indices, hyperedges, k, edge_weights, node_weights)

context = kahypar.Context()
context.loadINIconfiguration("<path/to/config>/km1_kKaHyPar_sea20.ini")

context.setK(k)
context.setEpsilon(0.03)

kahypar.partition(hypergraph, context)

HOW can I print the partitioning result?

@SebastianSchlag
Copy link
Member

Hey @gzzyyxh ,

You can iterate over all the nodes of the hypergraph by using

.def("nodes", [](Hypergraph &h) {

and then call

.def("blockID", &Hypergraph::partID,

for each of the nodes.

@SebastianSchlag
Copy link
Member
SebastianSchlag commented Nov 22, 2024

If you want to write out the actual partition file, you can add write-partition=1 to the .ini config file you use for partitioning. That should create a partition file named <hypergraph>.hgr.part<k>.epsilon<epislon>.seed<seed>.KaHyPar in the same folder as the actual hypergraph.

@gzzyyxh
Copy link
Author
gzzyyxh commented Nov 22, 2024

Hey@SebastianSchlag ,
Thank you for getting back to me. I'm sorry, but I don't understand how to use the code you mentioned. Could you provide a simple example? I added write-partition=1 to the km1_kKaHyPar_sea20.ini configuration file, but nothing is generated after running it.

@N-Maas
Copy link
Collaborator
N-Maas commented Nov 22, 2024

A usage example would be this:

for node in hypergraph.nodes():
  blockID = hypergraph.blockID(node)
  print(f"Node {node} is mapped to block {blockID}")

@gzzyyxh
Copy link
Author
gzzyyxh commented Nov 23, 2024

Hi @N-Maas ,
Thanks so much. Last question:
I added two for-loop to print the result:

for node in hypergraph.nodes():
    blockID = hypergraph.blockID(node)
    print(f"Node {node} is mapped to block {blockID}")

for net in hypergraph.edges():
    blockID = hypergraph.blockID(net)
    print(f"Net {net} is mapped to block {blockID}")

And I got this output:

Objectives: 
Hyperedge Cut  (minimize) = 99 
SOED           (minimize) = 198 
(k-1)          (minimize) = 99 
Absorption     (maximize) = 64.1667 
Imbalance                 = 0 

Partition sizes and weights:  
|part 0 | = 3  w( 0 ) = 14 
|part 1 | = 4  w( 1 ) = 14 
...
Node 0 is mapped to block 1
Node 1 is mapped to block 0
Node 2 is mapped to block 1
Node 3 is mapped to block 1
Node 4 is mapped to block 0
Node 5 is mapped to block 1
Node 6 is mapped to block 0
Net 0 is mapped to block 1
Net 1 is mapped to block 0
Net 2 is mapped to block 1
Net 3 is mapped to block 1

Could you tell me what's SODE, (k-1), Absorption?
And, as I have learned, the net is said to be cut if it connects more than one part (block here), uncut otherwise. So what does "Net 1 is mapped to block 0" mean? Net 1 connects two blocks here. I think it should be outside, not in any blocks.

@SebastianSchlag
Copy link
Member
SebastianSchlag commented Nov 24, 2024

Could you tell me what's SODE, (k-1), Absorption?

These are other quality metrics. SOED is the shorthand for the Sum-Of-External-Degrees metric, (k-1) is the shorthand for the connectivity (sometimes also called ($\lambda-1$)) metric.

And, as I have learned, the net is said to be cut if it connects more than one part (block here), uncut otherwise. So what does "Net 1 is mapped to block 0" mean? Net 1 connects two blocks here. I think it should be outside, not in any blocks.

The blockID method returns the block the vertex with the given id is assigned to. Nets are not assigned to any blocks. So this code:

for net in hypergraph.edges():
    blockID = hypergraph.blockID(net)
    print(f"Net {net} is mapped to block {blockID}")

actually just returns the block IDs of the first 4 vertices (with ids 0, 1, 2, and 3), since hypergraph.edges() just enumerates the ids of all the edges in the hypergraph.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0