foreach ($cariche as $i => $carica_i) {
     $politico_i = $carica_i->getOppPolitico();
     $gruppo_id = $politico_i->getGruppoCorrente()->getId();
     // stampa riga con id politico, nome e gruppo
     $pol_descr = sprintf("p%d = \"id:%d,nome:%s,cognome:%s,gruppo_id:%d;\"\n", $i + 1, $politico_i->getId(), $politico_i->getNome(), $politico_i->getCognome(), $gruppo_id);
     print $pol_descr;
     fwrite($fp, $pol_descr);
     // stampa riga con valori matrice distanze
     // dist = MAX - sim
     fprintf($fp, "r%d = [ ", $i + 1);
     foreach ($cariche as $j => $carica_j) {
         $d = OppSimilaritaPeer::retrieveByPK($carica_i->getId(), $carica_j->getId());
         if ($tipo == 'votes') {
             $dist = OppSimilaritaPeer::getMaxSimilarityForVotes() - $d->getVotingSimilarity();
         } else {
             $dist = OppSimilaritaPeer::getMaxSimilarityForSignatures() - $d->getSigningSimilarity();
         }
         fprintf($fp, "%5.3f", $dist);
         if ($j < $ncariche - 1) {
             fprintf($fp, ", ");
         }
     }
     fprintf($fp, " ];\n");
 }
 // ultime due righe per aggregare i dati precedenti
 fprintf($fp, "ps = [");
 for ($i = 0; $i < $ncariche; $i++) {
     fprintf($fp, "p%d; ", $i + 1);
 }
 fprintf($fp, "];\n");
 fprintf($fp, "D = [");
 if (is_null($dd)) {
     $dd = new OppSimilarita();
     $dd->setCaricaFromId($politici[$i]['id']);
     $dd->setCaricaToId($politici[$i]['id']);
 }
 for ($j = $i + 1; $j < $ncariche; $j++) {
     $d = OppSimilaritaPeer::retrieveByPK($politici[$i]['id'], $politici[$j]['id']);
     if (is_null($d)) {
         $d = new OppSimilarita();
         $d->setCaricaFromId($politici[$i]['id']);
         $d->setCaricaToId($politici[$j]['id']);
     }
     $d->setSigningSimilarity(OppSimilaritaPeer::similarityForSignatures($politici[$i], $politici[$j]) / $ncariche);
     $d->save();
     // scrittura elemento simmetrico
     $ds = OppSimilaritaPeer::retrieveByPK($politici[$j]['id'], $politici[$i]['id']);
     if (is_null($ds)) {
         $ds = new OppSimilarita();
         $ds->setCaricaFromId($d->getCaricaToId());
         $ds->setCaricaToId($d->getCaricaFromId());
     }
     $ds->setSigningSimilarity($d->getSigningSimilarity());
     $ds->save();
     $ds = null;
     unset($ds);
     $d = null;
     unset($d);
 }
 print ".";
 if ($i > 0 && $i % 10 == 0) {
     print "{$i}/{$ncariche} ";