Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:construct large random village
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:bd58c1ec81e088ca79e3ff8127019395938dabcf
User & Date: ravenspoint 2016-05-30 14:20:57
Context
2016-05-30
15:21
check for stop condition check-in: b91609e688 user: ravenspoint tags: trunk
14:20
construct large random village check-in: bd58c1ec81 user: ravenspoint tags: trunk
13:47
fix handling of leaves in DFS check-in: 70b33f7e96 user: ravenspoint tags: trunk
Changes

Changes to trade.cpp.

10
11
12
13
14
15
16
























17
18
19
20
21
22
23
..
75
76
77
78
79
80
81




82
83
84
85
86
87
88
...
117
118
119
120
121
122
123

























124
125
126
127
128
129
130
...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
...
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <boost/property_map/property_map.hpp>
#include <boost/graph/depth_first_search.hpp>

using namespace std;
using namespace boost;

#include "trade.h"

























void cTrader::Display()
{
    if( myPrice >= 1 )
        cout << "Gives 1 " << Sell() << " For " << myPrice <<" "<< Buy() << endl;
    else
    {
................................................................................
void cVillage::AddTrader(
    eCommodity sell,
    eCommodity buy,
    float price )
{
    myGraph[ add_vertex( myGraph ) ] = cTrader( sell, buy, price );
}





void cVillage::ConstructProblem1()
{
    static eCommodity hold = eCommodity::wood;
    static float quantity = 1;
    myTraveler.myHold = &hold;
    myTraveler.myQuantity = &quantity;
................................................................................

    AddTrader( eCommodity::wood, eCommodity::sand, 2 );
    AddTrader( eCommodity::sand, eCommodity::gras, 0.5 );
    AddTrader( eCommodity::gras, eCommodity::sand, 1);
    AddTrader( eCommodity::sand, eCommodity::wood, 0.25);
    AddTrader( eCommodity::gras, eCommodity::wood, 0.3333);
}


























void cVillage::Display()
{
    int index = 0;
    graph_traits<trade_graph_t>::vertex_iterator vi, vi_end;
    for (boost::tie(vi, vi_end) = vertices(myGraph); vi != vi_end; ++vi)
    {
................................................................................
    // The two most recent traders visited
    unsigned int src, dst;
    myTraveler.src = &src;
    myTraveler.dst = &dst;

    // Do a fixed number of searches
    int count = 0;
    while( count < 5 )
    {
        try
        {
            // go back to go
            myTraveler.Restart();

            // do a depth first search, trading as we go
................................................................................
        cout << v << " ";
    cout << endl;
}

int main()
{
    cVillage village;
    village.ConstructProblem2();
    village.Display();
    village.Travel();

    return 0;
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







 







|





10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
..
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
...
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
...
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <boost/property_map/property_map.hpp>
#include <boost/graph/depth_first_search.hpp>

using namespace std;
using namespace boost;

#include "trade.h"

cTrader::cTrader( int count )
{
    // set up random generator
    static std::random_device rd;
    static std::mt19937_64 gen(rd());
    static std::uniform_int_distribution<int> dis_com(0,count-1);
    static std::uniform_int_distribution<int> dis_price(0,7);

    // commodities
    mySell = (eCommodity) dis_com( gen );
    do
    {
        myBuy  = (eCommodity) dis_com( gen );
    }
    while( mySell == myBuy );

    // price
    myPrice = dis_price( gen );
    if( myPrice < 4 )
        myPrice = 1 / (myPrice+1);
    else
        myPrice = myPrice - 2;
}

void cTrader::Display()
{
    if( myPrice >= 1 )
        cout << "Gives 1 " << Sell() << " For " << myPrice <<" "<< Buy() << endl;
    else
    {
................................................................................
void cVillage::AddTrader(
    eCommodity sell,
    eCommodity buy,
    float price )
{
    myGraph[ add_vertex( myGraph ) ] = cTrader( sell, buy, price );
}
void cVillage::AddTrader( int count )
{
    myGraph[ add_vertex( myGraph ) ] = cTrader( count );
}

void cVillage::ConstructProblem1()
{
    static eCommodity hold = eCommodity::wood;
    static float quantity = 1;
    myTraveler.myHold = &hold;
    myTraveler.myQuantity = &quantity;
................................................................................

    AddTrader( eCommodity::wood, eCommodity::sand, 2 );
    AddTrader( eCommodity::sand, eCommodity::gras, 0.5 );
    AddTrader( eCommodity::gras, eCommodity::sand, 1);
    AddTrader( eCommodity::sand, eCommodity::wood, 0.25);
    AddTrader( eCommodity::gras, eCommodity::wood, 0.3333);
}

void cVillage::ConstructProblem3()
{
    static eCommodity hold = eCommodity::wood;
    static float quantity = 1;
    myTraveler.myHold = &hold;
    myTraveler.myQuantity = &quantity;
    myTraveler.myInitiallHold = hold;
    myTraveler.myInitialQuantity = quantity;

    static vector<int> path;
    myTraveler.myPath = &path;

    AddTrader( eCommodity::wood,  eCommodity::wood, 1);

    AddTrader( eCommodity::wood, eCommodity::sand, 2 );
    AddTrader( eCommodity::sand, eCommodity::gras, 0.5 );
    AddTrader( eCommodity::gras, eCommodity::sand, 1);
    AddTrader( eCommodity::sand, eCommodity::wood, 0.25);
    AddTrader( eCommodity::gras, eCommodity::wood, 0.3333);

    // Add some random traders
    for( int k = 0; k < 200; k++ )
        AddTrader( 200 );
}

void cVillage::Display()
{
    int index = 0;
    graph_traits<trade_graph_t>::vertex_iterator vi, vi_end;
    for (boost::tie(vi, vi_end) = vertices(myGraph); vi != vi_end; ++vi)
    {
................................................................................
    // The two most recent traders visited
    unsigned int src, dst;
    myTraveler.src = &src;
    myTraveler.dst = &dst;

    // Do a fixed number of searches
    int count = 0;
    while( count < 500 )
    {
        try
        {
            // go back to go
            myTraveler.Restart();

            // do a depth first search, trading as we go
................................................................................
        cout << v << " ";
    cout << endl;
}

int main()
{
    cVillage village;
    village.ConstructProblem3();
    village.Display();
    village.Travel();

    return 0;
}

Changes to trade.h.

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43





44
45
46
47
48
49
50
..
55
56
57
58
59
60
61




62







63
64
65
66
67
68
69
...
201
202
203
204
205
206
207





208
209
210
211

212
213
214


215
216
217
218
219
220
221

    /** Default constructor

    Used by the STL for placeholders - do not use in application code
     */
    cTrader() {}

    /** Construct
        @param[in] sell commodity
        @param[in] buy commodity
        @param[in] price
    */
    cTrader( eCommodity sell,
             eCommodity buy,
             float price )
        : mySell( sell )
        , myBuy( buy )
        , myPrice( price )
    {

    }





    void Display();

    const string Sell() const
    {
        return Name( mySell );
    }
    const string Buy() const
................................................................................
    {
        static vector< string > Names
        {
            "wood",
            "sand",
            "gras",
        };




        return Names[ (int) c ];







    }
};
/** The trip between two traders.

There is nothing here.

This is represented by an edge in the graph.  The edge cannot be
................................................................................
                            needed to purchase one unit of the sell commodity
     */
    void AddTrader(
        eCommodity sell,
        eCommodity buy,
        float price );






    /** Construct test problems */

    void ConstructProblem1();
    void ConstructProblem2();


    /** Display the traders ( problem statement ) */
    void Display();


    void DisplayGraph();

    /** Construct potentialy profitable trips ( edges ) between traders

    There is no point in travelling between two traders
    unless the source is selling what the target is buying








|













>
>
>
>
>







 







>
>
>
>
|
>
>
>
>
>
>
>







 







>
>
>
>
>




>



>
>







23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
..
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

    /** Default constructor

    Used by the STL for placeholders - do not use in application code
     */
    cTrader() {}

    /** Construct specified trader
        @param[in] sell commodity
        @param[in] buy commodity
        @param[in] price
    */
    cTrader( eCommodity sell,
             eCommodity buy,
             float price )
        : mySell( sell )
        , myBuy( buy )
        , myPrice( price )
    {

    }
    /** Construct random trader
    @param[in] count the number of different commodities
    */
    cTrader( int count );

    void Display();

    const string Sell() const
    {
        return Name( mySell );
    }
    const string Buy() const
................................................................................
    {
        static vector< string > Names
        {
            "wood",
            "sand",
            "gras",
        };

        string ret;

        if( (int) c < 3 )
            ret = Names[ (int) c ];
        else
        {
            stringstream sst;
            sst << "c" << (int) c;
            ret = sst.str();
        }
        return ret;
    }
};
/** The trip between two traders.

There is nothing here.

This is represented by an edge in the graph.  The edge cannot be
................................................................................
                            needed to purchase one unit of the sell commodity
     */
    void AddTrader(
        eCommodity sell,
        eCommodity buy,
        float price );

    /** Add random trader
        @param[in] count of commodities
    */
    void AddTrader( int count );

    /** Construct test problems */

    void ConstructProblem1();
    void ConstructProblem2();
    void ConstructProblem3();

    /** Display the traders ( problem statement ) */
    void Display();

    /** Display potentially profitable trips betwee traders */
    void DisplayGraph();

    /** Construct potentialy profitable trips ( edges ) between traders

    There is no point in travelling between two traders
    unless the source is selling what the target is buying