コード例 #1
0
ファイル: create_visual.php プロジェクト: gabriel89/simulator
function createVisual($con)
{
    $nodes = checkNodesGlobalVariable($con);
    $products = checkProductsGlobalVariable($con);
    $pairs = '';
    $visual = '';
    $betweenness = getBetweenness();
    foreach ($nodes as $row) {
        $visual .= 'n' . $row['id'] . '{';
        //set node visual properties
        $visual .= 'shape:dot, color:' . genColor($row['links'], $betweenness[0], $betweenness[1]) . ', ';
        // set serves and requests
        if ($row['serves']) {
            $visual .= 'serves: ' . str_replace('^', ', ', $row['serves']) . ',';
        }
        if ($row['requests']) {
            $visual .= ' requests: ' . str_replace('^', ', ', $row['requests']) . ',';
        }
        // set wealth
        $visual .= ' money: ' . $row['money'];
        // add new-lines
        $visual .= "}\n\n";
        // set links
        if (isset($row['links'])) {
            foreach (explode(',', $row['links']) as $value) {
                // hacky solution, but avoids duplicates by searching for a simple |n0-n1| pairs in a string
                if (strpos($pairs, '|n' . $value . '-n' . $row['id'] . '|') === false) {
                    $pairs .= '|n' . $row['id'] . '-n' . $value . '|';
                    $visual .= 'n' . $row['id'] . "--n{$value} {color: #777777, weight: 1.5}\n\n";
                }
            }
        }
    }
    return $visual;
}
コード例 #2
0
ファイル: simulator.php プロジェクト: gabriel89/simulator
function consumerPhase($con)
{
    global $nodes;
    $nodes = checkNodesGlobalVariable($con);
    // retrieve a list of lists representing an economic path from producer to consumer
    $consumer_path = getConsumerPath($con, $nodes);
    // make transaction for each possible consumer
    finalizeTransaction($con, $consumer_path);
    // unset $consumer_path array once it is not used anymore
    unset($consumer_path);
}
コード例 #3
0
function getConsumerPath($con)
{
    global $nodes;
    $nodes = checkNodesGlobalVariable($con);
    $consumer_path = [];
    addToLog("\n\n\n;--------------------------------\n;        ESTABLISHING TRANSACTION PATH\n;--------------------------------");
    for ($b = 0; $b < sizeof($nodes); $b++) {
        $list = [];
        $providers = getProviders($b);
        for ($p = 0; $p < sizeof($providers); $p++) {
            if ($providers[$p] != null) {
                $list = array_merge($list, array(BFS($b, $providers[$p])));
            } else {
                echo "<br />No providers found for {$b} <br /><br />";
            }
        }
        $consumer_path = array_merge($consumer_path, $list);
        addToLog("\n\n" . implode("\n", str_replace(',', ' -> ', $list)));
    }
    return $consumer_path;
}
コード例 #4
0
function read_CSV($content, $con)
{
    global $nodes;
    global $products;
    $nodes = checkNodesGlobalVariable($con);
    $products = checkProductsGlobalVariable($con);
    // get all the lines of the csv file
    $lines = explode("\n", $content);
    // get rid of first line of the file within the varable
    $lines = removeNormalizationFlag($lines);
    // get the number of nodes
    $node_count = sizeof(explode(";", $lines[0])) - 1;
    // get rid of first line of the file | DON'T NEED ANYMORE
    array_shift($lines);
    // iterate through all the lines of the file
    // line $i corresponds to $nodes[$i]'s $links
    for ($i = 0; $i < $node_count; $i++) {
        // get corresponding line
        $line_links = explode(";", $lines[$i]);
        // get rid of first element | REDUNDANT
        array_shift($line_links);
        // prepare variables corresponding to table fields
        $ID = '';
        $links = '';
        $requests = '';
        $serves = '';
        $quantity = '';
        $money = '';
        //set ID
        $ID = $i;
        //set links
        // TODO!: when a node has 0 links, $links will be empty and will cause an error
        for ($j = 0; $j < $node_count; $j++) {
            if ($line_links[$j] != 0) {
                // node $i has link to node $j
                $links .= $j . ',';
            }
        }
        //remove tailing ','
        $links = trim($links, ',');
        //set serves and quantity
        $idx = empty($products) ? 0 : floor(frand(sizeof($products)) % sizeof($products));
        $serves = 'P' . $idx;
        $quantity = floor(mt_rand(20, 70));
        // update global quantity for served product
        $products[$idx]['global_quantity'] += $quantity;
        //set requests if $products is not an empty array
        if (!empty($products)) {
            for ($j = 0; $j < sizeof($products); $j++) {
                //for each product randomize if product is requested
                //a node may not request the product it produces
                if (floor(frand(3, 5, 7, 3)) % 2 == 0 && $idx != $j) {
                    //set product ID
                    $request = 'P' . $j . '|';
                    //set quantity
                    $request .= ceil(frand(10)) . '|';
                    //set priority
                    $request .= floor(frand(25)) % 3 . '^';
                    $requests .= $request;
                }
            }
            //remove tailing '^'
            $requests = trim($requests, '^');
        }
        // REQUEST FAILSAFE
        if ($requests === '') {
            //set product ID
            $requests = 'P' . generateProductFailSafe($i, $idx) . '|';
            //set quantity
            $requests .= ceil(frand(10)) . '|';
            //set priority
            $requests .= floor(frand(25)) % 3;
        }
        // set money
        $money = mt_rand(100, 200);
        //commit to DB
        execute_sql('<create_initial_arbor.php>', $con, "INSERT INTO nodes (ID, links, requests, serves, quantity, money) VALUES ('" . $ID . "', '" . $links . "', '" . $requests . "', '" . $serves . "', '" . $quantity . "', '" . $money . "')");
    }
    /// INSERET PRODUCTS CALCULATIONS HERE
    foreach ($products as $p) {
        $base_cost = calc_base_cost($p['max_cost'], $p['global_quantity'], 1);
        execute_sql('<create_initial_products.php>', $con, "UPDATE products SET base_cost='" . $base_cost . "', global_quantity='" . $p['global_quantity'] . "' WHERE name='" . $p['name'] . "'");
    }
    // unset large array to free up memory
    unset($rows);
    unset($headings);
}