[SOLUTION] Kalah (#58)

I know this is a bit late, but I've been trying to train my neural net.
I've never used them before and I've been wanting to learn. I created a
neural net with the input being the kalah board plus one bias node(so
15 input nodes), 20 hidden nodes, and 6 output nodes. A move is
selected by running the neural net and then picking the valid move
whose corresponding output node is highest. I'm training the net right
now using Particle Swarm Optimization to find good weights.

I trained it against dave's player and adam's remove right. It found a
pretty good solution against the remove right player, but not dave's.
The best I could get after searching for a while was losing by 12. I
might have better luck if I just take dave's solution and replace the
function it uses to rate the board's goodness with a neural net and
then train it. That might be fun for later.

Anyways sorry about the lateness, but here it is.

-----Horndude77

NeuralPlayer.rb (Sorry about the long section of weights. I had them on
one line each.):

···

=================================================
require 'NeuralNet'

class NeuralPlayer < Player
    def initialize(name)
        super(name)
        @net = NeuralNet.new(15, 20, 6)
        @num_weights = 15*20 + 20*6

        #weights for RemoveRight
        #self.weights = [-0.428456550403913, -0.909217917148082,
-0.468284431041651, 0.357903882475321, -0.104252998861521,
-0.460447451110633, 0.264105895124314, -0.220335064829405,
0.344066370486408, 0.110887382259345, -0.112061654833158,
0.387531331474845, 0.253470699210902, 0.58235888221721,
0.306268984507146, 0.692431343604429, -0.0536485504199445,
1.25989522777867, -1.10462932704233, -0.351286944668706,
-0.567170711584581, 1.29779740339143, 0.907906329763339,
-0.207925256569637, 0.177102138750371, 0.128961825966903,
-1.40103570953856, 0.60460561949975, -0.293757627893575,
0.519854674076599, -0.121247185426499, -0.48243591070721,
-0.5635586279583, 0.87760707848256, -0.24059588906072,
-0.0397911286451754, -2.61454740162978, 0.100948481882159,
0.649844494979625, -0.20679545186547, 0.956480816100499,
0.379654934947368, 0.213954601603671, 0.0528884967154502,
0.625382960895802, 0.362097750351292, -0.253497508633716,
-0.883702364891605, -0.101659606324204, 0.248216287934223,
-0.113052794219895, 0.634086308736, -0.302798853606374,
-0.346890845789382, -0.779510000549479, -0.0360852571092638,
0.0153516471477581, -0.171835179564683, 0.0251128237293062,
0.645490210802246, -0.193099922624216, 0.712786971652146,
-0.152956125676004, -0.616851260423588, 0.2459019745739,
-0.955383328688331, -0.364705013321598, -0.617848226340645,
0.475091440306075, 0.354449966944016, -1.01613173478496,
0.676427027866194, 0.284847198728979, -0.841248187176398,
0.429200477632715, -0.326666975727536, -0.900577897928031,
-0.151187352160431, 0.418136290801099, 0.0941511164912963,
0.506840998984342, 0.103254060859946, 0.333803252464272,
-2.06121470406907, -0.225537973154723, -0.233342939397305,
-0.102999506187284, -0.53850202703304, -0.555527143472232,
-0.671017075023332, -0.282380091302946, 0.131082542665543,
-0.723479705286975, 0.795054642785429, 0.491077298824963,
0.423059363779898, -2.29476738228994, -0.121696962009613,
-0.328075962465621, -0.367687083335075, 0.37277431250442,
-0.891982913707634, 0.690166633881076, 2.20541381798063,
0.33841734898414, 1.34426349664742, 0.217566888046106,
-0.671506739310853, 0.0457879215134461, -0.785153977981076,
0.4399351958435, -1.07227081177901, 0.80071121984544,
-0.498359707974827, 1.50749203618481, 0.00155955509612907,
0.558230940831766, -0.762174449476105, -0.0842126063238458,
-0.861999505780237, -0.346765623726261, 0.189041394170814,
0.515209464762614, 0.953695829738959, 0.109226147741295,
-0.410604039478746, 1.26399802844183, -0.42263330334738,
-1.22115749426171, -0.331234653704824, 0.102886993963447,
-0.100692469885383, 1.20483707939992, -0.347967500808309,
0.553050973560177, -0.0947739419530351, 0.491011977559244,
0.628874101325749, -1.95690538525791, 0.297814702564266,
-0.471007824648471, -0.414483183737145, 0.0726120056956718,
-0.933570106288705, 0.194825010200492, -0.332411723772583,
-0.454179722185871, -1.52057182606227, -0.743829261927187,
0.217180769977938, 1.23707542789918, 0.249214021285456,
0.980914083149354, 0.840109804787634, 0.0549821073482349,
0.759213641648435, 0.345571660732875, 0.642177051909433,
-0.699048329339318, -0.711124967241672, -0.198273084388503,
-0.682136345206517, 0.311305839135266, 0.631334457513039,
0.600316128422118, 0.119320030158148, -0.277536611595672,
0.152726604986164, -1.12883395672669, 0.685939108743903,
0.612340495493681, -0.470976215605829, 0.192660126498784,
-0.38101262950115, 0.692042726229392, 0.375075320693942,
-0.537093511777074, -0.75716836463867, 0.133598059764474,
-0.0990331086141189, -0.342974262988399, 0.148259446813639,
-0.307505302024212, -0.691169077958319, 0.160615435359153,
-0.473318586554398, -0.147746841313201, 0.384987460864245,
-1.71568495055048, 0.605786228599172, -0.331527288831919,
-0.742009170961669, -0.286044861721401, -0.197316839222173,
-0.652244349958852, -0.6931049458475, 0.606268100395642,
-0.816028396199571, -0.276575039831182, -0.965134498229981,
-0.758589133085266, -0.215462052762639, -1.22485716505581,
0.635854825774725, 0.0347106278236468, 0.68017020773746,
0.486510650159744, -0.950942640912604, -0.408793432853341,
0.0660212214118211, 0.439275058115052, -1.90764266527444,
0.0705340780324987, -0.579390528498766, -0.594227207465761,
-0.607485381621411, -0.745915297691384, 0.648556199264886,
0.424482268251714, 0.448565852678736, -0.212875620275073,
-1.60767121899908, 0.590280093295565, 0.220889024665176,
0.519830949320126, -1.58646652366069, 0.0490820218238098,
-0.939734916717465, 0.271622882831742, 0.587123430789783,
0.356941209321922, 0.431588738070851, -0.00363682276519776,
0.632624296908395, -0.460607536832767, -0.162344504807553,
0.244686235644648, 0.365983697367206, -0.556207060994067,
-0.247592686197143, -0.256153222456516, 1.42851969844773,
0.150973382582707, 0.217898142551229, 0.0327769935098895,
0.0301879317982763, 0.0504474882971441, 0.927135059762964,
-1.32001110030053, -0.0908289933296031, -0.649306861640546,
-0.409394034911994, 0.548916567397019, -0.816455346515919,
-0.23857172691294, 0.124546229435521, -0.557501169427238,
0.0367883579939846, 0.217641257520411, -0.370946458590141,
0.665435343143148, 0.795439121655607, -0.328009918280298,
-0.889705313261023, 0.528441781909589, -0.521804444678478,
-0.257741432376917, 0.28334033531291, -0.647816542299153,
-0.11188517833591, -0.382643376445593, -0.50836534761093,
-0.881302835589924, 0.451928929683854, 0.322905810792575,
-0.295616304054877, -0.0490996038261706, -0.419065683767976,
0.0889212390738239, 0.354441291440674, 0.533502165411728,
0.39775482176289, 0.522613557593291, 0.56763651037131,
-0.553985587928485, -0.0892770054346463, 0.417242403170033,
-0.0622231365237445, -0.536468658574459, -0.625611293373744,
0.397897575429255, -0.164522809437456, -0.76074160128093,
-0.338860069316338, 0.108424735798555, 0.72183978231058,
0.270777413929673, -0.0460416847164016, -0.107106555215344,
-1.16391449205179, 0.0287701615910848, 0.338030888059846,
0.916736090240755, -0.0946791015986226, -0.337775858656124,
-0.184221985273276, 0.7317875788664, -0.246207369460492,
0.627625049453152, -0.265781090911406, 0.250113034586153,
-0.203373686280343, -0.632653811240432, 0.96838789592563,
0.19063294685656, 1.17332675801791, -0.516224856380255,
-0.264875062177442, 0.50505882714235, -0.491935430100226,
0.4549665963441, -0.414740791398929, -0.571609921257171,
-0.265250663950144, -0.475686272333388, -1.07131641800336,
-0.168325437973922, -0.17234499168695, 1.2020474172808,
0.337248901596775, -0.634358591648102, -2.94504361685402,
-0.0474679493484615, -0.46289571515561, -0.149910278399775,
-0.56911680359925, 0.856479648671053, 0.0222252254438126,
-0.0789053965464129, -0.865175832880598, 0.813113040749056,
1.40052017545579, 0.247265188021256, -0.440075796418733,
0.917240725211066, 0.726954195590911, 0.573646173145207,
-0.336511408821544, -0.222735979722609, 0.879239872079174,
-0.733586265855175, -0.41900569195846, -0.293545971690515,
-0.632169580731648, -0.0856443553620738, -0.223316902854729,
0.403983130234464, -0.0986907442550533, 0.505726677775571,
0.544979171664306, -0.92863401861874, -0.583791809420349,
-0.682365608929785, -0.620485109687089, -0.0729610489595295,
1.67633914332691, 0.557908335099985, -0.654472279868246,
1.696774835487, 0.0502110469681493, 1.18420531528306,
-0.479767058178744, -1.14370493230831, 0.0504510683261292,
-0.237302537046759, -0.0639529013404704, 0.276548508170619,
0.742445105762371, -0.792246089654982, 0.269589581769951,
-0.593650654967001, -1.10475004597524, -0.705061651554581,
0.560147802074617, 0.112770824037605, 0.308635801604607,
-0.139788519302014, -0.171711481459321, 0.0548073868984597,
0.622975084294081, -0.887766178428485, -0.0826250645225019,
-0.294980224893042, 0.324832701146147, -0.632388433547754,
-0.0743648174407521, 0.256966886000103, -0.348550409126788,
0.86243829650827, 0.110484229604374, -0.105573500449927,
-0.584517432238993, 0.119208851966649, 0.993143683984971,
-0.358145459385369, -0.0579403357513844, -0.965421816301397,
0.512560827725041, -1.42246421454656, 0.863912738871492,
1.04780140334956, -0.0646477189132773, -0.543296500968879,
0.271989197407765, -0.959413629549363, 0.280181611937799,
-0.363234787452163, 0.0239440462433509, 0.906154588569019,
0.158870148658544]

        #weights for Dave
        self.weights = [0.595765520562772, 0.572313565308922,
-1.01593258550904, -1.63154027608213, -0.242177969065067,
-1.44498150429411, -0.611346758262896, -0.714767503318283,
0.133920007475619, -0.0280544755220122, 0.805544561666266,
-0.194520526790607, 1.1332800840179, -0.899871016430959,
0.247063828714958, 0.224254662718752, -0.618371544496958,
0.684186664735964, -0.190457855344232, 0.799862285892088,
-0.555844449171634, 0.0238775186945493, -0.180573650320856,
0.354543728300561, 0.513768996058762, 0.23348678845317,
0.178371745419366, -0.591680504803924, -0.201463863457764,
1.68690987229618, 0.444379703941704, 0.516277936677377,
-0.165293331915385, 0.183888467391197, -0.207605328542711,
0.0336538540792326, 0.0830359695274841, 0.791089393285359,
0.617361158680298, 0.860187793570229, -0.85071993055499,
0.397924224079726, -0.921314700002768, 0.15306944130641,
-0.738627827917805, -0.796644068900635, -0.0234897027237452,
-0.596106214153031, 0.0103028873257534, 0.694204932672169,
-0.0783157140820865, 1.47304347282447, 0.243039689593514,
-0.154484297644386, -0.857262383343563, 0.252696814893751,
-0.40343952897704, -0.294704552057072, -0.605114371140755,
-0.117410075097659, 0.16976424826419, 0.0797140147048645,
1.15575512685767, -0.716442817770782, 0.0288381173038192,
1.24303417704093, 1.08948769036477, -0.250157841294754,
-0.221026751512915, -0.0412528829199518, -1.28136626981268,
0.0271221530977974, -0.819704733220784, 0.826844506255673,
-0.732099526633263, 0.524618630380285, -0.648083676037858,
-0.00499945681324156, -0.594934960761219, -0.776499962950219,
-0.595310019616089, -0.40437646365592, 0.0213853255182831,
-0.123200509730716, 0.595725189798745, 0.387206450180365,
-0.103128672991375, -0.796595266582018, -0.57040356766035,
0.617376786302605, -0.0492958991933076, 1.87381016576847,
0.0500697306671592, -0.00374619138529117, -0.333956103620826,
0.682428766042459, 0.45085001975015, 0.157797184138026,
0.537906024336842, 0.240119993671652, -1.71128081561554,
0.409489725584302, -0.920812577416039, -0.256255495792021,
-0.704681250903957, -0.948557914062174, -0.290420522444693,
-0.0532276068256267, 0.828402958638734, 1.35836495166806,
0.280025344929557, 0.936707731344621, -0.192066513071091,
-0.075782675806965, 1.35427201545769, -0.113366307708726,
-0.0504057876198619, 0.73601129297563, -0.897915461464336,
0.271291192038269, 0.134944553786449, 0.938718165429378,
-0.991812182124245, 0.613719915325468, -0.361579916581629,
-0.825772807653645, 1.42672684008144, 0.17313277238907,
0.283468798275685, 0.0904374445462766, 0.668305801258156,
-0.0088422724896573, 0.139621424234071, -0.903858344121345,
0.656929574729915, -0.437276244381152, 1.0358154860163,
-0.00675062072284553, 0.36195163246692, -0.414024029206097,
-0.445187541089639, -0.360619428139445, 1.07422913793284,
0.857220973547937, 1.2867735591499, -0.267002726755962,
-1.00800992274211, -0.280795345456028, -0.337565151748177,
-0.089099648711894, 0.0360080087280767, -0.447012196582776,
-0.949137872310408, -2.46913736427525, 0.20902350638093,
-0.14902418251281, -0.300060511323241, -0.0355604438556937,
-0.0946652143482725, 0.0244973704529738, 0.121535182384221,
0.877938462750686, 0.13514569157873, 0.730894434624369,
-0.728258747062556, -0.585656722420927, -0.546402878819304,
-0.343737233363635, 0.409586311239579, -0.254866830426791,
-0.242883255706001, -0.186348714111791, -0.265694023398695,
0.731393941678047, -0.0746181059090219, 0.218347383891012,
-0.812929710983027, 0.146549203387507, 0.110651741058259,
-1.09842076325924, -1.04917158207456, -1.47015228627904,
0.575126789184426, 1.85809542921445, 0.936099082727676,
-0.778637023080794, 0.416111666430353, -0.194774649283276,
-0.941750335545621, -0.131274284178395, -0.0477668866045489,
0.602038230833452, 0.569125547608728, -0.434815281396334,
0.0596187688278964, -0.789702207783062, 0.206776039104787,
0.412273228995914, -0.726223826540894, 0.23140765095681,
0.918138236244244, 0.0140589140937358, -0.879874698211939,
0.884726701492646, -0.9246077647375, 0.397956364508967,
-0.39926602290578, 0.294201967223436, -0.924516544204645,
-0.126637130005552, -0.0567156618256692, -0.211649106737817,
-0.247314351914321, -0.189047057484531, 2.15593418636414,
-0.279329113054268, 0.191024744803032, 0.395286138597393,
-1.39057157713051, 0.369662236309845, 1.04386554488724,
-1.43230866140681, -0.366813710646212, -0.430484633313484,
-2.31467831654428, -0.0955713345233841, 0.665451124992163,
0.256568831197233, 1.99980579849825, 0.0105295972456726,
-1.85153413418145, -0.323188710035103, -0.0589264915911539,
0.741860000848901, 0.987659517391252, -0.217542415932929,
-0.0237332701760307, -0.210199811323904, 0.480403776250072,
-0.620030875875817, -0.876460650685941, -0.317406520628967,
-0.952794168007896, -0.319999914913699, -1.3316286915274,
-0.281774914754018, -0.76777164315395, -0.994548131998872,
-0.817347614586046, -0.181307768357958, 1.11715374852209,
0.639399050861402, 0.630134457849217, -0.0915725401145076,
-1.71523900325495, 0.800906843087041, -0.160391655299882,
-0.787097863742348, 0.23632512472484, -0.179783010552081,
-1.04790809440798, 1.93084760034675, -0.203343177185156,
-0.485059144063684, 0.123123233243786, 2.47502432679845,
-0.372943001667596, -0.720993545833188, -0.533049453232514,
-1.30323445917252, -0.47058984022737, -0.513020646920172,
-1.0662627912043, -0.830457885094073, 1.19166394800038,
0.898537828821606, 0.403628314437249, 0.534754482297255,
0.194168555573731, 0.297322223207161, 0.702070065980465,
0.427354253638605, 0.550765820388865, 0.0496154314917118,
-0.18783154800471, 0.281383359645264, -0.101787180249122,
0.467473794871525, 0.429333489799105, -0.925046880212712,
0.768862953876434, -0.244065842007048, -0.776634866039481,
-0.970422919673118, 0.91621374088124, -1.25133173208347,
0.586925976766684, 0.611170478831929, -0.960917249895769,
0.399177856656425, 1.0399190659828, 1.05995835961873,
-0.0208502304088755, -0.220667255620733, 0.165445576721379,
-0.114795073831474, 0.617090582805893, 0.21823817643972,
0.123207495797606, 1.42383666809597, -0.294183654221106,
0.691711203231252, -0.142925100919743, -0.179032193873003,
0.0702627574729848, 0.050761109919964, -0.370460718365878,
0.242887469540777, 0.601316253626796, 1.18056456152599,
0.0130617818565887, 0.383274585308917, 0.678831957200485,
0.774370794216735, 0.272507059890609, -0.655792750040113,
0.769805400188241, -0.693486521873978, -0.754598879188519,
-0.074234513578388, 0.159284258659505, -0.450713615502174,
-0.390642496445363, -0.442808200102512, 0.267069977558354,
1.17025338402709, -0.214563443086823, -0.887455981648988,
-0.260310917181494, -0.00786283804287108, 0.247962909868963,
-0.362811078077127, 0.741310134065408, -0.200574130010101,
0.06615901401702, -1.44879735953932, -0.951059812759707,
0.32796539362041, 1.33352570257521, 0.202301767080624,
0.417550060347928, -0.513589660508719, 0.887169302020696,
-0.307274011792591, -0.532308937796632, -1.20959762064861,
0.45600602813945, -0.0874192202404007, 0.202709375017572,
0.17294847307896, -0.567443011220909, 0.440070114304498,
-0.0462495898214306, -1.05197180669229, 0.459254566465936,
0.311883378462867, -0.132864109735062, -0.353295045617759,
0.657360728811636, -0.00434468322735784, 0.25414344776134,
1.36859672044245, 0.537296612344187, 0.662805848878859,
-0.428961273880273, -0.510298293008916, 0.908502083520438,
0.543623178256533, 0.194510179239622, 0.150953472942602,
-0.723401233383223, 0.222253838102068, -0.252464263706322,
-0.320356082060333, 0.019418223582139, 0.904531241872239,
-0.458408804709521, -0.164916747460608, 0.504399574818756,
-0.0285722916383224, -0.666632449053057, 0.0531630039692571,
0.101723326754842, -0.768629611012825, -1.19955210039743,
-0.778246378153086, 0.0675714759482876, -0.396111692515287,
1.5613083872805, -0.494767892310519, 0.875913986718127,
0.249256338465321, -0.362941176741653, 0.937178999188274,
-1.3214093512053, -0.243068895971681, 0.179619525348131,
0.0273438374107876, 0.968372096980664, -0.209418553863336,
1.06698122835133, 0.112620291820417, 0.554996780348471,
0.34794240961505, -0.34321078192519, 0.513339264686877,
-1.24218990057805, 0.0172507054843588, 1.73545904209529,
-0.310201933026909]
    end

    def weights=(weights)
        @net.weights = weights
    end

    def choose_move
        #the board is the input to the neural net plus a bias node.
        output = @net.run(@game.board+[1])
        max_index = 0
        max = -1000000
        valid_moves.each do |m|
            max_index, max = m, output[m] if output[m] > max
        end
        offset = @side == KalahGame::TOP ? 7 : 0
        max_index + offset
    end

    def valid_moves
        offset = @side == KalahGame::TOP ? 7 : 0
        moves = []
        6.times do |i|
            moves << i if @game.legal_move?(i+offset)
        end
        moves
    end
end

NeuralNet.rb:

class NeuralNet
    attr_reader :num_input, :num_hidden, :num_output, :weights

    def initialize(num_input, num_hidden, num_output)
        @num_input = num_input
        @num_hidden = num_hidden
        @num_output = num_output
        @weights = Array.new(@num_input*@num_hidden +
@num_hidden*@num_output) { rand }
    end

    def weights=(weights)
        @weights = weights
    end

    def run(inputs)
        index = 0
        hidden = Array.new(@num_hidden, 0)

        inputs.each do |i|
            @num_hidden.times do |h|
                hidden[h] += @weights[index] * i
                index += 1
            end
        end

        output = Array.new(@num_output, 0)

        hidden.each do |h|
            @num_output.times do |o|
                output[o] += @weights[index] * h
                index += 1
            end
        end

        output
    end
end

Dude, nice work. I really must take a closer look later on.

···

On 12/16/05, horndude77@gmail.com <horndude77@gmail.com> wrote:

I know this is a bit late, but I've been trying to train my neural net.
I've never used them before and I've been wanting to learn. I created a
neural net with the input being the kalah board plus one bias node(so
15 input nodes), 20 hidden nodes, and 6 output nodes. A move is
selected by running the neural net and then picking the valid move
whose corresponding output node is highest. I'm training the net right
now using Particle Swarm Optimization to find good weights.

I trained it against dave's player and adam's remove right. It found a
pretty good solution against the remove right player, but not dave's.
The best I could get after searching for a while was losing by 12. I
might have better luck if I just take dave's solution and replace the
function it uses to rate the board's goodness with a neural net and
then train it. That might be fun for later.

Anyways sorry about the lateness, but here it is.

-----Horndude77

NeuralPlayer.rb (Sorry about the long section of weights. I had them on
one line each.):

Wow!

-Ezra

···

On Dec 15, 2005, at 5:45 PM, horndude77@gmail.com wrote:

I know this is a bit late, but I've been trying to train my neural net.
I've never used them before and I've been wanting to learn. I created a
neural net with the input being the kalah board plus one bias node(so
15 input nodes), 20 hidden nodes, and 6 output nodes. A move is
selected by running the neural net and then picking the valid move
whose corresponding output node is highest. I'm training the net right
now using Particle Swarm Optimization to find good weights.

I trained it against dave's player and adam's remove right. It found a
pretty good solution against the remove right player, but not dave's.
The best I could get after searching for a while was losing by 12. I
might have better luck if I just take dave's solution and replace the
function it uses to rate the board's goodness with a neural net and
then train it. That might be fun for later.

Anyways sorry about the lateness, but here it is.

-----Horndude77

NeuralPlayer.rb (Sorry about the long section of weights. I had them on
one line each.):

require 'NeuralNet'

class NeuralPlayer < Player
    def initialize(name)
        super(name)
        @net = NeuralNet.new(15, 20, 6)
        @num_weights = 15*20 + 20*6

        #weights for RemoveRight
        #self.weights = [-0.428456550403913, -0.909217917148082,
-0.468284431041651, 0.357903882475321, -0.104252998861521,
-0.460447451110633, 0.264105895124314, -0.220335064829405,
0.344066370486408, 0.110887382259345, -0.112061654833158,
0.387531331474845, 0.253470699210902, 0.58235888221721,
0.306268984507146, 0.692431343604429, -0.0536485504199445,
1.25989522777867, -1.10462932704233, -0.351286944668706,
-0.567170711584581, 1.29779740339143, 0.907906329763339,
-0.207925256569637, 0.177102138750371, 0.128961825966903,
-1.40103570953856, 0.60460561949975, -0.293757627893575,
0.519854674076599, -0.121247185426499, -0.48243591070721,
-0.5635586279583, 0.87760707848256, -0.24059588906072,
-0.0397911286451754, -2.61454740162978, 0.100948481882159,
0.649844494979625, -0.20679545186547, 0.956480816100499,
0.379654934947368, 0.213954601603671, 0.0528884967154502,
0.625382960895802, 0.362097750351292, -0.253497508633716,
-0.883702364891605, -0.101659606324204, 0.248216287934223,
-0.113052794219895, 0.634086308736, -0.302798853606374,
-0.346890845789382, -0.779510000549479, -0.0360852571092638,
0.0153516471477581, -0.171835179564683, 0.0251128237293062,
0.645490210802246, -0.193099922624216, 0.712786971652146,
-0.152956125676004, -0.616851260423588, 0.2459019745739,
-0.955383328688331, -0.364705013321598, -0.617848226340645,
0.475091440306075, 0.354449966944016, -1.01613173478496,
0.676427027866194, 0.284847198728979, -0.841248187176398,
0.429200477632715, -0.326666975727536, -0.900577897928031,
-0.151187352160431, 0.418136290801099, 0.0941511164912963,
0.506840998984342, 0.103254060859946, 0.333803252464272,
-2.06121470406907, -0.225537973154723, -0.233342939397305,
-0.102999506187284, -0.53850202703304, -0.555527143472232,
-0.671017075023332, -0.282380091302946, 0.131082542665543,
-0.723479705286975, 0.795054642785429, 0.491077298824963,
0.423059363779898, -2.29476738228994, -0.121696962009613,
-0.328075962465621, -0.367687083335075, 0.37277431250442,
-0.891982913707634, 0.690166633881076, 2.20541381798063,
0.33841734898414, 1.34426349664742, 0.217566888046106,
-0.671506739310853, 0.0457879215134461, -0.785153977981076,
0.4399351958435, -1.07227081177901, 0.80071121984544,
-0.498359707974827, 1.50749203618481, 0.00155955509612907,
0.558230940831766, -0.762174449476105, -0.0842126063238458,
-0.861999505780237, -0.346765623726261, 0.189041394170814,
0.515209464762614, 0.953695829738959, 0.109226147741295,
-0.410604039478746, 1.26399802844183, -0.42263330334738,
-1.22115749426171, -0.331234653704824, 0.102886993963447,
-0.100692469885383, 1.20483707939992, -0.347967500808309,
0.553050973560177, -0.0947739419530351, 0.491011977559244,
0.628874101325749, -1.95690538525791, 0.297814702564266,
-0.471007824648471, -0.414483183737145, 0.0726120056956718,
-0.933570106288705, 0.194825010200492, -0.332411723772583,
-0.454179722185871, -1.52057182606227, -0.743829261927187,
0.217180769977938, 1.23707542789918, 0.249214021285456,
0.980914083149354, 0.840109804787634, 0.0549821073482349,
0.759213641648435, 0.345571660732875, 0.642177051909433,
-0.699048329339318, -0.711124967241672, -0.198273084388503,
-0.682136345206517, 0.311305839135266, 0.631334457513039,
0.600316128422118, 0.119320030158148, -0.277536611595672,
0.152726604986164, -1.12883395672669, 0.685939108743903,
0.612340495493681, -0.470976215605829, 0.192660126498784,
-0.38101262950115, 0.692042726229392, 0.375075320693942,
-0.537093511777074, -0.75716836463867, 0.133598059764474,
-0.0990331086141189, -0.342974262988399, 0.148259446813639,
-0.307505302024212, -0.691169077958319, 0.160615435359153,
-0.473318586554398, -0.147746841313201, 0.384987460864245,
-1.71568495055048, 0.605786228599172, -0.331527288831919,
-0.742009170961669, -0.286044861721401, -0.197316839222173,
-0.652244349958852, -0.6931049458475, 0.606268100395642,
-0.816028396199571, -0.276575039831182, -0.965134498229981,
-0.758589133085266, -0.215462052762639, -1.22485716505581,
0.635854825774725, 0.0347106278236468, 0.68017020773746,
0.486510650159744, -0.950942640912604, -0.408793432853341,
0.0660212214118211, 0.439275058115052, -1.90764266527444,
0.0705340780324987, -0.579390528498766, -0.594227207465761,
-0.607485381621411, -0.745915297691384, 0.648556199264886,
0.424482268251714, 0.448565852678736, -0.212875620275073,
-1.60767121899908, 0.590280093295565, 0.220889024665176,
0.519830949320126, -1.58646652366069, 0.0490820218238098,
-0.939734916717465, 0.271622882831742, 0.587123430789783,
0.356941209321922, 0.431588738070851, -0.00363682276519776,
0.632624296908395, -0.460607536832767, -0.162344504807553,
0.244686235644648, 0.365983697367206, -0.556207060994067,
-0.247592686197143, -0.256153222456516, 1.42851969844773,
0.150973382582707, 0.217898142551229, 0.0327769935098895,
0.0301879317982763, 0.0504474882971441, 0.927135059762964,
-1.32001110030053, -0.0908289933296031, -0.649306861640546,
-0.409394034911994, 0.548916567397019, -0.816455346515919,
-0.23857172691294, 0.124546229435521, -0.557501169427238,
0.0367883579939846, 0.217641257520411, -0.370946458590141,
0.665435343143148, 0.795439121655607, -0.328009918280298,
-0.889705313261023, 0.528441781909589, -0.521804444678478,
-0.257741432376917, 0.28334033531291, -0.647816542299153,
-0.11188517833591, -0.382643376445593, -0.50836534761093,
-0.881302835589924, 0.451928929683854, 0.322905810792575,
-0.295616304054877, -0.0490996038261706, -0.419065683767976,
0.0889212390738239, 0.354441291440674, 0.533502165411728,
0.39775482176289, 0.522613557593291, 0.56763651037131,
-0.553985587928485, -0.0892770054346463, 0.417242403170033,
-0.0622231365237445, -0.536468658574459, -0.625611293373744,
0.397897575429255, -0.164522809437456, -0.76074160128093,
-0.338860069316338, 0.108424735798555, 0.72183978231058,
0.270777413929673, -0.0460416847164016, -0.107106555215344,
-1.16391449205179, 0.0287701615910848, 0.338030888059846,
0.916736090240755, -0.0946791015986226, -0.337775858656124,
-0.184221985273276, 0.7317875788664, -0.246207369460492,
0.627625049453152, -0.265781090911406, 0.250113034586153,
-0.203373686280343, -0.632653811240432, 0.96838789592563,
0.19063294685656, 1.17332675801791, -0.516224856380255,
-0.264875062177442, 0.50505882714235, -0.491935430100226,
0.4549665963441, -0.414740791398929, -0.571609921257171,
-0.265250663950144, -0.475686272333388, -1.07131641800336,
-0.168325437973922, -0.17234499168695, 1.2020474172808,
0.337248901596775, -0.634358591648102, -2.94504361685402,
-0.0474679493484615, -0.46289571515561, -0.149910278399775,
-0.56911680359925, 0.856479648671053, 0.0222252254438126,
-0.0789053965464129, -0.865175832880598, 0.813113040749056,
1.40052017545579, 0.247265188021256, -0.440075796418733,
0.917240725211066, 0.726954195590911, 0.573646173145207,
-0.336511408821544, -0.222735979722609, 0.879239872079174,
-0.733586265855175, -0.41900569195846, -0.293545971690515,
-0.632169580731648, -0.0856443553620738, -0.223316902854729,
0.403983130234464, -0.0986907442550533, 0.505726677775571,
0.544979171664306, -0.92863401861874, -0.583791809420349,
-0.682365608929785, -0.620485109687089, -0.0729610489595295,
1.67633914332691, 0.557908335099985, -0.654472279868246,
1.696774835487, 0.0502110469681493, 1.18420531528306,
-0.479767058178744, -1.14370493230831, 0.0504510683261292,
-0.237302537046759, -0.0639529013404704, 0.276548508170619,
0.742445105762371, -0.792246089654982, 0.269589581769951,
-0.593650654967001, -1.10475004597524, -0.705061651554581,
0.560147802074617, 0.112770824037605, 0.308635801604607,
-0.139788519302014, -0.171711481459321, 0.0548073868984597,
0.622975084294081, -0.887766178428485, -0.0826250645225019,
-0.294980224893042, 0.324832701146147, -0.632388433547754,
-0.0743648174407521, 0.256966886000103, -0.348550409126788,
0.86243829650827, 0.110484229604374, -0.105573500449927,
-0.584517432238993, 0.119208851966649, 0.993143683984971,
-0.358145459385369, -0.0579403357513844, -0.965421816301397,
0.512560827725041, -1.42246421454656, 0.863912738871492,
1.04780140334956, -0.0646477189132773, -0.543296500968879,
0.271989197407765, -0.959413629549363, 0.280181611937799,
-0.363234787452163, 0.0239440462433509, 0.906154588569019,
0.158870148658544]

        #weights for Dave
        self.weights = [0.595765520562772, 0.572313565308922,
-1.01593258550904, -1.63154027608213, -0.242177969065067,
-1.44498150429411, -0.611346758262896, -0.714767503318283,
0.133920007475619, -0.0280544755220122, 0.805544561666266,
-0.194520526790607, 1.1332800840179, -0.899871016430959,
0.247063828714958, 0.224254662718752, -0.618371544496958,
0.684186664735964, -0.190457855344232, 0.799862285892088,
-0.555844449171634, 0.0238775186945493, -0.180573650320856,
0.354543728300561, 0.513768996058762, 0.23348678845317,
0.178371745419366, -0.591680504803924, -0.201463863457764,
1.68690987229618, 0.444379703941704, 0.516277936677377,
-0.165293331915385, 0.183888467391197, -0.207605328542711,
0.0336538540792326, 0.0830359695274841, 0.791089393285359,
0.617361158680298, 0.860187793570229, -0.85071993055499,
0.397924224079726, -0.921314700002768, 0.15306944130641,
-0.738627827917805, -0.796644068900635, -0.0234897027237452,
-0.596106214153031, 0.0103028873257534, 0.694204932672169,
-0.0783157140820865, 1.47304347282447, 0.243039689593514,
-0.154484297644386, -0.857262383343563, 0.252696814893751,
-0.40343952897704, -0.294704552057072, -0.605114371140755,
-0.117410075097659, 0.16976424826419, 0.0797140147048645,
1.15575512685767, -0.716442817770782, 0.0288381173038192,
1.24303417704093, 1.08948769036477, -0.250157841294754,
-0.221026751512915, -0.0412528829199518, -1.28136626981268,
0.0271221530977974, -0.819704733220784, 0.826844506255673,
-0.732099526633263, 0.524618630380285, -0.648083676037858,
-0.00499945681324156, -0.594934960761219, -0.776499962950219,
-0.595310019616089, -0.40437646365592, 0.0213853255182831,
-0.123200509730716, 0.595725189798745, 0.387206450180365,
-0.103128672991375, -0.796595266582018, -0.57040356766035,
0.617376786302605, -0.0492958991933076, 1.87381016576847,
0.0500697306671592, -0.00374619138529117, -0.333956103620826,
0.682428766042459, 0.45085001975015, 0.157797184138026,
0.537906024336842, 0.240119993671652, -1.71128081561554,
0.409489725584302, -0.920812577416039, -0.256255495792021,
-0.704681250903957, -0.948557914062174, -0.290420522444693,
-0.0532276068256267, 0.828402958638734, 1.35836495166806,
0.280025344929557, 0.936707731344621, -0.192066513071091,
-0.075782675806965, 1.35427201545769, -0.113366307708726,
-0.0504057876198619, 0.73601129297563, -0.897915461464336,
0.271291192038269, 0.134944553786449, 0.938718165429378,
-0.991812182124245, 0.613719915325468, -0.361579916581629,
-0.825772807653645, 1.42672684008144, 0.17313277238907,
0.283468798275685, 0.0904374445462766, 0.668305801258156,
-0.0088422724896573, 0.139621424234071, -0.903858344121345,
0.656929574729915, -0.437276244381152, 1.0358154860163,
-0.00675062072284553, 0.36195163246692, -0.414024029206097,
-0.445187541089639, -0.360619428139445, 1.07422913793284,
0.857220973547937, 1.2867735591499, -0.267002726755962,
-1.00800992274211, -0.280795345456028, -0.337565151748177,
-0.089099648711894, 0.0360080087280767, -0.447012196582776,
-0.949137872310408, -2.46913736427525, 0.20902350638093,
-0.14902418251281, -0.300060511323241, -0.0355604438556937,
-0.0946652143482725, 0.0244973704529738, 0.121535182384221,
0.877938462750686, 0.13514569157873, 0.730894434624369,
-0.728258747062556, -0.585656722420927, -0.546402878819304,
-0.343737233363635, 0.409586311239579, -0.254866830426791,
-0.242883255706001, -0.186348714111791, -0.265694023398695,
0.731393941678047, -0.0746181059090219, 0.218347383891012,
-0.812929710983027, 0.146549203387507, 0.110651741058259,
-1.09842076325924, -1.04917158207456, -1.47015228627904,
0.575126789184426, 1.85809542921445, 0.936099082727676,
-0.778637023080794, 0.416111666430353, -0.194774649283276,
-0.941750335545621, -0.131274284178395, -0.0477668866045489,
0.602038230833452, 0.569125547608728, -0.434815281396334,
0.0596187688278964, -0.789702207783062, 0.206776039104787,
0.412273228995914, -0.726223826540894, 0.23140765095681,
0.918138236244244, 0.0140589140937358, -0.879874698211939,
0.884726701492646, -0.9246077647375, 0.397956364508967,
-0.39926602290578, 0.294201967223436, -0.924516544204645,
-0.126637130005552, -0.0567156618256692, -0.211649106737817,
-0.247314351914321, -0.189047057484531, 2.15593418636414,
-0.279329113054268, 0.191024744803032, 0.395286138597393,
-1.39057157713051, 0.369662236309845, 1.04386554488724,
-1.43230866140681, -0.366813710646212, -0.430484633313484,
-2.31467831654428, -0.0955713345233841, 0.665451124992163,
0.256568831197233, 1.99980579849825, 0.0105295972456726,
-1.85153413418145, -0.323188710035103, -0.0589264915911539,
0.741860000848901, 0.987659517391252, -0.217542415932929,
-0.0237332701760307, -0.210199811323904, 0.480403776250072,
-0.620030875875817, -0.876460650685941, -0.317406520628967,
-0.952794168007896, -0.319999914913699, -1.3316286915274,
-0.281774914754018, -0.76777164315395, -0.994548131998872,
-0.817347614586046, -0.181307768357958, 1.11715374852209,
0.639399050861402, 0.630134457849217, -0.0915725401145076,
-1.71523900325495, 0.800906843087041, -0.160391655299882,
-0.787097863742348, 0.23632512472484, -0.179783010552081,
-1.04790809440798, 1.93084760034675, -0.203343177185156,
-0.485059144063684, 0.123123233243786, 2.47502432679845,
-0.372943001667596, -0.720993545833188, -0.533049453232514,
-1.30323445917252, -0.47058984022737, -0.513020646920172,
-1.0662627912043, -0.830457885094073, 1.19166394800038,
0.898537828821606, 0.403628314437249, 0.534754482297255,
0.194168555573731, 0.297322223207161, 0.702070065980465,
0.427354253638605, 0.550765820388865, 0.0496154314917118,
-0.18783154800471, 0.281383359645264, -0.101787180249122,
0.467473794871525, 0.429333489799105, -0.925046880212712,
0.768862953876434, -0.244065842007048, -0.776634866039481,
-0.970422919673118, 0.91621374088124, -1.25133173208347,
0.586925976766684, 0.611170478831929, -0.960917249895769,
0.399177856656425, 1.0399190659828, 1.05995835961873,
-0.0208502304088755, -0.220667255620733, 0.165445576721379,
-0.114795073831474, 0.617090582805893, 0.21823817643972,
0.123207495797606, 1.42383666809597, -0.294183654221106,
0.691711203231252, -0.142925100919743, -0.179032193873003,
0.0702627574729848, 0.050761109919964, -0.370460718365878,
0.242887469540777, 0.601316253626796, 1.18056456152599,
0.0130617818565887, 0.383274585308917, 0.678831957200485,
0.774370794216735, 0.272507059890609, -0.655792750040113,
0.769805400188241, -0.693486521873978, -0.754598879188519,
-0.074234513578388, 0.159284258659505, -0.450713615502174,
-0.390642496445363, -0.442808200102512, 0.267069977558354,
1.17025338402709, -0.214563443086823, -0.887455981648988,
-0.260310917181494, -0.00786283804287108, 0.247962909868963,
-0.362811078077127, 0.741310134065408, -0.200574130010101,
0.06615901401702, -1.44879735953932, -0.951059812759707,
0.32796539362041, 1.33352570257521, 0.202301767080624,
0.417550060347928, -0.513589660508719, 0.887169302020696,
-0.307274011792591, -0.532308937796632, -1.20959762064861,
0.45600602813945, -0.0874192202404007, 0.202709375017572,
0.17294847307896, -0.567443011220909, 0.440070114304498,
-0.0462495898214306, -1.05197180669229, 0.459254566465936,
0.311883378462867, -0.132864109735062, -0.353295045617759,
0.657360728811636, -0.00434468322735784, 0.25414344776134,
1.36859672044245, 0.537296612344187, 0.662805848878859,
-0.428961273880273, -0.510298293008916, 0.908502083520438,
0.543623178256533, 0.194510179239622, 0.150953472942602,
-0.723401233383223, 0.222253838102068, -0.252464263706322,
-0.320356082060333, 0.019418223582139, 0.904531241872239,
-0.458408804709521, -0.164916747460608, 0.504399574818756,
-0.0285722916383224, -0.666632449053057, 0.0531630039692571,
0.101723326754842, -0.768629611012825, -1.19955210039743,
-0.778246378153086, 0.0675714759482876, -0.396111692515287,
1.5613083872805, -0.494767892310519, 0.875913986718127,
0.249256338465321, -0.362941176741653, 0.937178999188274,
-1.3214093512053, -0.243068895971681, 0.179619525348131,
0.0273438374107876, 0.968372096980664, -0.209418553863336,
1.06698122835133, 0.112620291820417, 0.554996780348471,
0.34794240961505, -0.34321078192519, 0.513339264686877,
-1.24218990057805, 0.0172507054843588, 1.73545904209529,
-0.310201933026909]
    end

    def weights=(weights)
        @net.weights = weights
    end

    def choose_move
        #the board is the input to the neural net plus a bias node.
        output = @net.run(@game.board+[1])
        max_index = 0
        max = -1000000
        valid_moves.each do |m|
            max_index, max = m, output[m] if output[m] > max
        end
        offset = @side == KalahGame::TOP ? 7 : 0
        max_index + offset
    end

    def valid_moves
        offset = @side == KalahGame::TOP ? 7 : 0
        moves =
        6.times do |i|
            moves << i if @game.legal_move?(i+offset)
        end
        moves
    end
end

NeuralNet.rb:

class NeuralNet
    attr_reader :num_input, :num_hidden, :num_output, :weights

    def initialize(num_input, num_hidden, num_output)
        @num_input = num_input
        @num_hidden = num_hidden
        @num_output = num_output
        @weights = Array.new(@num_input*@num_hidden +
@num_hidden*@num_output) { rand }
    end

    def weights=(weights)
        @weights = weights
    end

    def run(inputs)
        index = 0
        hidden = Array.new(@num_hidden, 0)

        inputs.each do |i|
            @num_hidden.times do |h|
                hidden[h] += @weights[index] * i
                index += 1
            end
        end

        output = Array.new(@num_output, 0)

        hidden.each do |h|
            @num_output.times do |o|
                output[o] += @weights[index] * h
                index += 1
            end
        end

        output
    end
end

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732