Finding all squares within range, and finding the shortest path to the selected square - graph-algorithm

I have a grid system with characters and obstacles, and I need to 1) find out all the squares on the grid that are within a range, say 2 steps from the current location 2) then find the shortest path to a square that is selected from the ones that are within the range.
E.g. if I had a 6 x 6 grid with # being obstacles and I wanted to get from A to B:
. . . . . .
. A . . . .
. # # . . .
. B . . . .
. . . . . .
. . . . . .
So I would first find the squares / points on the grid within say 5 steps from A:
x x x x x x
x A x x x x
x # # x x x
x B x x x .
x x . x . .
x . . . . .
And then if it is true that B is in those points, find the shortest path there, in this case from the left side of the obstacles. Bad example in the sense that there is no other option, but there could well be if the range was higher. And the range is not something I know beforehand.
. . . . . .
o A . . . .
o # # . . .
o B . . . .
. . . . . .
. . . . . .
I could do the first part, finding all points within range, with BFS. And maybe the second one with Dijkstra's. But is this the most efficient way of doing it? Is there maybe a better way to do both somewhat similar things with one algorithm or less bruteforcing?
It will be part of the game where I first highlight the squares where one could move and then once a square is selected the character moves there. I'm using Python and Pygame if that matters.

Related

How to add time series data stored in a csv column to rdf graph

I have a set of multiple files that store time series data.
The time series data is stored as a CSV file with multiple large columns.
E.g.:
Time
Force
1
0.1
2
0.2
3
0.2
4
0.3
...
...
I would like to make this data accessible using RDF without conversion of the actual data into RDF. The columns in the file should be related to ontology individuals.
I know I can create a json-ld file that stores the meta information of the file. I read that this file is usually stored together with the csv file as "-metadata.json" file.
I made a minimal example for my table:
{
"#context":[
"http://www.w3.org/ns/csvw"
],
"url":"PATH-TO-URL",
"dialect":{
"delimiter":"\t",
"skipRows":4,
"headerRowCount":2,
"encoding":"ISO-8859-1"
},
"tableSchema":{
"columns":[
{
"titles":"Time",
"#id":"Time",
"#type":"Column"
},
{
"titles":"Force",
"#id":"Force",
"#type":"Column"
}
]
}
}
Now I also have an ontology, with individuals representing time and force of a specific experiment (F1 and T1). This ontology is stored in a tripplestore.
E.g.:
#prefix : <http://www.semanticweb.org/example#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix xml: <http://www.w3.org/XML/1998/namespace> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#base <http://www.semanticweb.org/example> .
<http://www.semanticweb.org/example> rdf:type owl:Ontology .
#################################################################
# Object Properties
#################################################################
### http://www.semanticweb.org/example#hasDataReference
:hasDataReference rdf:type owl:ObjectProperty .
#################################################################
# Classes
#################################################################
### http://www.semanticweb.org/example#Force
:Force rdf:type owl:Class .
### http://www.semanticweb.org/example#Time
:Time rdf:type owl:Class .
#################################################################
# Individuals
#################################################################
### http://www.semanticweb.org/example#F1
:F1 rdf:type owl:NamedIndividual ,
:Force .
### http://www.semanticweb.org/example#T1
:T1 rdf:type owl:NamedIndividual ,
:Time .
I would like to relate these individuals to the columns stored in the "-metadata.json" file. So that the user can locate the specific column by following the graph from the individuals.
What would be the correct way to link the individuals with the column location ? Do I need to convert the json-ld to ttl, add it to the tripplestore and then add a relation to the column, e.g. using my "hasDataReference" relation?
I think the file itself could be made accessible using an ontology like dcat.
In general, I am looking for the recommended way to make csv data (e.g. the columns and rows) available via an ontology-based graph.
I do not want to load the entire column in the tripplestore only the location of the column in the file, so that the user can extract the column if needed.

Jena Fuseki missed inference?

I'm using Jena Fuseki 3.13.1 (with OWLFBRuleReasoner), and I have asserted (uploaded) the following triples:
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix f: <http://vleo.net/family#> .
f:Bob f:hasWife f:Alice .
f:Bob f:hasWife f:Alice2 .
f:Alice2 f:hasHusband f:Bob2 .
f:hasWife a owl:FunctionalProperty .
f:hasWife a owl:InverseFunctionalProperty .
f:hasHusband owl:inverseOf f:hasWife .
Now, If I query and ASK { f:Alice owl:sameAs f:Alice2 }, I get true.
However, If I ASK { f:Bob owl:sameAs f:Bob2 }, I get false! Loading the same triples on another reasoner (owl-rl), I get the triple f:Bob owl:sameAs f:Bob2 inferred.
What is happening here?
I have worked with jena reasoner following this doc:
https://jena.apache.org/documentation/inference/
I have many years of experience with jena, and had never used OWLFBRuleReasoner, and it does not appear in the indicated doc, which is curious for me.
Not all reasoners work with the same construct, and that is the reason why I check the doc, that means perhaps OWLFBRuleReasoner does not use the same owl construct as the another reasoner you used (owl-rl).
Another thing is that, as I understand your KB is inconsistent, because you are declaring:
f:hasWife a owl:FunctionalProperty
But, you are assigning 2 values to it, which must make your KB inconsistent.
Luis Ramos
A follow-up: as suggested by UninformedUser, I've asked this one on the Jena mailing list and got an answer from Dave.
Jena's implementation trades some reasoning completeness for performance, the solution in this case is to add explicitly the forward version of inverseOf to the owl-fb rules file:
[inverseOf2b: (?P owl:inverseOf ?Q), (?X ?P ?Y) -> (?Y ?Q ?X) ]
The details are in this thread.

How to create user defined datatypes in Apache Jena?

I'm creating an ontology using Apache Jena. However, I can't find a way of creating custom datatypes as in the following example:
'has value' some xsd:float[>= 0.0f , <= 15.0f].
Do you have any ideas?
It seems what you need is DatatypeRestriction with two facet restrictions: xsd:minInclusive and xsd:maxInclusive.
It is OWL2 constructions.
org.apache.jena.ontology.OntModel does not support OWL2, only OWL1.1 partially (see documentation), and, therefore, there are no builtin methods for creating such data-ranges (there is only DataOneOf data range expression, see OntModel#createDataRange(RDFList)).
So you have to create a desired datatype manually, triple by triple, using the general org.apache.jena.rdf.model.Model interface.
In RDF, it would look like this:
_:x rdf:type rdfs:Datatype.
_:x owl:onDatatype DN.
_:x owl:withRestrictions (_:x1 ... _:xn).
See also owl2-quick-guide.
Or, to build such an ontology, you can use some external utilities or APIs.
For example, in ONT-API (v. 2.x.x) the following snippet
String ns = "https://stackoverflow.com/questions/54131709#";
OntModel m = OntModelFactory.createModel()
.setNsPrefixes(OntModelFactory.STANDARD).setNsPrefix("q", ns);
OntDataRange.Named floatDT = m.getDatatype(XSD.xfloat);
OntFacetRestriction min = m.createFacetRestriction(OntFacetRestriction.MinInclusive.class,
floatDT.createLiteral("0.0"));
OntFacetRestriction max = m.createFacetRestriction(OntFacetRestriction.MaxInclusive.class,
floatDT.createLiteral("15.0"));
OntDataRange.Named myDT = m.createDatatype(ns + "MyDatatype");
myDT.addEquivalentClass(m.createDataRestriction(floatDT, min, max));
m.createResource().addProperty(m.createDataProperty(ns + "someProperty"),
myDT.createLiteral("2.2"));
m.write(System.out, "ttl");
will produce the following ontology:
#prefix q: <https://stackoverflow.com/questions/54131709#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
[ q:someProperty "2.2"^^q:MyDatatype ] .
q:MyDatatype a rdfs:Datatype ;
owl:equivalentClass [ a rdfs:Datatype ;
owl:onDatatype xsd:float ;
owl:withRestrictions ( [ xsd:minInclusive "0.0"^^xsd:float ]
[ xsd:maxInclusive "15.0"^^xsd:float ]
)
] .
q:someProperty a owl:DatatypeProperty .

How to quit from 'auto' block in gitbook ?

I am new to gitbook . As I have started writing books in gitbook , I am facing some problems . The following issues are needed to known to me :
1) I can write mathematical equations in gitbook using katex/latex . For example , I can write the following equations in gitbook :
$$\frac{1}{2}$$
After this code , a fraction is appeared as 1/2 . This works fine at the time of editing . But at the time of reading , no fraction is appeared. Only the above code is shown . How to get escape from this problem ?
2) If I want to position an equation/image or whatever , I am surprisingly observe that that image is gone and instad a code is appeared .
After googling , I have come to know that a code block will be appeared if I keep 4 spaces or a tab spaces .
So what should I do if I want to keep an equation or an image at center ?
3) I am editing my book in this url : . From which url , will i read my book ?

Trying to Learn Hexagony

I'm trying to wrap my head around the programming language Hexagony. I've looked at a few sample programs and have tried writing a simple program to read input and print Hey [input]!. Here's what I have.
, < . . C $
. . { . # . ;
. . . # . . 2 '
. . . . # C 3 < .
. # . # . . * . ; .
. E . . E . ; . . > /
= . . . } y . . 3 .
. . . . ; . . 3 .
. . . e $ . ; .
. . ; . . # .
. H . . > &
Try it online!
If there is no input it prints Hey ! as expected. But with any input it prints EE!. I'm pretty sure my issue is I don't understand how the memory pointers work on the hexagonal grid. I'm assuming my code has issues with over writing values if the input is too many string but I'm not concerned with that as of yet. I confused as to the exact rules for which edge is selected with the { and } commands and how the direction of the MP is affected with this.
If anyone could give me a more explicit explanation of how the memory works how the MP is oriented with a shift that would be appreciated
Here's a diagram for how the memory movement commands work:
The memory pointer (MP) has a position (which is an edge of the memory grid) and an orientation along that edge. In the diagram, the initial position of the MP is marked with the red arrow, so it currently points north.
The MP movement commands are always relative to the current position and orientation of the MP. I've annotated the four adjacent edges based on which command gets you there. So { moves the MP forward and to left, and ' moves it backward and to the right, for example.
To figure out its new orientation, you can think of this movement as rotating the MP by 60 degrees about one of the adjacent hexagons. So depending on the chosen command, the MP would point in the following directions for the above diagram:
Cmd Orientation
{ north west
} north east
" north east
' north west
Also, just in case this is part of the confusion, remember that the memory grid is independent of the source grid (so these are not the edges of the grid containing the commands... it's just a separate, and infinite, hexagonal grid).
As for your actual program, it seems to have bigger issues than memory layout. Most of all, I'm not sure what those # are doing in there: # switches to one of the 6 IPs based on the current memory value modulo 6. Assuming your input is arbitrary, this basically switches you to a random IP, which is probably not what you want.
I can highly recommend trying out Timwi's EsotericIDE (which I also used to generate the diagram above), which allows you to step through the program and includes a visualisation of the memory grid.
I'm also happy to help you in this SE chat room if you have any further questions about the language.

Resources