# File: example/data.php

 Recommend this page to a friend! Stumble It! Bookmark in del.icio.us
 Classes of Benjamin Vatter > PHP Clarke and Wright Algorithm > example/data.php > Download
File: example/data.php Auxiliary script text/plain data load PHP Clarke and Wright Algorithm Solve a truck routing problem with Clarke & Wright By Benjamin Vatter adapted for the weight restriction on the distance.csv file imposed by the site (no more than 500Kb). 1 year ago 4,337 bytes

## Contents

 ``` array(             'total_weight_of_order'=>'weight',             'total_volume_of_order'=>'volume',             'client_type'=> 'N/M/S/C')         );         //bidimensional array of distances for node a->b and b->a for all nodes. 0 has to be the base     \$distance_matrix = array(         'node_a' => array(             'node_a' => 0,             'every_other_node' => 'value'),         'node_b' => array(             'node_b'=>0,             'every_other_node'=>'value')             );     // array of the capacities of each truck     \$truck_capacity = array(         0=>array('weight'=>'weight_cap', 'volume'=>'volume_cap'),         1=>array('weight'=>'weight_cap', 'volume'=>'volume_cap')         ); */ /*--data load--*/ //client data \$clients_csv = fopen('clientes.csv','r'); \$headers = fgetcsv(\$clients_csv); \$duplicate_nodes = Array(); \$client_demands = Array(); while(!feof(\$clients_csv)){     \$values = fgetcsv(\$clients_csv);     \$client_node = preg_replace('/[^0-9]/','',\$values[0]);     if(isset(\$client_demands[\$client_node])){ \$duplicate_nodes[] = \$values; continue; }     \$type = \$values[1];     \$weight = preg_replace('/[^0-9.]/','',\$values[2]);     \$volume = preg_replace('/[^0-9.]/','',\$values[3]);     \$client_demands[\$client_node] = array(         'total_weight_of_order'=>\$weight,         'total_volume_of_order'=>\$volume,         'client_type'=>\$type); } fclose(\$clients_csv); //distance matrix -- split in two beacuse the site dosen't allow upload greater then 500kb \$distance_matrix = Array(); \$distance_csv = fopen('distance1.csv','r'); \$headers = explode(';',fgets(\$distance_csv)); foreach(\$headers as \$key=>\$val){     if(\$key==0)continue; //skip 0,0 point of the table     \$distance_matrix['val'] = array(); } while(!feof(\$distance_csv)){     \$values = explode(';',fgets(\$distance_csv));     \$i = \$values[0];     for(\$j=1 ; \$j\$weight,'volume'=>\$volume); } fclose(\$truck_csv);   //fix duplicate clientes \$keys = array_keys(\$client_demands); \$last_client_id = end(\$keys); reset(\$client_demands); \$n=1; \$conversion = Array(); foreach(\$duplicate_nodes as \$values){     \$client_node = preg_replace('/[^0-9]/','',\$values[0]);     \$conversion[] = array(\$client_node,\$last_client_id+\$n);     \$type = \$values[1];     \$weight = preg_replace('/[^0-9.]/','',\$values[2]);     \$volume = preg_replace('/[^0-9.]/','',\$values[3]);     \$client_demands[\$last_client_id+\$n] = array(         'total_weight_of_order'=>\$weight,         'total_volume_of_order'=>\$volume,         'client_type'=>\$type);     foreach(\$distance_matrix as \$row_key=>\$col){         if(!isset(\$distance_matrix[\$row_key][(int)\$client_node]))continue;         \$distance_matrix[\$row_key][\$last_client_id+\$n] = \$distance_matrix[\$row_key][\$client_node];     }     \$distance_matrix[\$last_client_id+\$n] = \$distance_matrix[\$client_node];     \$n++; } \$client_nodes = array_keys(\$client_demands); //uncomment to see result /* print_r(\$client_nodes); echo"

"; \$to = array_keys(\$distance_matrix[\$row_key]); ?>
/
\$row):?>                                             ```