[redistricter] r212 committed - add text csv blockid,district-number loading...

0 views
Skip to first unread message

redist...@googlecode.com

unread,
Apr 3, 2011, 2:19:03 PM4/3/11
to redistrict...@googlegroups.com
Revision: 212
Author: brian.olson
Date: Sun Apr 3 11:18:14 2011
Log: add text csv blockid,district-number loading
works with data published for Iowa redistricting!

http://code.google.com/p/redistricter/source/detail?r=212

Modified:
/trunk/GeoData.h
/trunk/Solver.cpp
/trunk/Solver.h
/trunk/analyze.cpp
/trunk/analyze_submissions.py
/trunk/fileio.cpp
/trunk/notes.txt

=======================================
--- /trunk/GeoData.h Fri Mar 4 09:21:28 2011
+++ /trunk/GeoData.h Sun Apr 3 11:18:14 2011
@@ -66,7 +66,7 @@
uint32_t index;
};
UST* ubids;
- /* binary search, fastish */
+ /* binary search, fastish, return INVALID_INDEX on failure */
uint32_t indexOfUbid( uint64_t u );
/* linear search, sloooow */
uint64_t ubidOfIndex( uint32_t index );
@@ -121,6 +121,8 @@
virtual ~GeoData();
};

+static const uint32_t INVALID_INDEX = ((uint32_t)-1);
+
#if 0
// deprecated
class ZCTA : public GeoData {
=======================================
--- /trunk/Solver.cpp Thu Feb 10 20:42:12 2011
+++ /trunk/Solver.cpp Sun Apr 3 11:18:14 2011
@@ -615,6 +615,86 @@
close( readfd );
return -1;
}
+int Solver::loadCsvSolution( const char* filename ) {
+ FILE* fin;
+ if (0 == strcmp(filename, "-")) {
+ fin = stdin;
+ } else {
+ fin = fopen(filename, "rb");
+ }
+ if (fin == NULL) {
+ perror( filename );
+ return -1;
+ }
+ static const int MAX_LINE_LENGTH = 1024;
+ char* lineBuf = new char[MAX_LINE_LENGTH];
+ char* line;
+ line = fgets(lineBuf, MAX_LINE_LENGTH, fin);
+ int err = 0;
+ int errcount = 0;
+ memset(winner, NODISTRICT, gd->numPoints);
+ int setcount = 0;
+ while (line != NULL) {
+ char* c = line;
+ while ((c != '\0') && (!isnumber(*c))) {
+ c++;
+ }
+ char* endp = NULL;
+ uint64_t tubid = strtoull(c, &endp, 10);
+ if ((endp == NULL) || (endp == c)) {
+ perror("reading ubid");
+ err = errno;
+ break;
+ }
+ c = endp;
+ while ((c != '\0') && (!isnumber(*c))) {
+ c++;
+ }
+ endp = NULL;
+ long district = strtol(c, &endp, 10);
+ if ((endp == NULL) || (endp == c)) {
+ perror("reading district number");
+ err = errno;
+ break;
+ }
+ uint32_t index = gd->indexOfUbid(tubid);
+ if (index == INVALID_INDEX) {
+ fprintf(stderr, "bogus ubid %lld\n" , tubid);
+ errcount++;
+ if (errcount > 20) {
+ err = -1;
+ break;
+ }
+ } else if ((district < 1) || (district > districts)) {
+ fprintf(stderr, "winner[%d]=%lu would be out of range (1..%d)\n",
index, district, districts);
+ errcount++;
+ if (errcount > 20) {
+ err = -1;
+ break;
+ }
+ } else {
+ winner[index] = district - 1;
+ setcount++;
+ }
+ line = fgets(lineBuf, MAX_LINE_LENGTH, fin);
+ }
+ if (err == 0) {
+ err = ferror(fin);
+ }
+ delete [] lineBuf;
+ if (err != 0) {
+ fprintf(stderr, "error reading file \"%s\" (%d): %s\n", filename, err,
strerror(err));
+ }
+ int notset = 0;
+ for (int i = 0; i < gd->numPoints; ++i) {
+ if (winner[i] == NODISTRICT) {
+ notset++;
+ }
+ }
+ fprintf(stderr, "set %d points of %d, %d not set\n", setcount,
gd->numPoints, notset);
+ dists->initFromLoadedSolution();
+ return err;
+}


void Solver::initSolution() {
=======================================
--- /trunk/Solver.h Thu Feb 10 20:42:12 2011
+++ /trunk/Solver.h Sun Apr 3 11:18:14 2011
@@ -108,6 +108,7 @@
void allocSolution();
int saveZSolution( const char* filename );
int loadZSolution( const char* filename );
+ int loadCsvSolution( const char* filename );
/* from loadname */
int loadSolution( const char* loadname );
void initSolution();
=======================================
--- /trunk/analyze.cpp Fri Mar 4 09:21:28 2011
+++ /trunk/analyze.cpp Sun Apr 3 11:18:14 2011
@@ -73,6 +73,7 @@
bool distrow = true;
bool distcol = false;
bool quiet = false;
+ bool loadSolutionCsvMode = false;
const char* exportPath = NULL;

vector<const char*> compareArgs;
@@ -149,6 +150,10 @@
} else if (!strcmp(argv[i], "--export")) {
++i;
exportPath = argv[i];
+ } else if (!strcmp(argv[i], "--csv-solution")) {
+ ++i;
+ sov.loadname = argv[i];
+ loadSolutionCsvMode = true;
} else {
argv[nargc] = argv[i];
nargc++;
@@ -170,8 +175,14 @@
if (!quiet) {
fprintf(stdout, "loading \"%s\"\n", sov.loadname);
}
- if (sov.loadZSolution(sov.loadname) < 0) {
- return 1;
+ if (loadSolutionCsvMode) {
+ if (sov.loadCsvSolution(sov.loadname) < 0) {
+ return 1;
+ }
+ } else {
+ if (sov.loadZSolution(sov.loadname) < 0) {
+ return 1;
+ }
}
if (!quiet) {
char* statstr = new char[10000];
=======================================
--- /trunk/analyze_submissions.py Thu Mar 10 10:30:51 2011
+++ /trunk/analyze_submissions.py Sun Apr 3 11:18:14 2011
@@ -279,6 +279,7 @@
kmppSpread = self.measureSolution(tfparts['solution'], config)
if kmppSpread is None:
logging.warn('failed to analyze solution in "%s"', fpath)
+ # TODO: set attempt count in 'vars' and retry up to N times
return False
else:
kmppSpread = (None, None)
=======================================
--- /trunk/fileio.cpp Fri Mar 4 09:21:28 2011
+++ /trunk/fileio.cpp Sun Apr 3 11:18:14 2011
@@ -722,7 +722,7 @@
return ubids[mid].index;
}
}
- return (uint32_t)-1;
+ return INVALID_INDEX; // -1
}
uint64_t GeoData::ubidOfIndex( uint32_t index ) {
for ( int i = 0; i < numPoints; i++ ) {
=======================================
--- /trunk/notes.txt Sun Feb 27 12:50:04 2011
+++ /trunk/notes.txt Sun Apr 3 11:18:14 2011
@@ -177,3 +177,10 @@
I have a first results on 2011 data! It's just 1000 generations of the
nearest neighbor solver, so I'm sure I'll do better soon. So far I've run
it for NJ and VA. Had to toss the super-fine face+edges based linking and
fall back to common-line-segment linking based on the tabblock tiger maps.
The faces seem to have lots of 'blocks' that don't exist in the
redistricting data. Probably extra detail on water and unpopulated areas.

http://www.redistricting.wa.gov/
+
+
+TODO: try to keep precincts/cities/counties together by imposing a
distance penalty on blocks that are part of a region that isn't entirely
within the district.
+
+
+http://www.legis.iowa.gov/Resources/Redist/redistricting.aspx
+http://www.house.mo.gov/largemap.aspx

Reply all
Reply to author
Forward
0 new messages