Monday, April 28, 2014

NetBeans and the UTF-8 Nag

I had a mildly annoying experience with the NetBeans IDE recently, and I'm making a note to myself here what to do if it happens again.

I was debugging a Java program that I wrote using an open-source library, and it became necessary for me to set a breakpoint in the library code. This was a first time experience for me: I use open-source libraries extensively, but I virtually never open the source code, and in fact I usually don't bother to include the path to the source code when I define the library in NetBeans. I've found that nothing spoils the taste of the sausage quite like seeing how it was made. :-)

At any rate, when I opened the source file to insert the breakpoint, NetBeans warned me that the source file "cannot be safely opened with encoding UTF-8" and asked if I wanted to open it anyway. I said yes, it opened, I set the breakpoint, found the bug (in my code), closed the source file and that was that. Or so I thought. There's a discussion of this warning on the NetBeans support forum, including various possible remedies (including converting the source library to UTF-8). That wasn't necessary or worth the effort in my case, since I was opening one file, one time only.

The first occurrence of the UTF-8 warning wasn't bothersome. What was annoying was that every time thereafter that I opened the project in NetBeans, I got the warning. Whether I answered the "really do it?" question yes or no did not matter; none of the source files opened, and the IDE thereafter gave me no grief ... until the next time I went into that project.

It turns out that I had left the breakpoint in the source file set. Even though the source file was closed, and I was not attempting to reopen it, the presence of the breakpoint was apparently causing NetBeans to look at the file, see the non-UTF-8 encoding, and nag me. Once I figured that out, the solution was simple: remove the breakpoint. Note to self: be extra careful to remove breakpoints from foreign source code in the future.


Saturday, April 5, 2014

Modeling an On/Off Span

I may be ruining a perfectly good homework problem by posting this. :-)

Occasionally someone needs to incorporate in an integer programming model the notion of something changing state for a predefined span of time. The typical characterization I've seen is as follows:
  • we have a sequence of binary variables $x_i\in\{0, 1\}, i\in\{1,\dots,N\}$ that indicate the state of something; and
  • if the state changes from 0 to 1, it must remain 1 for exactly $K$ consecutive periods (with a possible exception of we reach the horizon limit $N$ before we see the $K$-th unit value).
For $K=3$, this means the sequence <... 0 1 1 1 0 ...> is valid but <... 0 1 1 0 ...> is invalid (not enough ones), <... 0 1 1 1 1 0 ...> is invalid (too many ones), and <... 0 1 1> (bumping into the end of the horizon) may or may not be considered valid. The question is, how do we model this with binary variables.

The answer is to rethink our choice of variables: rather than focusing on binary variables representing the system state, we focus on binary decision variables indicating whether or not a particular epoch represents the start of a string of ones. So we introduce a new set of binary variables $y_i\in\{0, 1\}, i\in\{1,\dots,N\}$, where $y_i=1$ if and only if a string of $x$ variables starting at epoch $i$ take the value 1. We enforce all this with the following constraints:
  1. $x_{i-1}\le 1-y_i, i\in\{2,\dots,N\}$ (the value of $x$ in the epoch immediately prior to the start of a run must be 0);
  2. $x_{i+K}\le 1-y_i, i\in\{1,\dots,N-K\}$ (the value of $x$ in the epoch immediately after the end of a run must be 0); and
  3. either $$x_i\ge y_j \forall i\in\{1,\dots,N\}, \forall j\in \{i-K+1,\dots,i\}$$ or $$x_i\ge \sum_{j=i-K+1}^i y_j,$$eliminating any terms with indices outside the domain $\{1,\dots,N\}$ (the $K$ values at or immediately after the start of a run must be 1).
The second version of (3) is more compact (good) but makes the constraint  matrix denser (not so good). I'm pretty sure the second version produces the tighter feasible region.

If you do not want the time frame to end on a run of fewer than $K$ ones, constrain $y_j=0$ for $j\in \{N-K+2,\dots,N\}$. If you must have at least one zero after the final run of ones (i.e., <... 0 1 1 1 0> is okay for $K=3$ but <... 0 1 1 1> is not), constrain $y_j=0$ for $j\in \{N-K+1,\dots,N\}$. If you do not want to start a run of ones at the very beginning of the time frame (<0 1 1 1 0 ...> is okay but <1 1 1 0 ...> is not), constrain $y_1=0$.


Thursday, April 3, 2014

The Definition of "Open"

Before I climb up on my soapbox, I probably should make a couple of disclaimers to put what follows in perspective.
  • I am a heavy user of open source software, and in particular of products from the Mozilla Project. My primary email client is Thunderbird. My first web browser was Netscape Navigator (which predates the Mozilla Project), which eventually morphed into Mozilla and, most recently, Firefox. I've used other browsers, depending on the platform, but Firefox remains my primary choice (although I may have to rethink that based on what follows). In fact, I'm typing this entry into Firefox.
  • I am in favor of the legalization of gay marriage. For that matter, I'm in favor of legalizing polygyny and polyandry. I'm not saying that I want to try any of them; I just don't see any reason that the government (at any level) has a compelling interest in forbidding any of them, and doing so clearly creates inequality in the treatment of citizens. (Actually, while I don't want to get into the subject in detail, my personal preference would be to take the government out of the marriage business entirely. Religions should be free to define "marriage" any way they want; the government should let any adults of legal age file paperwork somewhere designating individuals to fill the "marital" roles for which the government has a legitimate interest, such as parenting, advanced health care decisions, mingling and division of assets, etc.)
Wondering what brought all that up? I was sitting in a coffee shop this afternoon, catching up on some reading, when I came across a story that nearly caused me to do a spit take: Mozilla CEO Brendan Eich resigns amid controversy. Here's a short summary of the events it describes:
  1. The Mozilla Foundation, in need of a new CEO, hires Brendan Eich (their CTO).
  2. The fact that Eich donated $1,000 (presumably of his own money) in 2008 to support California's Proposition 8 (a bill to ban gay marriage) resurfaces.
  3. Mozilla's corner of the universe reacts (sample here), or possibly overreacts, to the hiring. As best I can tell from reading a few articles, the reaction is largely if not entirely negative.
  4. Three members of the Mozilla Foundation board resign, but allegedly due to a disagreement on hiring an internal candidate and not due to the donation. (This is reported to be half the board, raising the question of how Eich got hired if half the board was so opposed to him that they would resign in protest.)
  5. Eich resigns (possibly under pressure) and Mozilla issues a statement about it (which reads like a mea culpa for having hired him in the first place).
I have never met Mr. Eich, I have no idea whether he was a good pick for the CEO position, and I'm massively confused about how he would be selected with so much apparent board opposition. As noted above, I don't agree with Eich's position on Prop. 8. What caused me to choke on my coffee, though, is nicely illustrated by this line from the Mozilla blog post:
Mozilla believes both in equality and freedom of speech. Equality is necessary for meaningful speech. And you need free speech to fight for equality. Figuring out how to stand for both at the same time can be hard.
So I think they've figured the "hard" part out. You start out by appointing yourself the arbiter of "equality", and then you define "free speech" to mean "you're free to make speeches agreeing with us". Somewhere on the AfterLifeNet, Voltaire and Evelyn B. Hall are probably installing new browsers. I may need to join them (with the browsers, not the after life, just to be clear).