2022-12-13 13:25:31 +00:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-14 10:33:13 +00:00
|
|
|
"execution_count": 1,
|
2022-12-13 13:25:31 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"from model import *\n",
|
|
|
|
"from similarity import *\n",
|
|
|
|
"import csv"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-14 10:33:13 +00:00
|
|
|
"execution_count": 2,
|
2022-12-13 13:25:31 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
2022-12-14 10:33:13 +00:00
|
|
|
"source": [
|
|
|
|
"def create_similarity_matrix(filename, key):\n",
|
|
|
|
" with open(filename) as file:\n",
|
|
|
|
" similarity_matrix = {}\n",
|
|
|
|
"\n",
|
|
|
|
" for line in csv.DictReader(file, skipinitialspace=True):\n",
|
|
|
|
" for k, v in line.items():\n",
|
|
|
|
" if k == key:\n",
|
|
|
|
" key_v = v\n",
|
|
|
|
" similarity_matrix[key_v] = {}\n",
|
|
|
|
" else:\n",
|
|
|
|
" similarity_matrix[key_v][k] = float(v)\n",
|
|
|
|
"\n",
|
|
|
|
" return similarity_matrix"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 3,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"CaseBase(cases=499, fields=[('v', 'v_left', 'v_front', 'd_left', 'd_front', 'type_left', 'type_front', 'radius_curve(m)', 'slope_street', 'street_type', 'time', 'weather', 'type_vehicle', 'speed_limit(km/h)'), 'action'])\n"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
|
|
|
"[Case(problem={'v_left': 36.5, 'v_front': 23.0, 'd_left': -56.0, 'd_front': 45.0, 'type_left': 'sportscar', 'type_front': 'truck', 'radius_curve(m)': 2237.0, 'slope_street': 'flat', 'street_type': 'country_road (separated)', 'time': 'night', 'weather': 'dry', 'type_vehicle': 'car', 'speed_limit(km/h)': 100.0}, solution={'action': 'continue'}),\n",
|
|
|
|
" Case(problem={'v_left': 32.0, 'v_front': 28.0, 'd_left': -114.0, 'd_front': 44.0, 'type_left': 'motorcycle', 'type_front': 'car', 'radius_curve(m)': 3891.0, 'slope_street': 'ascending', 'street_type': 'autobahn', 'time': 'night', 'weather': 'dry', 'type_vehicle': 'truck', 'speed_limit(km/h)': 250.0}, solution={'action': 'continue'}),\n",
|
|
|
|
" Case(problem={'v_left': 43.0, 'v_front': 31.5, 'd_left': -98.0, 'd_front': 60.0, 'type_left': 'truck', 'type_front': 'car', 'radius_curve(m)': 1720.0, 'slope_street': 'flat', 'street_type': 'autobahn', 'time': 'dusk', 'weather': 'rain', 'type_vehicle': 'car', 'speed_limit(km/h)': 130.0}, solution={'action': 'continue'})]"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"execution_count": 3,
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
2022-12-13 13:25:31 +00:00
|
|
|
"source": [
|
|
|
|
"case_base = CaseBase.from_csv(\n",
|
|
|
|
" \"data/SIM_001.csv\",\n",
|
|
|
|
" problem_fields = (\"v\", \"v_left\", \"v_front\", \"d_left\", \"d_front\", \"type_left\", \"type_front\", \"radius_curve(m)\", \"slope_street\", \"street_type\", \"time\", \"weather\", \"type_vehicle\", \"speed_limit(km/h)\"),\n",
|
|
|
|
" solution_fields = (\"action\"),\n",
|
|
|
|
" encoding = \"utf-8\",\n",
|
|
|
|
" delimiter = \";\",\n",
|
|
|
|
" set_int = True\n",
|
|
|
|
")\n",
|
|
|
|
"print(case_base)\n",
|
|
|
|
"case_base[:3]"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-14 10:33:13 +00:00
|
|
|
"execution_count": 4,
|
2022-12-13 13:25:31 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
2022-12-14 10:33:13 +00:00
|
|
|
"source": [
|
|
|
|
"case_base.add_symbolic_sim(\n",
|
|
|
|
" field = \"type_left\",\n",
|
|
|
|
" similarity_matrix = create_similarity_matrix(\"data/vehicle_type_sim.csv\", \"type_vehicle\")\n",
|
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"case_base.add_symbolic_sim(\n",
|
|
|
|
" field = \"type_front\",\n",
|
|
|
|
" similarity_matrix = create_similarity_matrix(\"data/vehicle_type_sim.csv\", \"type_vehicle\")\n",
|
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"case_base.add_symbolic_sim(\n",
|
|
|
|
" field = \"type_vehicle\",\n",
|
|
|
|
" similarity_matrix = create_similarity_matrix(\"data/vehicle_type_sim.csv\", \"type_vehicle\")\n",
|
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"case_base.add_symbolic_sim(\n",
|
|
|
|
" field = \"slope_street\",\n",
|
|
|
|
" similarity_matrix = create_similarity_matrix(\"data/street_slope_sim.csv\", \"type_street_slope\")\n",
|
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"case_base.add_symbolic_sim(\n",
|
|
|
|
" field = \"time\",\n",
|
|
|
|
" similarity_matrix = create_similarity_matrix(\"data/time_type_sim.csv\", \"type_time\")\n",
|
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"case_base.add_symbolic_sim(\n",
|
|
|
|
" field = \"weather\",\n",
|
|
|
|
" similarity_matrix = create_similarity_matrix(\"data/weather_type_sim.csv\", \"type_weather\")\n",
|
|
|
|
")\n"
|
|
|
|
]
|
2022-12-13 13:25:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2022-12-14 10:33:13 +00:00
|
|
|
"execution_count": 5,
|
2022-12-13 13:25:31 +00:00
|
|
|
"metadata": {},
|
2022-12-14 10:33:13 +00:00
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"Your Query:\n",
|
|
|
|
" - v = 28\n",
|
|
|
|
" - v_left = 37\n",
|
|
|
|
" - v_front = 22.5\n",
|
|
|
|
" - d_left = -20\n",
|
|
|
|
" - d_front = 51\n",
|
|
|
|
" - radius_curve = 2000\n",
|
|
|
|
" - speed_limit = 200\n",
|
|
|
|
" - type_vehicle = motorcycle\n",
|
|
|
|
" - type_left = motorcycle\n",
|
|
|
|
" - type_front = motorcycle\n",
|
|
|
|
"\n",
|
|
|
|
"I recommend you this car:\n",
|
|
|
|
"Accelerated_lane_change\n",
|
|
|
|
"\n",
|
|
|
|
"Explanation:\n",
|
|
|
|
" - v_left = 37.0 (similarity: 1.00)\n",
|
|
|
|
" - v_front = 22.5 (similarity: 1.00)\n",
|
|
|
|
" - d_left = -17.0 (similarity: 0.25)\n",
|
|
|
|
" - d_front = 51.0 (similarity: 1.00)\n",
|
|
|
|
" - radius_curve(m) = 3020.0 (similarity: 0.00)\n",
|
|
|
|
" - speed_limit(km/h) = 120.0 (similarity: 0.01)\n",
|
|
|
|
" - type_vehicle = car (similarity: 0.50)\n",
|
|
|
|
" - type_left = truck (similarity: 0.50)\n",
|
|
|
|
" - type_front = truck (similarity: 0.50)\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2022-12-13 13:25:31 +00:00
|
|
|
"source": [
|
|
|
|
"query = Query.from_problems(\n",
|
|
|
|
" v = 28,\n",
|
|
|
|
" v_left = 37,\n",
|
|
|
|
" v_front = 22.5,\n",
|
|
|
|
" d_left = -20,\n",
|
|
|
|
" d_front = 51,\n",
|
2022-12-14 10:33:13 +00:00
|
|
|
" radius_curve = 2000,\n",
|
|
|
|
" speed_limit = 200,\n",
|
|
|
|
" type_vehicle = \"motorcycle\",\n",
|
|
|
|
" type_left = \"motorcycle\",\n",
|
|
|
|
" type_front = \"motorcycle\",\n",
|
2022-12-13 13:25:31 +00:00
|
|
|
")\n",
|
|
|
|
"\n",
|
2022-12-14 10:33:13 +00:00
|
|
|
"# sim_funcs: manhattan_sim, euclid_sim\n",
|
2022-12-13 13:25:31 +00:00
|
|
|
"retrieved = case_base.retrieve(\n",
|
|
|
|
" query,\n",
|
|
|
|
" v_left = euclid_sim,\n",
|
|
|
|
" v_front = euclid_sim,\n",
|
|
|
|
" d_left = euclid_sim,\n",
|
|
|
|
" d_front = euclid_sim,\n",
|
2022-12-14 10:33:13 +00:00
|
|
|
" radius_curve = manhattan_sim,\n",
|
|
|
|
" speed_limit = manhattan_sim,\n",
|
|
|
|
" type_vehicle = symbolic_sim,\n",
|
|
|
|
" type_left = symbolic_sim,\n",
|
|
|
|
" type_front = symbolic_sim,\n",
|
2022-12-13 13:25:31 +00:00
|
|
|
")\n",
|
|
|
|
"\n",
|
|
|
|
"print(\"Your Query:\")\n",
|
|
|
|
"for k, v in query.problem.items():\n",
|
|
|
|
" print(f\" - {k} = {v}\")\n",
|
|
|
|
"print()\n",
|
|
|
|
"print(\"I recommend you this car:\")\n",
|
|
|
|
"print(\" \".join(retrieved.solution.values()).capitalize())\n",
|
|
|
|
"print()\n",
|
|
|
|
"print(\"Explanation:\")\n",
|
|
|
|
"for field, sim_val in retrieved.sim_per_field.items():\n",
|
|
|
|
" print(f\" - {field} =\", retrieved.problem[field], f\"(similarity: {sim_val:.2f})\")"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
2022-12-14 10:33:13 +00:00
|
|
|
"display_name": "venv",
|
2022-12-13 13:25:31 +00:00
|
|
|
"language": "python",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
"name": "python",
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
|
|
|
"version": "3.11.0"
|
|
|
|
},
|
|
|
|
"orig_nbformat": 4,
|
|
|
|
"vscode": {
|
|
|
|
"interpreter": {
|
2022-12-14 10:33:13 +00:00
|
|
|
"hash": "4c522f398908c053844cc48bcd755f88db468f52081f171c67c4d9e41e8d16a6"
|
2022-12-13 13:25:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 2
|
|
|
|
}
|