Beispiel #1
0
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;
}
function generateProducts($file, $con)
{
    global $products;
    $products = checkProductsGlobalVariable($con);
    $multiplier = 0.15;
    $n_node = retrieveNodesCount($file, $con);
    for ($i = 0; $i < ceil($n_node * $multiplier); $i++) {
        execute_sql('<create_initial_products.php>', $con, "INSERT INTO products (name, base_cost, max_cost, global_quantity) VALUES ('P" . $i . "', '" . 0 . "', '" . frand() . "', '" . 0 . "')");
    }
    $products = fetch_nodes_toArray($con);
}
Beispiel #3
0
function get_initial_cost_ppc($product, $con)
{
    global $products;
    $products = checkProductsGlobalVariable($con);
    foreach ($products as $temp) {
        if ($temp["name"] == $product[0]) {
            //	returns the value (float) of ONE instance of the product the buyer needs
            return $temp["base_cost"];
        }
    }
}
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);
}