ラベル micro:bit microbit の投稿を表示しています。 すべての投稿を表示
ラベル micro:bit microbit の投稿を表示しています。 すべての投稿を表示

2021年1月28日木曜日

プログラミング例題(ハミルトン閉路問題):micro:bit版

  【what is this】マイクロビット(micro:bit)のプログラミング例題として、有向グラフのハミルトン閉路問題をやってみました。これは(NP完全問題)であり、効率的な解法は無いようです。そこで、全パス検索になるのですが、DNA(分子生物学の)を使って試験管のなかでこの問題を解いたAdelmanに習い、解の候補となる経路を事前に生成して、それをふるいにかける方式としました。この例題作成をとおして、micro:bitのプログラミングシステムが非常に優れていることを再確認しました。

(⇒本記事の続編はこちらにあります。)

■micro:bitでの有向グラフのハミルトン閉路問題
 micro:bitは最近Version2が発表され、メモリの増強(RAM 8倍化、Flash 2倍化)、タッチセンサやマイク、スピーカーの内蔵などがなされています。今回、これを記念して?ハミルトン閉路問題(始点ノードから出発して、全てのノードを1度だけ訪問し、始点へ戻る経路を探索)を解いてみます。高校生向け講座などの例題になるかも知れません。

 問題設定ですが、micro:bitには、5x5のLED表示しかありませんので、5ノードの有向グラフに限定します。図1のように、ボタンAを押してランダムに5ノード間の接続行列を作るようにします。



 次にボタンBを押すとその解を探索し、見つかった解の個数を表示します。解がなければ0個となります。さらにタッチセンサに触れると、解を電光掲示板方式で1文字づつ流して表示します。これだけですが、ちょっと楽しいプログラミングとなるはずです。


■
有向グラフのハミルトン経路問題の解法
 この解法は、全パス検索になります。ただし、AdelmanのDNAを利用した(試験管のなかでの)解法を念頭におき、事前に解の候補をすべて生成します。すなわち、ノード1, 2, 3, 4の順列(4! = 24個しかありませんが)を生成し、その先頭と末尾に0を付加した経路シーケンスが、上で生成した接続行列から作れるかを検査します。出題となる接続行列の個数は約100万個(= 2の20乗)あるのですが、そのいずれに対しても24個の可能性を調べれば良いことになります。ランダムに生成した接続行列では、解が存在しない場合も多く、解があっても、数個程度以内となります。

■micro:bitプログラミング
 この解法のプログラムの主要部を図3に示します。ブロック形式、μPython、JavaScriptの3種が利用できます。ここで特筆すべきは、大まかには最初にブロック形式で作成して、細部をμPythonで作ることができることです。図ではmakePathという関数を作っているのですが、再帰的構造になり、ブロック形式で作成していると細部が考えにくい状況になりました。そこで、μPythonに切り替えて作成するとかなり楽でした。そして、その後、再度ブロック形式に戻ることができるのです!

 さらに素晴らしいと思ったのは、実機のmicro:bitでやる前に、PCでエディタやシュミレータを使えることです。それには、デバッガーやコンソールも使えるという優れた開発環境になっているのです。


■micro:bitは誰のものか?
 micro:bitは、通常、小中高生のプログラミング教育などに広く利用されています。大学ではあまり関心は高くないようです。ですが、上記の経験からも、これは大学でも、初級プログラミングにはかなり使えのではないかと思った次第です。
 micro:bit単体では、確かに作れるアプリに限界があります。しかし、他の機器やスマホとの連動機能も豊富なので、それを使うとかなり高度なアプリも楽しく作れると思います。

■小生がこれまでに作成したmicro:bitアプリもご参考に
 ちょっと手前味噌になりますが、小生がこれまでに、micro:bitと他の機器などと連携させて作成したアプリの情報は以下にあります。ご参考になれば幸いです。

⇒本記事の続編はこちらにあります。

2018年5月14日月曜日

Cooperation between micro:bit and MIT App Inventor (No.3)

In the last article (No.2), communication between micro:bit and Android smartphone was done with Bluetooth UART service (serial over Bluetooth). I will also use this UART this time, but I would like to consider a simple example including NFC which is a short range wireless communication other than Bluetooth.

The figure below shows the contents. Prepared here a big toy dice. We prepared six NFC tags on which the number of eyes on each side are written, and incorporated them on each surface of the dice. Flip over the Android terminal and touch the dice on the back of it. Then the number of eyes on the opposite side of the touched surface is read. The number of eyes read is sent to micro:bit by UART communication. Pressing the micro:bit buttonA will make it receive that information and display the number of eyes on the micro:bit LED.



please see also the following youtube:
https://youtu.be/aOb69nweq3c

Using NFC in addition to BLE UART

The Android program was created using MIT App Inventor. After reading NFC tag information (number of eyes of dice), it will send the number of eyes on the opposite side of that face to micro:bit using UART. At the same time, an image corresponding to the number of eyes should be displayed on Android's screen.

Android program (reading NFC tags and sending with UART)

On the micro:bit side, pressing buttonA gets the number of eyes sent from Android, so we just display it on the LED. The function "show_spots_on_dice" is defined with JavaSript as a custom block. In this example, since we received 4 as the number of eyes, the four outer corners of the LED are lit.


micro:bit program to receive the pips of a dice and to display it on the LED board