heal-research / HeuristicLab

HeuristicLab - An environment for heuristic and evolutionary optimization
https://dev.heuristiclab.com
GNU General Public License v3.0
38 stars 16 forks source link

Shape constraint parser refactoring #3170

Closed DavidPiringer closed 2 years ago

DavidPiringer commented 2 years ago

Due to the usage of complex regular expressions, I decided to refactor the shape constraint parser component using the recursive descent technique. The grammar for shape constraints changed slightly, new keywords for regions ('where') and weight ('with') are introduced as shown in the following EBNF.

ShapeConstraintList = { ShapeConstraint } .
ShapeConstraint = ['#'] Shape 'in' Interval [ 'where' RegionList ] [ 'with' Weight ] .
Shape = Func | ('d' Func '/' 'd' Variable) | ('d²' Func '/' 'd' Variable '²') | ('d³' Func '/' 'd' Variable '³') .
Func = 'f' | text .
Interval = '[' (number | '-Inf.') ('..' | ',') (number | 'Inf.') ']' .
Variable = text .
RegionList = Region { ',' Region } .
Region = Variable 'in' Interval .
Weight = 'weight' ':' number .

Working Examples:

f in [-inf. .. 0] where G in [-10 .. 10], c in [-1234.23 .. 7.53e-4] with weight: 1.2
∂f/∂c in [0 .. inf.]
df/dG in [-inf. .. 0]
d²f/dG² in [-inf. .. 0] where c in [-50 .. 10]
∂³f/∂c³ in [0 .. 0]

For error handling, the parser throws exceptions containing an error message with the next expected symbol and the location of the error with line/column information.

gkronber commented 2 years ago

Please add unit tests. Please change '#' symbol to mark beginning of comment until the end of the line.

gkronber commented 2 years ago

Reviewed changes, tested existing unit tests that are affected as well as new unit tests. Tested parser in GUI. The only minor issue is that the synchronization between the CheckedItemList of constraints and the string representation now drops disabled / commented constraints when the string representation is parsed again.