TicTacToe

I thought that now that I am back in a couple of programming courses, I would be posting here more often. However, most of the course work I end up doing is pretty tame, and not really worth writing about. But I finally ran into a fun little puzzle with the tic-tac-toe challenge. Not with the game of course, but with the AI. It is easy enough make an AI that cannot be beat, but I wanted to make one that would intelligently try to win as well. I went with a move-weight model, scanning every possible move adding up weights, and then taking the highest weighted move.

The following is the code for the AI;

Continue reading

a better date range picker

I was unhappy with the range-datepickers I had seen so I made my own. I needed a date range picker that could select ranges of years, quarters or months, and I couldn’t find one that worked in this way. I had been using the jquery datepicker, but it didn’t have a “quarter” option; although it may have been possible to extend the datepicker to allow for this. at any rate, this is still a little rough and a little long in the code, but it works the way I wanted it to; the available months/quarters and years change in logical ways depending on the current selection in each of the other select menus. It also preserves the selected date through aggregate mode changes; a setting of March is still there when switched to “quarterly mode” and back to “monthly mode”.

date-range-picker
the date-range-picker in Google Chrome

Here’s the date-range-picker in chrome; appearance will vary depending on the browser and the implemented formating. This may be a reason to switch over to hidden select elements mirrored by styled divs in order to have cross-browser consistency and for increased visual adaptability.

Continue reading

netbeans 7.0.1 is broken

I just spent way too much time figuring out that Netbeans 7.0.1 (from the repos) is broken. If anybody else is struggling with it, stop and upgrade to the 7.1.2 version at netbeans.org. In particular, it was silently failing to integrate with Mercurial. Additionally, many of the configuration dialogs were malfunctioning. Hopefully this helps somebody.

python approximate intetegrator 2.0

A guy in calc class was working on a more complicated solution to the approximate integration problem, and so I felt inspired to make a few improvements:

#!/usr/bin/python

import sys
from math import *

def main(argv):
        try :
                if argv[0] == '-h':
                        print 'usage: apxint.py <function_of_x> <lower_bound> <upper_bound> <subinterval_width>';
                        sys.exit();
                else :
                        function_of_x = argv[0];
                        a = float(eval(argv[1]));
                        b = float(eval(argv[2]));
                        n = float(eval(argv[3]));
                        dx = (b-a)/n;

                        # trapezoid Rule
                        def f(x) :
                                return eval(function_of_x);
                        x = a;
                        t = 0.0;
                        while (x < b) :
                                t += f(x);
                                x += dx;
                                t += f(x);

                        ft = (dx/2)*t;
                        print ("Trapezoid Rule = " + str(round(ft, 6)));

                        # midpoint Rule
                        x = a + (dx/2);
                        m = 0.0;
                        while (x <= b) :
                                m += f(x);
                                x += dx;
                        print ("Midpoint Rule = " + str(round(dx*m, 6)));

                        # Simpsons Rule
                        x = a;

                        s = 0.0;
                        while (x < b) :
                                s += f(x);
                                x += dx;
                                s += 4.0*f(x);
                                x += dx;
                                s += f(x);

                        fs = (dx/3)*s;
                        print ("Simpson's Rule = " + str(round(fs,6)));
                        sys.exit(1);

        except IndexError:
                print 'usage: apxint.py <function_of_x> <lower_bound> <upper_bound> <subinterval_width>'
                sys.exit(2);

if __name__ == "__main__":
        main(sys.argv[1:])

calculator fatigue

tired of tediously punching away at my calculator doing approximate integration I was inspired to whip up a little python script for computing Trapezoid rule, Midpoint rule and Simpson’s rule:

import math
from math import sqrt, log
a,b,n = 0.0,1.0,4.0;
dx = (b-a)/n;

# trapezoid rule
def f(x) :
        return 34*(math.cos(x**2));
x = a;
t = f(x);
x += dx;
while ((x + dx) <= b) :
        tt = f(x);
        t += 2*tt;
        x += dx;
tt = f(x);
t += tt;
ft = (dx/2)*t;
print ("T = " + str(round(ft, 6)));

# midpoint rule
x = a + (dx/2);
m = 0.0;
while (x <= b) :
        mm = f(x);
        m += mm;
        x += dx;

print ("M = " + str(round(dx*m, 6)));

# Simpsons rule
x = a;
s = f(x);
x += dx;
while ((x + 3.0*dx) <= b) :
        ss = f(x);
        s += 4.0*ss;
        x += dx;
        ss = f(x);
        s += 2.0*ss;
        x += dx;

ss = f(x);
s += 4.0*ss;
x += dx;
ss = f(x);
s += ss;
fs = (dx/3)*s;
print ("S = " + str(round(fs,6)));

Sage Fever

Well, I should be studying Calculus, but instead I wrote another sage application for visualizing another project problem. This one is the classic cow tied to a silo problem; if the rope just reaches the far side of the silo, how much area can the cow cover?

My python is still pretty rusty, but I think I did a little better on this one than the last:

###################################################################
#
# an application for plotting "the cow tied to a silo" problem.
# author: bnr
# date: 2012-10-16
#
###################################################################
from sage.plot.line import Line
from sage.plot.arc import Arc
r,t, a1, labl1,label2 = var('r,t, a1,labl1,labl2')
# the radius of the silo
r = 1
@interact
def _( r = slider( [1..25],1, label = "Silo Radius") ): 

 #the area labels
 labl1 = text(('$ \\frac{A_1}{2} $'), (2.5*r,1.5*r),fontsize=20,color='black') 
 labl2 = text(('$A_2$'),(.5*r,1.5*r),fontsize=20,color='brown')

 # x and y on the silo perimeter 
 def x1(t): return r*cos((t))
 def y1(t): return r*sin((t))
#the area of the half-circle; A_1
 f = sqrt( (pi*r)^2 - (x -r)^2 ) 
 L1 = plot(f, r, r*(pi+1), fill=True) + labl1
# the curve of the silo wall
 L2 = parametric_plot( (x1(t), y1(t) ), (t, 0, float(pi)), color='red')

 # wrapping the rope around the silo
 def silo_rad(a):

 # finding the curve as the rope is stretched taught
 def new_x(a) : 
 new_x = x1(a) - abs( r*(pi-a)*sin(a) ) 
 return new_x

 def new_y(a) : 
 new_y = y1(a) + ( r*(pi-a)*cos(a) )
 return new_y

 # the tangent line of the taught rope
 s_tan = line( ( ( new_x(a), new_y(a) ) , ( ( x1(a),y1(a) ) ) ) )

 # the arc walked by the cow with the rope taught 
 cow = parametric_plot( (new_x(t), new_y(t) ), (t, 0, float(pi)), color='brown')
 return s_tan + cow + labl2

 # calculating the area in the half-circle
 def A1(a) : 
 return round( (pi^3)*(a^2)/2, 4)
# calculating twice the area of the integral part; A_2
 def A2(a) : 
 return round( (pi^3)*(a^2)/3, 4)
# point of tangent to silo
 def f(a):
 p1 = point( ( x1(a), y1(a) ) )
 return L1 + L2 + p1 + silo_rad(a)
# printing the calculated areas 
 print( html('$A_1= \\frac{\pi^3 r^2}{2} {\\approx} $' + str(A1(r) ) + '$\hspace{12 mm}2 \\times A_2 = \\frac{\pi^3 r^2}{3} {\\approx} $' + str(A2(r)) + '$\hspace{12 mm}Total\ Area = \\frac{5\pi^3 r^2}{6} {\\approx} $' + str(A2(r) + A1(r)) ) )

 # animating the tangent line/integral part; A_2
 a1 = animate([f(i) for i in srange(0,float(pi),.1)] , xmin = float(-2*r), xmax = float((1.1+pi)*r), ymin = float(-.2), ymax = float(1.1*pi*r))
a1.gif(savefile='my_animation.gif', show_path=False )