<commit_before>from __future__ import print_function

from cyclopts.instance import ExGroup, ExNode, ExArc, ExSolution, ExSolver, Run
from cyclopts.params import Incrementer
from cyclopts.dtypes import xd_exgroup, xd_exnode, xd_exarc

import numpy as np

import nose
from nose.tools import assert_equal, assert_true

def gprint(g):
    print(g.id, g.kind, g.caps, g.qty)

def aprint(a):
    print("id: {id}, unode: {0}, vnode: {1}\npref: {2}, flow: {3}".format(
            a.uid, a.vid, a.pref, a.flow, id=a.id))

"""This tests all possible features of a resource exchange graph generated using
a Cyclopts instance:

#. request groups with one and more than one request
#. bid groups with one and more than one bid
#. exclusive and non-exclusive bids to an exclusive request
#. exclusive bid groupings
#. multiple bids for a single request
#. single bid to multiple requests

The graph structure tested is

.. image:: ../test_inst.svg

Each node group has a single constraint, and all unit capacities are equal to 1.

=======  ========
Group Constraints
-----------------
GId      Value
=======  ========
1        1
2        1.5
3        1
4        2
5        1
6        1
=======  ========
        
Each node has an associated maximum quantity

=======  ========
Node Quantities
-----------------
NId      Value
=======  ========
1        1
2        1.5
3        1.5
4        1
5        2
6        1
7        1
8        1
=======  ========

Nodes 1, 6, and 7 are exclusive with nodes 6 and 7 forming an exclusive bid
group (ie., only one can be matched).

Finally, preferences for arcs are ranked in the following order: 3, 2, 1, 4,
5. This ordering guarantees that the exclusive bid grouping capability is
tested, because arcs 3 and 2 correspond to nodes 6 and 7, and are of highest
rank.
"""   
def test_inst():
    req = True
    bid = False
    excl = True
    
    gid = Incrementer()
    # constraint for qty needed for cbc/clp and actual qty needed for greedy
    rg1 = ExGroup(gid.next(), req, np.array([1], dtype='float'), 1)
    rg2 = ExGroup(gid.next(), req, np.array([1.5], dtype='float'), 1.5)
    # there is a bug in the greedy solver, this should be able to be [1, < 0.5]
    rg3 = ExGroup(gid.next(), req, np.array([1, 0.5], dtype='float'), 1)
    bg1 = ExGroup(gid.next(), bid, np.array([2], dtype='float'))
    bg2 = ExGroup(gid.next(), bid, np.array([1.5, 2], dtype='float'))
    bg3 = ExGroup(gid.next(), bid, np.array([1], dtype='float'))
    grps = np.array([rg1, rg2, rg3, bg1, bg2, bg3], dtype=xd_exgroup)
 
    nid = Incrementer()
    ex_grp_id = Incrementer(1)
    r11 = ExNode(nid.next(), rg1.id, req, 1, excl, ex_grp_id.next())
    r21 = ExNode(nid.next(), rg2.id, req, 1.5)
    r22 = ExNode(nid.next(), rg2.id, req, 1.5)
    r31 = ExNode(nid.next(), rg3.id, req, 1)
    b11 = ExNode(nid.next(), bg1.id, bid, 2)
    b21 = ExNode(nid.next(), bg2.id, bid, 1, excl, ex_grp_id.next())
    b22 = ExNode(nid.next(), bg2.id, bid, 1, excl, b21.excl_id)
    b31 = ExNode(nid.next(), bg3.id, bid, 1)
    nodes = np.array([r11, r21, r22, r31, b11, b21, b22, b31], dtype=xd_exnode)

    # p3 > p2 > p1 > p4 > p5
    prefs = [1.0 / 2**i for i in range(5)]
    aid = Incrementer()
    a1 = ExArc(aid.next(), 
               r11.id, np.array([1], dtype='float'), <commit_msg>fixed cyclus greedy bugs<commit_after>from __future__ import print_function

from cyclopts.instance import ExGroup, ExNode, ExArc, ExSolution, ExSolver, Run
from cyclopts.params import Incrementer
from cyclopts.dtypes import xd_exgroup, xd_exnode, xd_exarc

import numpy as np

import nose
from nose.tools import assert_equal, assert_true

def gprint(g):
    print(g.id, g.kind, g.caps, g.qty)

def aprint(a):
    print("id: {id}, unode: {0}, vnode: {1}\npref: {2}, flow: {3}".format(
            a.uid, a.vid, a.pref, a.flow, id=a.id))

"""This tests all possible features of a resource exchange graph generated using
a Cyclopts instance:

#. request groups with one and more than one request
#. bid groups with one and more than one bid
#. exclusive and non-exclusive bids to an exclusive request
#. exclusive bid groupings
#. multiple bids for a single request
#. single bid to multiple requests

The graph structure tested is

.. image:: ../test_inst.svg

Each node group has a single constraint, and all unit capacities are equal to 1.

=======  ========
Group Constraints
-----------------
GId      Value
=======  ========
1        1
2        1.5
3        1
4        2
5        1
6        1
=======  ========
        
Each node has an associated maximum quantity

=======  ========
Node Quantities
-----------------
NId      Value
=======  ========
1        1
2        1.5
3        1.5
4        1
5        2
6        1
7        1
8        1
=======  ========

Nodes 1, 6, and 7 are exclusive with nodes 6 and 7 forming an exclusive bid
group (ie., only one can be matched).

Finally, preferences for arcs are ranked in the following order: 3, 2, 1, 4,
5. This ordering guarantees that the exclusive bid grouping capability is
tested, because arcs 3 and 2 correspond to nodes 6 and 7, and are of highest
rank.
"""   
def test_inst():
    req = True
    bid = False
    excl = True
    
    gid = Incrementer()
    rg1 = ExGroup(gid.next(), req, np.array([1], dtype='float'), 1)
    rg2 = ExGroup(gid.next(), req, np.array([1.5], dtype='float'), 1.5)
    rg3 = ExGroup(gid.next(), req, np.array([1, 0.4], dtype='float'), 1)
    bg1 = ExGroup(gid.next(), bid, np.array([2], dtype='float'))
    bg2 = ExGroup(gid.next(), bid, np.array([1.5, 2], dtype='float'))
    bg3 = ExGroup(gid.next(), bid, np.array([1], dtype='float'))
    grps = np.array([rg1, rg2, rg3, bg1, bg2, bg3], dtype=xd_exgroup)
 
    nid = Incrementer()
    ex_grp_id = Incrementer(1)
    r11 = ExNode(nid.next(), rg1.id, req, 1, excl, ex_grp_id.next())
    r21 = ExNode(nid.next(), rg2.id, req, 1.5)
    r22 = ExNode(nid.next(), rg2.id, req, 1.5)
    r31 = ExNode(nid.next(), rg3.id, req, 1)
    b11 = ExNode(nid.next(), bg1.id, bid, 2)
    b21 = ExNode(nid.next(), bg2.id, bid, 1, excl, ex_grp_id.next())
    b22 = ExNode(nid.next(), bg2.id, bid, 1, excl, b21.excl_id)
    b31 = ExNode(nid.next(), bg3.id, bid, 1)
    nodes = np.array([r11, r21, r22, r31, b11, b21, b22, b31], dtype=xd_exnode)

    # p3 > p2 > p1 > p4 > p5
    prefs = [1.0 / 2**i for i in range(5)]
    aid = Incrementer()
    a1 = ExArc(aid.next(), 
               r11.id, np.array([1], dtype='float'), 
{'fraction_non_alphanumeric': 0.16428349393845199, 'fraction_numerical': 0.04491762511656823, 'mean_word_length': 2.7830687830687832, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1168048', 'n_tokens_mistral': 2448, 'n_tokens_neox': 2256, 'n_words': 777}