Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Answer Set Programming and Linear Programming #7184

Merged
merged 7 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@
[submodule "vendor/grammars/asciidoc.tmbundle"]
path = vendor/grammars/asciidoc.tmbundle
url = https://github.com/zuckschwerdt/asciidoc.tmbundle
[submodule "vendor/grammars/asp-syntax-highlight"]
path = vendor/grammars/asp-syntax-highlight
url = https://github.com/nickswalker/asp-syntax-highlight.git
[submodule "vendor/grammars/asp.tmbundle"]
path = vendor/grammars/asp.tmbundle
url = https://github.com/textmate/asp.tmbundle
Expand Down
2 changes: 2 additions & 0 deletions grammars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ vendor/grammars/applescript.tmbundle:
- source.applescript
vendor/grammars/asciidoc.tmbundle:
- text.html.asciidoc
vendor/grammars/asp-syntax-highlight:
- source.answersetprogramming
vendor/grammars/asp.tmbundle:
- source.asp
- text.html.asp
Expand Down
5 changes: 5 additions & 0 deletions lib/linguist/heuristics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ disambiguations:
pattern: '^import [a-z]'
- language: Lean 4
pattern: '^import [A-Z]'
- extensions: ['.lp']
rules:
- language: Linear Programming
pattern: '^(?i:minimize|minimum|min|maximize|maximum|max)(?i:\s+multi-objectives)?$'
- language: Answer Set Programming
- extensions: ['.ls']
rules:
- language: LoomScript
Expand Down
17 changes: 17 additions & 0 deletions lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ AngelScript:
codemirror_mode: clike
codemirror_mime_type: text/x-c++src
language_id: 389477596
Answer Set Programming:
type: programming
color: "#A9CC29"
extensions:
- ".lp"
interpreters:
- "clingo"
tm_scope: source.answersetprogramming
ace_mode: prolog
language_id: 433009171
Ant Build System:
type: data
color: "#A9157E"
Expand Down Expand Up @@ -3915,6 +3925,13 @@ Limbo:
tm_scope: none
ace_mode: text
language_id: 201
Linear Programming:
type: programming
extensions:
- ".lp"
tm_scope: none
ace_mode: text
language_id: 377204539
Linker Script:
type: data
extensions:
Expand Down
43 changes: 43 additions & 0 deletions samples/Answer Set Programming/15puzzle-encoding.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <incmode>.

% if set to one, permits consecutive moves in one time step
% this will not provide optimal plans but usually finds solutions much faster
#const consecutive = 0.

#program base.

% neighborhood relation
d(1,0;0,1;-1,0;0,-1).
n((X,Y),(X+DX,Y+DY)) :- dim((X,Y)), dim((X+DX,Y+DY)), d(DX,DY).

% positions at time step zero
at(0,(X,Y),N) :- in(X,Y,N).

#program step(t).

% guess moves
1 { move(t,A,B) : n(A,B) } 1 :- dim(B), not at(t-1,B,_).
0 { move(t,A,B) : n(A,B) } 1 :- move(t,B,_), consecutive == 1.

% check moves
:- 2 { move(t,A,B) }, dim(A).

% state transition
at(t,A,N) :- at(t-1,A,N), not move(t,A,_).
at(t,B,N) :- at(t-1,A,N), move(t,A,B).

% some redundant constraints
:- move(t,A,_), not at(t-1,A,_).
:- move(t,A,B), move(t-1,B,A), consecutive != 1.

#program check(t).

% check domain knowledge
:- not 1 { not at(t,A,_) : dim(A) } 1.
:- 2 { at(t,A,N) }, dim(A).

% check goal
:- go(X,Y,N), not at(t,(X,Y),N), query(t).

#show move/3.
#show at/3.
34 changes: 34 additions & 0 deletions samples/Answer Set Programming/apt.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
% implementation of APT

#include "asp/define.lp".
#include "asp/generate.lp".
#include "asp/hard.lp".

:~ type(E,quantitative), channel(E,x), priority(E,P). [1@P,E]
:~ type(E,quantitative), channel(E,y), priority(E,P). [1@P,E]
:~ type(E,quantitative), channel(E,size), priority(E,P). [2@P,E]
:~ type(E,quantitative), channel(E,color), priority(E,P). [3@P,E]

:~ type(E,ordinal), channel(E,x), priority(E,P). [1@P,E]
:~ type(E,ordinal), channel(E,y), priority(E,P). [1@P,E]
:~ type(E,ordinal), channel(E,color), priority(E,P). [2@P,E]
:~ type(E,ordinal), channel(E,size), priority(E,P). [3@P,E]

:~ type(E,nominal), channel(E,x), priority(E,P). [1@P,E]
:~ type(E,nominal), channel(E,y), priority(E,P). [1@P,E]
:~ type(E,nominal), channel(E,color), priority(E,P). [2@P,E]
:~ type(E,nominal), channel(E,shape), priority(E,P). [3@P,E]
:~ type(E,nominal), channel(E,size), priority(E,P). [4@P,E]

:- channel(_,text).
:- channel(_,detail).
:- channel(_,row).
:- channel(_,column).

% don't bin, use log, or aggregate
:- bin(_).
:- log(_).
:- aggregate(_,_).

#show mark/1.
#show channel/2.
25 changes: 25 additions & 0 deletions samples/Answer Set Programming/onmodel-py.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#script (python)

import clingo

def main(prg):
def on_model(m):
print("shown")
print(" positive: " + ", ".join(map(str, m.symbols(shown=True))))
print(" negative: " + ", ".join(map(str, m.symbols(shown=True, complement=True))))
print("atoms")
print(" positive: " + ", ".join(map(str, m.symbols(atoms=True))))
print(" negative: " + ", ".join(map(str, m.symbols(atoms=True, complement=True))))
print("terms")
print(" positive: " + ", ".join(map(str, m.symbols(terms=True))))
print(" negative: " + ", ".join(map(str, m.symbols(terms=True, complement=True))))

prg.ground([("base", [])])
prg.solve(on_model=on_model)

#end.

{a}.
b :- a.
#show c : a.
#show b/0.
19 changes: 19 additions & 0 deletions samples/Linear Programming/Multiobj.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
\ Model Multiobj
\ LP format - for model browsing. Use MPS format to capture full model detail.
Maximize multi-objectives
Set0: Priority=3 Weight=1 AbsTol=1 RelTol=0.01
El0 + El1 + El2 + El3 + El4 + El5 + El6 + El7 + El8 + El9
Set1: Priority=2 Weight=0.25 AbsTol=2 RelTol=0.01
El5 + El6 + El7 + El8 + El9 + El15 + El16 + El17 + El18 + El19
Set2: Priority=2 Weight=1.25 AbsTol=3 RelTol=0.01
El3 + El4 + El6 + El7 + El13 + El14 + El16 + El17
Set3: Priority=1 Weight=1 AbsTol=4 RelTol=0.01
El3 + El4 + El5 + El9 + El10 + El11 + El15 + El16 + El17
Subject To
Budget: El0 + El1 + El2 + El3 + El4 + El5 + El6 + El7 + El8 + El9 + El10
+ El11 + El12 + El13 + El14 + El15 + El16 + El17 + El18 + El19 <= 12
Bounds
Binaries
El0 El1 El2 El3 El4 El5 El6 El7 El8 El9 El10 El11 El12 El13 El14 El15 El16
El17 El18 El19
End
61 changes: 61 additions & 0 deletions samples/Linear Programming/diet.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
\ This file has been generated by DOcplex
\ ENCODING=ISO-8859-1
\Problem name: diet

Minimize
obj: 0.840000000000 Roasted_Chicken + 0.780000000000 Spaghetti_W__Sauce
+ 0.270000000000 Tomato,Red,Ripe,Raw + 0.240000000000 Apple,Raw,W_Skin
+ 0.320000000000 Grapes + 0.030000000000 Chocolate_Chip_Cookies
+ 0.230000000000 Lowfat_Milk + 0.340000000000 Raisin_Brn
+ 0.310000000000 Hotdog
Subject To
c1: 277.400000000000 Roasted_Chicken + 358.200000000000 Spaghetti_W__Sauce
+ 25.800000000000 Tomato,Red,Ripe,Raw + 81.400000000000 Apple,Raw,W_Skin
+ 15.100000000000 Grapes + 78.100000000000 Chocolate_Chip_Cookies
+ 121.200000000000 Lowfat_Milk + 115.100000000000 Raisin_Brn
+ 242.100000000000 Hotdog- Rgc1 = 2500
c2: 21.900000000000 Roasted_Chicken + 80.200000000000 Spaghetti_W__Sauce
+ 6.200000000000 Tomato,Red,Ripe,Raw + 9.700000000000 Apple,Raw,W_Skin
+ 3.400000000000 Grapes + 6.200000000000 Chocolate_Chip_Cookies
+ 296.700000000000 Lowfat_Milk + 12.900000000000 Raisin_Brn
+ 23.500000000000 Hotdog- Rgc2 = 1600
c3: 1.800000000000 Roasted_Chicken + 2.300000000000 Spaghetti_W__Sauce
+ 0.600000000000 Tomato,Red,Ripe,Raw + 0.200000000000 Apple,Raw,W_Skin
+ 0.100000000000 Grapes + 0.400000000000 Chocolate_Chip_Cookies
+ 0.100000000000 Lowfat_Milk + 16.800000000000 Raisin_Brn
+ 2.300000000000 Hotdog- Rgc3 = 30
c4: 77.400000000000 Roasted_Chicken + 3055.200000000000 Spaghetti_W__Sauce
+ 766.300000000000 Tomato,Red,Ripe,Raw + 73.100000000000 Apple,Raw,W_Skin
+ 24 Grapes + 101.800000000000 Chocolate_Chip_Cookies
+ 500.200000000000 Lowfat_Milk + 1250.200000000000 Raisin_Brn- Rgc4 = 50000
c5: 11.600000000000 Spaghetti_W__Sauce + 1.400000000000 Tomato,Red,Ripe,Raw
+ 3.700000000000 Apple,Raw,W_Skin + 0.200000000000 Grapes + 4 Raisin_Brn-
Rgc5 = 100
c6: 58.300000000000 Spaghetti_W__Sauce + 5.700000000000 Tomato,Red,Ripe,Raw
+ 21 Apple,Raw,W_Skin + 4.100000000000 Grapes
+ 9.300000000000 Chocolate_Chip_Cookies + 11.700000000000 Lowfat_Milk
+ 27.900000000000 Raisin_Brn + 18 Hotdog- Rgc6 = 300
c7: 42.200000000000 Roasted_Chicken + 8.200000000000 Spaghetti_W__Sauce
+ Tomato,Red,Ripe,Raw + 0.300000000000 Apple,Raw,W_Skin
+ 0.200000000000 Grapes + 0.900000000000 Chocolate_Chip_Cookies
+ 8.100000000000 Lowfat_Milk + 4 Raisin_Brn + 10.400000000000 Hotdog- Rgc7
= 100

Bounds
Roasted_Chicken <= 10
Spaghetti_W__Sauce <= 10
Tomato,Red,Ripe,Raw <= 10
Apple,Raw,W_Skin <= 10
Grapes <= 10
Chocolate_Chip_Cookies <= 10
Lowfat_Milk <= 10
Raisin_Brn <= 10
Hotdog <= 10
-500 <= Rgc1 <= 0
-800 <= Rgc2 <= 0
-20 <= Rgc3 <= 0
-45000 <= Rgc4 <= 0
-75 <= Rgc5 <= 0
-300 <= Rgc6 <= 0
-50 <= Rgc7 <= 0
End
7 changes: 7 additions & 0 deletions test/test_heuristics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,13 @@ def test_lisp_by_heuristics
}, "main.lisp")
end

def test_lp_by_heuristics
assert_heuristics({
"Answer Set Programming" => all_fixtures("Answer Set Programming", "*.lp"),
"Linear Programming" => all_fixtures("Linear Programming", "*.lp")
})
end

def test_ls_by_heuristics
assert_heuristics({
"LiveScript" => all_fixtures("LiveScript", "*.ls"),
Expand Down
1 change: 1 addition & 0 deletions vendor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This is a list of grammars that Linguist selects to provide syntax highlighting
- **Alpine Abuild:** [atom/language-shellscript](https://github.com/atom/language-shellscript)
- **Altium Designer:** [textmate/ini.tmbundle](https://github.com/textmate/ini.tmbundle)
- **AngelScript:** [wronex/sublime-angelscript](https://github.com/wronex/sublime-angelscript)
- **Answer Set Programming:** [nickswalker/asp-syntax-highlight](https://github.com/nickswalker/asp-syntax-highlight)
- **Ant Build System:** [textmate/ant.tmbundle](https://github.com/textmate/ant.tmbundle)
- **Antlers:** [Stillat/vscode-antlers-language-server](https://github.com/Stillat/vscode-antlers-language-server)
- **ApacheConf:** [textmate/apache.tmbundle](https://github.com/textmate/apache.tmbundle)
Expand Down
1 change: 1 addition & 0 deletions vendor/grammars/asp-syntax-highlight
Submodule asp-syntax-highlight added at 913ac8
31 changes: 31 additions & 0 deletions vendor/licenses/git_submodule/asp-syntax-highlight.dep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
name: asp-syntax-highlight
version: 913ac8ffc70f5d72c07d74637511d632c175c410
type: git_submodule
homepage: https://github.com/nickswalker/asp-syntax-highlight.git
license: mit
licenses:
- sources: LICENSE
text: |
MIT License

Copyright (c) 2017 Arnaud Belcour

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
notices: []
Loading