Exemplos de Projetos
Lista de Projetos Propostos da UC de Programação em Lógica
Nethack
DEsign and implement a Prolog player that can play Nethack.
Nethack is a single-person dungeon game.THe dungeon consts of a number of levels, that grow harder as you go down.
You can use the Python-nethack interface developed for the nethack challenge.
Installing the enviroment
The easiest approach is to use the github repo. Please follow these steps:
python3 -m venv ~/venv
. ~/venv/bin/activate
git clone https://github.com/facebookresearch/nle --recursive
cd nle
pip install -e ".[dev]"
pre-commit install
Using the environment
You can now call nethack from Python. This is a first step:
import gym
import nle
env = gym.make("NetHackScore-v0")
env.reset() # each reset generates a new dungeon
env.step(1) # move agent '@' north
env.render()
----.--
|...%.|
+f....|
|@!.x.|
-------
Agent the Candidate St:18 Dx:18 Co:9 In:10 Wi:13 Ch:7 Neutral S:0
Dlvl:1 $:0 HP:14(14) Pw:4(4) AC:4 Xp:1/0 T:2
step()
method corresponds to a play. To find out which actions are available please try:
env.actions
(<MiscAction.MORE: 13>, <CompassDirection.N: 107>, <CompassDirection.E: 108>,
<CompassDirection.S: 106>, <CompassDirection.W: 104>, <CompassDirection.NE: 117>,
<CompassDirection.SE: 110>, <CompassDirection.SW: 98>, <CompassDirection.NW: 121>, <CompassDirectionLonger.N: 75>,
<CompassDirectionLonger.E: 76>, <CompassDirectionLonger.S: 74>, <CompassDirectionLonger.W: 72>, <CompassDirectionLonger.NE: 85>,
<CompassDirectionLonger.SE: 78>, <CompassDirectionLonger.SW: 66>, <CompassDirectionLonger.NW: 89>, <MiscDirection.UP: 60>,
<MiscDirection.DOWN: 62>, <MiscDirection.WAIT: 46>, <Command.KICK: 4>, <Command.EAT: 101>, <Command.SEARCH: 115>)
Finally, observations describe the state if the game. You can obtain an observation using:
obs=env.step(1)
obs=last_observation
OBSERVATION_DESC
in nle/nethack/nethack.py
and the include
directory.
Nethack in Prolog
Both YAP and SWI can call and be called by Python-code. YAP uses the python
library. The previous example would be:
:- use_module(library(python)).
:- python_import(gym).
:- python_import( nle ).
:-
env := gym.make(`NetHackScore-v0`),
env.reset(), % each reset generates a new dungeon
env.step(1), % move agent '@' north
env.render().
Playing the game
-
You may want to start with a very simple algorithm, say, always move left, and then improve. There have been quite a few There are a few sample players available. You may use them as yardsticks to compare against.
-
[ ] Don't try to handle everything ftom the very beginning. It's a hige environment. Focus on small tasks, like entering a room or deciding whether to kick or to flee.
-
Your goal is not to beat the record (it would be nice), but to write beautiful Prolog code. Do not worry if your resuts are disappointing: you won't be the first.
-
The code should include comments!!
Happy Nethacking!!
Fine Querying.
In Portugal, INE maintains statistics about all kinds of bulk Information is collected every ten years (the national Census). Estimates using partial information are often available between census.
The informtion may be at a very fine grain. Data at the parish level is available between 1991 and 2021 and covers population and housing. You can browse the data in the browser, download to QGIS using the qgis (plugin)[https://mapas.ine.pt/download/plugin.html], or just call sqlite3.
vsc@MacBook-Pro-7 build % sqlite3 LUGARES21_PORTUGAL.gpkg
SQLite version 3.43.2 2023-10-10 13:08:14
Enter ".help" for usage hints.
sqlite> .tables
LUGARES21_AC25 rtree_LUGARES21_AC26_geom
LUGARES21_AC26 rtree_LUGARES21_AC26_geom_node
LUGARES21_CONT rtree_LUGARES21_AC26_geom_parent
LUGARES21_MAD rtree_LUGARES21_AC26_geom_rowid
gpkg_contents rtree_LUGARES21_CONT_geom
gpkg_extensions rtree_LUGARES21_CONT_geom_node
gpkg_geometry_columns rtree_LUGARES21_CONT_geom_parent
gpkg_metadata rtree_LUGARES21_CONT_geom_rowid
gpkg_metadata_reference rtree_LUGARES21_MAD_geom
gpkg_ogr_contents rtree_LUGARES21_MAD_geom_node
gpkg_spatial_ref_sys rtree_LUGARES21_MAD_geom_parent
gpkg_tile_matrix rtree_LUGARES21_MAD_geom_rowid
gpkg_tile_matrix_set vgpkg_LUGARES21_AC25
rtree_LUGARES21_AC25_geom vgpkg_LUGARES21_AC26
rtree_LUGARES21_AC25_geom_node vgpkg_LUGARES21_CONT
rtree_LUGARES21_AC25_geom_parent vgpkg_LUGARES21_MAD
rtree_LUGARES21_AC25_geom_rowid
LUGARES
tables as standard db tables
select * from LUGARES21_CONT ;
sqlite> select * from LUGARES21_AC25
888 ...> ;
1|GP|1|026951|Corvo|195|205|155|383|10101.2415084094|1891231.82889174
2|GP|2|027010|Fajã Grande|165|167|81|190|4558.80963294993|670573.305038122
3|GP|3|027011|Ponta|39|39|15|24|1744.65106911556|135001.958507844
4|GP|4|027012|Fajãzinha|80|80|33|71|2991.25865593435|374268.587373426
5|GP|5|027013|Fazenda das Lajes|139|148|107|261|4763.46802344975|804077.915418515
6|GP|6|027014|Costa|31|31|17|38|3095.62107821716|361245.906206526
7|GP|7|027015|Lajedo|29|29|17|36|3065.05110139948|397700.972576389
8|GP|8|027016|Lajes das Flores|343|360|241|562|6870.49207043614|1434259.01576619
9|GP|9|027017|Lomba|117|117|76|184|3726.36379814778|492212.998615541
10|GP|10|027018|Mosteiro|27|27|11|19|1856.47496969638|131912.241467239
11|GP|11|027019|Caveira|49|50|30|75|2575.85438004819|316025.295810257
12|GP|12|027020|Cedros|48|48|33|75|2874.65853370449|256590.478563383
13|GP|13|027021|Ponta Ruiva|25|25|11|35|2378.06191484529|269820.577701033
14|GP|14|027022|Ponta Delgada|201|201|114|277|6527.5839243013|1330763.49184058
15|GP|15|027023|Santa Cruz das Flores|382|444|327|789|8278.62878808951|1129236.57550367
16|GP|16|027024|Fazenda de Santa Cruz|101|107|74|187|5621.76773974764|567425.672693288
17|GP|17|027025|Monte|99|105|85|190|3712.81217713631|583371.425096664
18|GP|18|027027|Boavista|76|80|60|166|3750.58282482039|310010.714237769
19|GP|19|027028|Ribeira dos Barqueiros|84|88|63|154|2669.03719323636|282997.355252324
20|GP|20|039891|Lugar dos Vales|18|19|16|41|2378.90684604977|125034.246975102
sqlite> SELECT id FROM rtree_LUGARES21_CONT_geom
...> WHERE minX >= -46843.1640625
...> AND maxX <= -35210.65234375
...> AND minY<= 163345.046875 AND maxY>=168634.1875;
4881
...> WHERE rtree_LUGARES21_CONT_geom.minX >= -50000.0
...> AND rtree_LUGARES21_CONT_geom.maxX <= -35000
...> AND rtree_LUGARES21_CONT_geom.minY >= 165000 AND rtree_LUGARES21_CONT_geom.maxY<=172000
...> AND LUGARES21_CONT.fid =rtree_LUGARES21_CONT_geom.id ;
Matosinhos
Senhora da Hora
São Mamede de Infesta
Pedrouços
Coarse Querying
As an alternative, the INE has tables at the "concelho" level. They cover a wider range of issues. To access them, just go to the database section in the INE estatísticas.
You will need to select the tables you are interested in. The system will show you tabbed options. If you want the detailed data, click on the "Change Selection Conditions" tab, and next on "Select All".Notice that the data may be at the country or region level. Detailed tables Check microdata for the most detailed tables.
The INteligent Database
The steps you should take are: 1. define a domainof interest; 2. find the available tables and translate them to Prolog; 3. build a prototype system with an input module, a reasoner, and an output module.
Design:
-
Input:
- A simple but effective approach is to present a sequence of menus, so that you control the user's choices into your spaces;
- otherwise, grabbing keywords, as done in the Eliza system, often works well;
- parser: you can wrte a Prolog parser;
- or you can do as for nethack and use a foreign tool. Python's [nltk]*https://www.nltk.org/howto/portuguese_en.html) has support for portuguese.
-
The core of your system will be the reasoner. Suggested:
- start from simple queries,eg,
how many people live in Leiria?
- Include comparisons,eg
what concelho has grown the most in the last 20 years?
- Include recursive queries
all contiguous concelhos that 10% richer than the national aaverage.
- define new tables, eq, ricolandia from the previous query.
- start from simple queries,eg,
-
Output: tables are a good way.