SlideShare una empresa de Scribd logo
1 de 72
PLAYING GO
WITH CLOJURE

   Zach Tellman
    @ztellman
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
RULES OF GO
SHALL WE PLAY A GAME?
MY QUALIFICATIONS


• I’m    a mediocre Go player

•I   think this stuff is pretty cool

• I’ve   written performance-oriented Clojure before
THE GAME TREE



          X           X       X
                                  …



X O   X       O   X
                  O       …
THE GAME TREE



          X           X       X
                                  …



X O   X       O   X
                  O       …
THE GAME TREE



   a cooperative or single-player game, we only need to
• in
 search for victory

• in   a competitive game, we need to optimize for victory
MINIMAX


• assign   a value to each leaf node

       the parent the maximal child value if it’s our turn,
• assign
 or the minimal child value if it’s our opponent’s turn

• ascend    one level, and repeat
MINIMAX


• plays   a perfect game

• requires   traversing the entire tree

• there   are ~9! possible tic-tac-toe games
EVALUATION FUNCTIONS


• construct    a shallow game tree

       each leaf node a value based on our best guess at
• assign
 the outcome

• apply    minimax to these values
THE TROUBLE
             WITH HEURISTICS

• difficult   to quantify

• depth   vs. quality

• understanding a function vs. understanding its repeated,
 recursive application
THE TROUBLE
                     WITH GO


• lots   of breadth

• lots   of depth
HISTORICAL APPROACHES



• programmers     who are professional-level players

• shallow   and narrow searches, heavy evaluation functions
MONTE CARLO SIMULATION
MONTE CARLO SIMULATION

             (defn inside-circle? [x y]
               (>= 1 (+ (* x x) (* y y))))

             (defn sample []
               (inside-circle? (rand) (rand)))
1




    (0,0)
MONTE CARLO SIMULATION

             (defn inside-circle? [x y]
               (>= 1 (+ (* x x) (* y y))))

             (defn sample []
               (inside-circle? (rand) (rand)))
1




             (repeatedly 1e6 sample)
    (0,0)
MONTE CARLO SIMULATION
MONTE CARLO EVALUATION


• thestrength of a position is how often one side wins,
 given repeated, naive playouts by both sides

• possible   playouts vs plausible playouts

• eachnew playout gives us additional information about
 what is plausible
MULTI-ARMED BANDIT
MULTI-ARMED BANDIT


• multiple   levers to pull, each with an unknown expected
  reward

• exploitation   vs exploration

• if
   the search space is stable over time, we expect to
  converge on a solution
MONTE CARLO TREE SEARCH
SIMULATING GO
                WITH CLOJURE

• select   a move

• check    for suicide

• make     move

• check    for capture

• repeat
SIMULATING GO
               WITH CLOJURE

• select   a move

•   check for suicide

• make     move

•   check for capture

• repeat
INCREMENTAL STATE




           4
INCREMENTAL STATE


           6
INCREMENTAL STATE


        8
INCREMENTAL STATE


        6

        2
INCREMENTAL STATE


• we  keep track of the pseudo-liberties, but also the
  neighbor sum and sum-of-squares

• ifsum × sum = psudeo-liberties × sum-of-squares,
  there’s only one real liberty

• proof   is left as an exercise for the reader
A LITTLE ARITHMETIC

• select   a move        1 second
                         / 10,000 games per second
• check    for suicide

• make     move

• check    for capture

• repeat
A LITTLE ARITHMETIC

• select   a move        1 second
                         / 10,000 games per second
• check    for suicide   / 100 moves per game

• make     move

• check    for capture

• repeat
A LITTLE ARITHMETIC

• select   a move        1 second
                         / 10,000 games per second
• check    for suicide   / 100 moves per game

• make     move

• check    for capture

• repeat                 = 1μs per move
A MILLION TIMES
         A SECOND

(assoc {} :a 1, :b 2, :c 3, :d 4, :e 5)



(reduce + (range 10))



(set (range 5))
execute typical instruction           1 ns

fetch from L1 cache memory          0.5 ns

branch misprediction                  5 ns

fetch from L2 cache memory            7 ns

mutex lock/unlock                   25 ns

fetch from main memory             100 ns



     “Teach Yourself Programming in Ten Years”
                           Peter Norvig, 2001
execute typical instruction           1 ns

fetch from L1 cache memory          0.5 ns

branch misprediction                  5 ns

fetch from L2 cache memory            7 ns

mutex lock/unlock                   25 ns

fetch from main memory             100 ns



     “Teach Yourself Programming in Ten Years”
                           Peter Norvig, 2001
A QUICK DESCENT
      INTO
   INSANITY
 PERFORMANCE
NOT YOUR DAY JOB


• not   latency bound

• faster   is smarter

• diminishing   returns not within reach
MUTABLE STATE


“They are for experts only - if the semantics and
 implications of :volatile-mutable or :unsynchronized-
 mutable are not immediately apparent to you, you
 should not be using them.”


                                      Rich Hickey,
                         the docstring for deftype
MUTABLE STATE
MUTABLE STATE


• if
   a value is only used on a single thread, it can be
  unsynchronized

• for   everything else, use volatile

• unsynchronized     is roughly 2x faster
MUTABLE STATE
(deftype Mutable
  [^:unsynchronized-mutable ^long counter]

  IMutable
  (get-counter [_]
    counter)
  (set-counter [_ val]
    (set! counter val)))
MUTABLE STATE
(deftype Mutable
  [^:unsynchronized-mutable ^long counter]

  IMutable
  (get-counter [_]
    counter)
  (set-counter [_ val]
    (set! counter val)))

(defn add [^Mutable m ^long val]
  (set-counter m (+ (get-counter m) val)))
MUTABLE STATE
(deftype Mutable
  [^:unsynchronized-mutable ^long counter]

  IMutable
  (get-counter [_]
    counter)
  (set-counter [_ val]
    (set! counter val))
  (add [_ val]
    (set! counter (+ counter val))))
MEASURE TWICE,
            CODE ONCE
user> (bench (assoc {} :a 1, :b 2, :c 3, :d 4, :e 5))

Evaluation count : 46619100 in 60 samples of 776985 calls.

           Execution time mean   :   1.286463   us
  Execution time std-deviation   :   4.220920   ns
 Execution time lower quantile   :   1.280215   us ( 2.5%)
 Execution time upper quantile   :   1.295557   us (97.5%)

Found 4 outliers in 60 samples (6.6667 %)
    low-severe   1 (1.6667 %)
    low-mild     2 (3.3333 %)
    high-mild    1 (1.6667 %)
 Variance from outliers : 1.6389 %
CRITERIUM


• one   of the many great libraries from Hugo Duncan

• use   it at the REPL, and in your tests

• tends   to exaggerate cache coherency

• minimum     resolution appears to be ~15ns
BOTTOM-UP PERFORMANCE


• measure   each piece of your code

• measure   them when used together

• buildan intuition for how long something should take,
 and investigate when you’re wrong
INDIRECTION


“Any performance problem can be solved
 by removing a layer of indirection.”

                            Unattributed
INDIRECTION


     =                    count



==       identical?   Array/getLength
INDIRECTION
PUT IT ALL TOGETHER
> lein test pushkin.test.simulator :benchmark

-----
 random 9x9 playout
-----

Evaluation count : 501120 in 60 samples of 8352 calls.
             Execution time mean : 121.293385 us
    Execution time std-deviation : 2.534460 us
   Execution time lower quantile : 119.685345 us ( 2.5%)
   Execution time upper quantile : 128.150772 us (97.5%)

Found 8 outliers in 60 samples (13.3333 %)
    low-severe   3 (5.0000 %)
    low-mild     5 (8.3333 %)
 Variance from outliers : 9.4036 %
PUT IT ALL TOGETHER
> lein test pushkin.test.simulator :benchmark

-----
 random 9x9 playout
-----

Evaluation count : 501120 in 60 samples of 8352 calls.
             Execution time mean : 121.293385 us
    Execution time std-deviation : 2.534460 us
   Execution time lower quantile : 119.685345 us ( 2.5%)
   Execution time upper quantile : 128.150772 us (97.5%)

Found 8 outliers in 60 samples (13.3333 %)
    low-severe   3 (5.0000 %)
    low-mild     5 (8.3333 %)
 Variance from outliers : 9.4036 %
PLUMBING THE DEPTHS
      OF PERFORMANCE
          INSANITY
• simulating   C-style structs with ByteBuffers

• mimicking    C compiler offset calculation with local analysis

• creating   a bridge to heterogeneous computing
Exploitation             Strong AIs




               Pushkin
Exploration
           Possible          Plausible




    http://github.com/ztellman/pushkin
QUESTIONS?

Más contenido relacionado

Similar a Playing Go with Clojure

Research overview Oct. 2018
Research overview Oct. 2018Research overview Oct. 2018
Research overview Oct. 2018XavierDevroey
 
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)Universitat Politècnica de Catalunya
 
Simulation-based optimization: Upper Confidence Tree and Direct Policy Search
Simulation-based optimization: Upper Confidence Tree and Direct Policy SearchSimulation-based optimization: Upper Confidence Tree and Direct Policy Search
Simulation-based optimization: Upper Confidence Tree and Direct Policy SearchOlivier Teytaud
 
Optimization of water distribution systems design parameters using genetic al...
Optimization of water distribution systems design parameters using genetic al...Optimization of water distribution systems design parameters using genetic al...
Optimization of water distribution systems design parameters using genetic al...Tanay Kulkarni
 
Accelerating Key Bioinformatics Tasks 100-fold by Improving Memory Access
Accelerating Key Bioinformatics Tasks 100-fold by Improving Memory AccessAccelerating Key Bioinformatics Tasks 100-fold by Improving Memory Access
Accelerating Key Bioinformatics Tasks 100-fold by Improving Memory AccessIgor Sfiligoi
 
Introduction to Alphago Zero
Introduction to Alphago ZeroIntroduction to Alphago Zero
Introduction to Alphago ZeroChia-Ching Lin
 
Volumetric Lighting for Many Lights in Lords of the Fallen
Volumetric Lighting for Many Lights in Lords of the FallenVolumetric Lighting for Many Lights in Lords of the Fallen
Volumetric Lighting for Many Lights in Lords of the FallenBenjamin Glatzel
 
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIATypes Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIADheeraj Kataria
 
PR-252: Making Convolutional Networks Shift-Invariant Again
PR-252: Making Convolutional Networks Shift-Invariant AgainPR-252: Making Convolutional Networks Shift-Invariant Again
PR-252: Making Convolutional Networks Shift-Invariant AgainHyeongmin Lee
 
Market Basket Analysis in SQL Server Machine Learning Services
Market Basket Analysis in SQL Server Machine Learning ServicesMarket Basket Analysis in SQL Server Machine Learning Services
Market Basket Analysis in SQL Server Machine Learning ServicesLuca Zavarella
 
Build tic tac toe with javascript (3:28)
Build tic tac toe with javascript (3:28)Build tic tac toe with javascript (3:28)
Build tic tac toe with javascript (3:28)Thinkful
 
Dmytro Panchenko "Cracking Kaggle: Human Protein Atlas"
Dmytro Panchenko "Cracking Kaggle: Human Protein Atlas"Dmytro Panchenko "Cracking Kaggle: Human Protein Atlas"
Dmytro Panchenko "Cracking Kaggle: Human Protein Atlas"Lviv Startup Club
 
Cassandra deep-dive @ NoSQLNow!
Cassandra deep-dive @ NoSQLNow!Cassandra deep-dive @ NoSQLNow!
Cassandra deep-dive @ NoSQLNow!Acunu
 
Score based Generative Modeling through Stochastic Differential Equations
Score based Generative Modeling through Stochastic Differential EquationsScore based Generative Modeling through Stochastic Differential Equations
Score based Generative Modeling through Stochastic Differential EquationsSungchul Kim
 
pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"
pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"
pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"YeChan(Paul) Kim
 
Understanding Basics of Machine Learning
Understanding Basics of Machine LearningUnderstanding Basics of Machine Learning
Understanding Basics of Machine LearningPranav Ainavolu
 
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)Tech in Asia ID
 

Similar a Playing Go with Clojure (20)

Research overview Oct. 2018
Research overview Oct. 2018Research overview Oct. 2018
Research overview Oct. 2018
 
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
 
Simulation-based optimization: Upper Confidence Tree and Direct Policy Search
Simulation-based optimization: Upper Confidence Tree and Direct Policy SearchSimulation-based optimization: Upper Confidence Tree and Direct Policy Search
Simulation-based optimization: Upper Confidence Tree and Direct Policy Search
 
Optimization of water distribution systems design parameters using genetic al...
Optimization of water distribution systems design parameters using genetic al...Optimization of water distribution systems design parameters using genetic al...
Optimization of water distribution systems design parameters using genetic al...
 
Games.4
Games.4Games.4
Games.4
 
Accelerating Key Bioinformatics Tasks 100-fold by Improving Memory Access
Accelerating Key Bioinformatics Tasks 100-fold by Improving Memory AccessAccelerating Key Bioinformatics Tasks 100-fold by Improving Memory Access
Accelerating Key Bioinformatics Tasks 100-fold by Improving Memory Access
 
Introduction to Alphago Zero
Introduction to Alphago ZeroIntroduction to Alphago Zero
Introduction to Alphago Zero
 
Volumetric Lighting for Many Lights in Lords of the Fallen
Volumetric Lighting for Many Lights in Lords of the FallenVolumetric Lighting for Many Lights in Lords of the Fallen
Volumetric Lighting for Many Lights in Lords of the Fallen
 
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIATypes Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
 
PR-252: Making Convolutional Networks Shift-Invariant Again
PR-252: Making Convolutional Networks Shift-Invariant AgainPR-252: Making Convolutional Networks Shift-Invariant Again
PR-252: Making Convolutional Networks Shift-Invariant Again
 
Market Basket Analysis in SQL Server Machine Learning Services
Market Basket Analysis in SQL Server Machine Learning ServicesMarket Basket Analysis in SQL Server Machine Learning Services
Market Basket Analysis in SQL Server Machine Learning Services
 
Build tic tac toe with javascript (3:28)
Build tic tac toe with javascript (3:28)Build tic tac toe with javascript (3:28)
Build tic tac toe with javascript (3:28)
 
Dmytro Panchenko "Cracking Kaggle: Human Protein Atlas"
Dmytro Panchenko "Cracking Kaggle: Human Protein Atlas"Dmytro Panchenko "Cracking Kaggle: Human Protein Atlas"
Dmytro Panchenko "Cracking Kaggle: Human Protein Atlas"
 
Cassandra deep-dive @ NoSQLNow!
Cassandra deep-dive @ NoSQLNow!Cassandra deep-dive @ NoSQLNow!
Cassandra deep-dive @ NoSQLNow!
 
Microchip Mfg. problem
Microchip Mfg. problemMicrochip Mfg. problem
Microchip Mfg. problem
 
Optimization
OptimizationOptimization
Optimization
 
Score based Generative Modeling through Stochastic Differential Equations
Score based Generative Modeling through Stochastic Differential EquationsScore based Generative Modeling through Stochastic Differential Equations
Score based Generative Modeling through Stochastic Differential Equations
 
pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"
pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"
pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"
 
Understanding Basics of Machine Learning
Understanding Basics of Machine LearningUnderstanding Basics of Machine Learning
Understanding Basics of Machine Learning
 
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
 

Último

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 

Último (20)

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 

Playing Go with Clojure

  • 1. PLAYING GO WITH CLOJURE Zach Tellman @ztellman
  • 20. SHALL WE PLAY A GAME?
  • 21. MY QUALIFICATIONS • I’m a mediocre Go player •I think this stuff is pretty cool • I’ve written performance-oriented Clojure before
  • 22. THE GAME TREE X X X … X O X O X O …
  • 23. THE GAME TREE X X X … X O X O X O …
  • 24. THE GAME TREE a cooperative or single-player game, we only need to • in search for victory • in a competitive game, we need to optimize for victory
  • 25. MINIMAX • assign a value to each leaf node the parent the maximal child value if it’s our turn, • assign or the minimal child value if it’s our opponent’s turn • ascend one level, and repeat
  • 26. MINIMAX • plays a perfect game • requires traversing the entire tree • there are ~9! possible tic-tac-toe games
  • 27. EVALUATION FUNCTIONS • construct a shallow game tree each leaf node a value based on our best guess at • assign the outcome • apply minimax to these values
  • 28. THE TROUBLE WITH HEURISTICS • difficult to quantify • depth vs. quality • understanding a function vs. understanding its repeated, recursive application
  • 29. THE TROUBLE WITH GO • lots of breadth • lots of depth
  • 30. HISTORICAL APPROACHES • programmers who are professional-level players • shallow and narrow searches, heavy evaluation functions
  • 32. MONTE CARLO SIMULATION (defn inside-circle? [x y] (>= 1 (+ (* x x) (* y y)))) (defn sample [] (inside-circle? (rand) (rand))) 1 (0,0)
  • 33. MONTE CARLO SIMULATION (defn inside-circle? [x y] (>= 1 (+ (* x x) (* y y)))) (defn sample [] (inside-circle? (rand) (rand))) 1 (repeatedly 1e6 sample) (0,0)
  • 35. MONTE CARLO EVALUATION • thestrength of a position is how often one side wins, given repeated, naive playouts by both sides • possible playouts vs plausible playouts • eachnew playout gives us additional information about what is plausible
  • 37. MULTI-ARMED BANDIT • multiple levers to pull, each with an unknown expected reward • exploitation vs exploration • if the search space is stable over time, we expect to converge on a solution
  • 39. SIMULATING GO WITH CLOJURE • select a move • check for suicide • make move • check for capture • repeat
  • 40. SIMULATING GO WITH CLOJURE • select a move • check for suicide • make move • check for capture • repeat
  • 41.
  • 46. INCREMENTAL STATE • we keep track of the pseudo-liberties, but also the neighbor sum and sum-of-squares • ifsum × sum = psudeo-liberties × sum-of-squares, there’s only one real liberty • proof is left as an exercise for the reader
  • 47. A LITTLE ARITHMETIC • select a move 1 second / 10,000 games per second • check for suicide • make move • check for capture • repeat
  • 48. A LITTLE ARITHMETIC • select a move 1 second / 10,000 games per second • check for suicide / 100 moves per game • make move • check for capture • repeat
  • 49. A LITTLE ARITHMETIC • select a move 1 second / 10,000 games per second • check for suicide / 100 moves per game • make move • check for capture • repeat = 1μs per move
  • 50. A MILLION TIMES A SECOND (assoc {} :a 1, :b 2, :c 3, :d 4, :e 5) (reduce + (range 10)) (set (range 5))
  • 51. execute typical instruction 1 ns fetch from L1 cache memory 0.5 ns branch misprediction 5 ns fetch from L2 cache memory 7 ns mutex lock/unlock 25 ns fetch from main memory 100 ns “Teach Yourself Programming in Ten Years” Peter Norvig, 2001
  • 52. execute typical instruction 1 ns fetch from L1 cache memory 0.5 ns branch misprediction 5 ns fetch from L2 cache memory 7 ns mutex lock/unlock 25 ns fetch from main memory 100 ns “Teach Yourself Programming in Ten Years” Peter Norvig, 2001
  • 53. A QUICK DESCENT INTO INSANITY PERFORMANCE
  • 54. NOT YOUR DAY JOB • not latency bound • faster is smarter • diminishing returns not within reach
  • 55. MUTABLE STATE “They are for experts only - if the semantics and implications of :volatile-mutable or :unsynchronized- mutable are not immediately apparent to you, you should not be using them.” Rich Hickey, the docstring for deftype
  • 57. MUTABLE STATE • if a value is only used on a single thread, it can be unsynchronized • for everything else, use volatile • unsynchronized is roughly 2x faster
  • 58. MUTABLE STATE (deftype Mutable [^:unsynchronized-mutable ^long counter] IMutable (get-counter [_] counter) (set-counter [_ val] (set! counter val)))
  • 59. MUTABLE STATE (deftype Mutable [^:unsynchronized-mutable ^long counter] IMutable (get-counter [_] counter) (set-counter [_ val] (set! counter val))) (defn add [^Mutable m ^long val] (set-counter m (+ (get-counter m) val)))
  • 60. MUTABLE STATE (deftype Mutable [^:unsynchronized-mutable ^long counter] IMutable (get-counter [_] counter) (set-counter [_ val] (set! counter val)) (add [_ val] (set! counter (+ counter val))))
  • 61. MEASURE TWICE, CODE ONCE user> (bench (assoc {} :a 1, :b 2, :c 3, :d 4, :e 5)) Evaluation count : 46619100 in 60 samples of 776985 calls. Execution time mean : 1.286463 us Execution time std-deviation : 4.220920 ns Execution time lower quantile : 1.280215 us ( 2.5%) Execution time upper quantile : 1.295557 us (97.5%) Found 4 outliers in 60 samples (6.6667 %) low-severe 1 (1.6667 %) low-mild 2 (3.3333 %) high-mild 1 (1.6667 %) Variance from outliers : 1.6389 %
  • 62. CRITERIUM • one of the many great libraries from Hugo Duncan • use it at the REPL, and in your tests • tends to exaggerate cache coherency • minimum resolution appears to be ~15ns
  • 63. BOTTOM-UP PERFORMANCE • measure each piece of your code • measure them when used together • buildan intuition for how long something should take, and investigate when you’re wrong
  • 64. INDIRECTION “Any performance problem can be solved by removing a layer of indirection.” Unattributed
  • 65. INDIRECTION = count == identical? Array/getLength
  • 67. PUT IT ALL TOGETHER > lein test pushkin.test.simulator :benchmark ----- random 9x9 playout ----- Evaluation count : 501120 in 60 samples of 8352 calls. Execution time mean : 121.293385 us Execution time std-deviation : 2.534460 us Execution time lower quantile : 119.685345 us ( 2.5%) Execution time upper quantile : 128.150772 us (97.5%) Found 8 outliers in 60 samples (13.3333 %) low-severe 3 (5.0000 %) low-mild 5 (8.3333 %) Variance from outliers : 9.4036 %
  • 68. PUT IT ALL TOGETHER > lein test pushkin.test.simulator :benchmark ----- random 9x9 playout ----- Evaluation count : 501120 in 60 samples of 8352 calls. Execution time mean : 121.293385 us Execution time std-deviation : 2.534460 us Execution time lower quantile : 119.685345 us ( 2.5%) Execution time upper quantile : 128.150772 us (97.5%) Found 8 outliers in 60 samples (13.3333 %) low-severe 3 (5.0000 %) low-mild 5 (8.3333 %) Variance from outliers : 9.4036 %
  • 69. PLUMBING THE DEPTHS OF PERFORMANCE INSANITY • simulating C-style structs with ByteBuffers • mimicking C compiler offset calculation with local analysis • creating a bridge to heterogeneous computing
  • 70. Exploitation Strong AIs Pushkin Exploration Possible Plausible http://github.com/ztellman/pushkin
  • 71.

Notas del editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n