Package: cgshop2021-pyutils
Official Python Utilities for the CG:SHOP 2021 Optimization Competition.We provide basic code to ease your participation in this year'schallenge.
Due to it popularity and simplicity, the choice has fallen on Python.This python module allows you to easily read the instance and potentially convert them
into an easier format.
The JSON format is simple but it is very common and allows to easily add metadata.
Further, this module allows you to verify your instances.
It uses the same core as the server so if this code accepts your solution, so will our server.The code is not perfect but we will work to improve this code during the competition.
Feedback is welcome.This time, the code is pure Python and should run on all machines.
The source code will be soon published.
We are currently deciding for the right platform (probably GitHub).FeaturesReading and writing instance files.Reading and writing solution files.Verifying solutions for feasibility.The exact same implementation will be used on the submission server.
Thus, if this library will accept your solution, so will the submission server.InstallationThe installation is trivial using pip.pip install cgshop2021-pyutilsNote that you have to use_instead of-when importing the module.Maybe you need to add to--userflag to save the package into your home directory instead of the system's directory.You will also be able to simply copy the source code into your code without any installation.
However, please keep this library updated as there will probably be some forgotten corner cases.
(Actually, at the time I am writing this, the code has not been properly tested as we had to prioritize instance generation and the server. Give us a few days.)pip install cgshop2021-pyutils --upgradeReading the instances from the ZipBest use the instance database. You simply download the .zip and then you can access all instances viafromcgshop2021_pyutilsimportInstanceDatabaseidb=InstanceDatabase("path/to/zip.zip")foriinidb:print("Instance:",i)Working with an instancefromcgshop2021_pyutilsimportInstancei:Instance#just to enable typingforrinrange(i.number_of_robots):print("Robot",i,"starts at",i.start_of(r)," and has to go to ",i.target_of(r))foroini.obstacles:print(o,"is blocked")Create a solutionfromcgshop2021_pyutilsimportSolution,SolutionStep,SolutionZipWriter,Directionfromcgshop2021_pyutilsimportInstanceinstance:Instancesolution=Solution(instance)# First time step in the solutionstep_1=SolutionStep()step_1[2]=Direction.NORTH# Robot 2 moves northstep_1[1]=Direction.WEST# Robot 1 moves westsolution.add_step(step_1)# Second time step in the solutionstep_2=SolutionStep()step_2[0]=Direction.SOUTH# Robot 0 moves northstep_2[2]=Direction.WEST# Robot 2 moves westsolution.add_step(step_2)print("Makespan:",solution.makespan)print("SUM:",solution.total_moves)Write solutionsYou can easily write solutions to an uploadable zip.fromcgshop2021_pyutilsimportSolutionZipWriter,Solutions1:Solutions2:Solutions3:SolutionwithSolutionZipWriter("output.zip")asszw:szw.add_solution(s1)szw.add_solutions([s1,s2])Validate your solutionSolutions can be validate viavalidate.
This function does not return anything but will throw an exception if something is wrong.
The exception will contain an explanation (by type and message).fromcgshop2021_pyutilsimportInstanceDatabase,ZipSolutionIterator,validate,ZipReaderError,InvalidSolutionError,SolutionEncodingErroridb=InstanceDatabase("path/to/instances.zip")try:forsolutioninZipSolutionIterator(idb)("/path/to/my_solutions.zip"):validate(solution)exceptZipReaderErroraszre:print("Bad Zip:",zre)exceptInvalidSolutionErrorasise:print("Bad Solution:",ise)exceptSolutionEncodingErrorassee:print("Bad Solution File:",see)Version History0.0.1-0.0.5: Beta Versions0.1.0: First officially used version.0.1.1: Increased allowed zip size.