Skip to content
Open
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
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
*.d
*.dat

sources/*.o
bin/*
bin/gpx2
bin/opt

*.txt
*.log
Expand All @@ -11,4 +10,4 @@ bin/*
.vscode/

scp/__pycache__/*
obj/*
*.o
23 changes: 23 additions & 0 deletions 2opt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import subprocess

def opt2(tour, problem):
tour = " ".join(map(lambda x: str(x+1), tour))
#print(tour)

result = subprocess.run(
[
"bin/opt",
"-l tsp/",
"-n " + problem
],
input=tour,
capture_output=True,
text=True
)

tour = list(map(lambda x: int(x)-1, result.stdout.split()))
return tour

tour = [13, 48, 30, 5, 9, 12, 50, 6, 22, 49, 36, 15, 43, 45, 31, 20, 24, 25, 0, 39, 3, 8, 46, 18, 21, 32, 10, 2, 33, 40, 34, 44, 17, 1, 37, 42, 19, 28, 47, 16, 7, 4, 27, 11, 29, 35, 38, 14, 51, 23, 41, 26]
tour = opt2(tour, "berlin52")
print(tour)
152 changes: 51 additions & 101 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,105 +1,55 @@
COM_COLOR = \033[0;34m
OBJ_COLOR = \033[0m
OK_COLOR = \033[0;32m
ERROR_COLOR = \033[0;31m
WARN_COLOR = \033[0;33m
NO_COLOR = \033[m

OK_STRING = "[OK]"
ERROR_STRING = "[ERROR]"
WARN_STRING = "[WARNING]"
COM_STRING = "Compiling"


define run_and_test
printf "%b" "$(COM_COLOR) $(COM_STRING) $(OBJ_COLOR) $(@F) $(NO_COLOR)\r"; \
$(1) 2> $@.log; \
RESULT=$$?; \
if [ $$RESULT -ne 0 ]; then \
printf "%-60b%b" "$(COM_COLOR) $(COM_STRING) $(OBJ_COLOR) $@" "$(ERROR_COLOR) $(ERROR_STRING) $(NO_COLOR)\n" ; \
elif [ -s $@.log ]; then \
printf "%-60b%b" "$(COM_COLOR) $(COM_STRING) $(OBJ_COLOR) $@" "$(WARN_COLOR) $(WARN_STRING) $(NO_COLOR)\n" ; \
else \
printf "%-60b%b" "$(COM_COLOR) $(COM_STRING) $(OBJ_COLOR) $(@F)" "$(OK_COLOR) $(OK_STRING) $(NO_COLOR)\n" ; \
fi; \
cat $@.log; \
rm -f $@.log; \
exit $$RESULT
endef


app = GPX2

srcExt = cpp
srcDir = src
objDir = obj
binDir = bin
inc = $(shell find -type f -iname "*.hpp" -printf "%h\n" | sort -u)

debug = 0

CFlags = -Wall -std=gnu++17 -O3
LDFlags =
libs =
libDir =

#************************ DO NOT EDIT BELOW THIS LINE! ****************
ifeq ($(debug),1)
debug=-g
else
debug=
endif

inc := $(addprefix -I,$(inc))
libs := $(addprefix -l,$(libs))
libDir := $(addprefix -L,$(libDir))
CFlags += -c $(debug) $(inc) $(libDir) $(libs)
sources := $(shell find $(srcDir) -name '*.$(srcExt)')
srcDirs := $(shell find . -name '*.$(srcExt)' -exec dirname {} \; | uniq)
objects := $(patsubst %.$(srcExt),$(objDir)/%.o,$(sources))

ifeq ($(srcExt),cpp)
CC = $(CXX)
else
CFlags += -std=gnu99
endif

.phony: all clean cleanbin

all: $(binDir)/$(app)

$(binDir)/$(app): COM_STRING = "Linking"
$(binDir)/$(app): buildrepo $(objects)
@mkdir -p `dirname $@`
@$(call run_and_test, $(CC) $(objects) $(LDFlags) -o $@)

$(objDir)/%.o: COM_STRING = "Compiling"
$(objDir)/%.o: %.$(srcExt)
@$(call make-depend,$<,$@,$(subst .o,.d,$@))
@$(call run_and_test, $(CC) $(CFlags) $< -o $@)
HFILES=\
include/AntColony.hpp \
include/Ant.hpp \
include/Arg.hpp \
include/City.hpp \
include/Constants.hpp \
include/Coordinates.hpp \
include/GAUtils.hpp \
include/Globals.hpp \
include/GPX2Fusion.hpp \
include/GPX2.hpp \
include/GPX2Structs.hpp \
include/GPX2Support.hpp \
include/ImportData.hpp \
include/Log.hpp \
include/Node.hpp \
include/Opt.hpp \
include/OX.hpp \
include/Partition.hpp \
include/Population.hpp \
include/Utils.hpp
OFILES=src/opt/Opt.o \
src/cross/gpx/Partition.o \
src/cross/gpx/Node.o \
src/cross/gpx/GPX2Support.o \
src/cross/gpx/GPX2Fusion.o \
src/cross/gpx/GPX2.o \
src/cross/OX/OX.o \
src/base/Utils.o \
src/base/Population.o \
src/base/Log.o \
src/base/ImportData.o \
src/base/Globals.o \
src/base/GAUtils.o \
src/base/Coordinates.o \
src/base/City.o \
src/args/Arg.o

CXX=g++
LD=g++
CFLAGS=-Iinclude -Wall -O3 -fdata-sections -ffunction-sections -Wl,--gc-sections
O=o

.phony: all clean
all: bin/opt bin/gpx2

clean:
$(RM) -r $(objDir)

cleanbin: clean
$(RM) -r $(binDir)/$(app)

buildrepo:
@$(call make-repo)
@find | grep '\.o$$' | xargs rm -f
@rm -f bin/opt bin/gpx2

define make-repo
for dir in $(srcDirs); \
do \
mkdir -p $(objDir)/$$dir; \
done
endef
%.$O: %.cpp
$(CXX) $(CFLAGS) $(<) -c -o $(@)

# usage: $(call make-depend,source-file,object-file,depend-file)
define make-depend
$(CC) -MM \
-MF $3 \
-MP \
-MT $2 \
$(CFlags) \
$1
endef
bin/%: $(OFILES) bin/%.$(O)
$(CXX) $(CFLAGS) $^ -o $@
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Disclaimer

This README is vastly out of date. You'll need to RTFS

# GPX2 (Generalized Partition Crossover 2) for solving the TSP (Travelling Salesperson Problem)

Repository for the development and testing of the Generalized Partition Crossover 2 (GPX2) algorithm.
Expand Down
130 changes: 130 additions & 0 deletions bin/gpx2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <random>
using std::random_device;

#include <fstream>
using std::ofstream;

#include <sstream>

#include <iostream>
using std::cout;
using std::endl;

#include <chrono>
//chrono

#include <vector>
using std::vector;

#include <stdexcept>
using std::invalid_argument;

#include <string>
using std::stof;
using std::stoi;

#include "Globals.hpp"
#include "Population.hpp"
#include "GAUtils.hpp"
#include "Log.hpp"
#include "Arg.hpp"
#include "ImportData.hpp"
#include "Opt.hpp"
#include "GPX2.hpp"

#include "Constants.hpp"

//begin the genetic algorithm
void GA();

//set arguments
void initArgs(int, char *[]);

//1 -name argumento tour_name REQUIRED
//2 -lib caminho para o .tsp
//3 -size tamanho da pop REQUIRED
//4 -id ID da run(usado para log
//5 -lk porcentagem de população inicial gerado pelo LK
//6 -newpop tipo de geração da nova poop
//7 -nbest n best para serem salvos para a proxima geração
//8 -bestfitness best fitness conhecida
// ex: GA -n berlin52 -l lib/ -s 100 -id 3 -lk 0.1 -np 1 -nb 4 -bf 255
// ex: GA -name berlin52 -size 100

#include "OX.hpp"

int main(int argc, char *argv[]) {
random_device rng;
Globals::urng.seed(rng());

initArgs(argc,argv);

ImportData dataFile(Config::LIB_PATH+Config::NAME);
Globals::map.setCityList(dataFile.getCitiesCoord());

// cout << "scanning" << endl;
std::string line;
std::vector<int> tour1;
std::vector<int> tour2;
int tmp;

std::getline(std::cin, line);
std::istringstream iss1(line);
std::getline(std::cin, line);
std::istringstream iss2(line);

// cout << "pushing" << endl;
while (iss1 >> tmp) {
tour1.push_back(tmp);
}
while (iss2 >> tmp) {
tour2.push_back(tmp);
}
// cout << "optimizing" << endl;
std::vector<int> res = GPX2::crossover(tour1, tour2);
for (auto i : res)
cout << i << " ";
cout << std::endl;

return 0;
}

void initArgs(int argc, char *argv[]){
string NAME = "name|n";
// string SIZE = "size|s";
string LIB = "lib|l";
// string ID = "id";
// string LK = "lk";
// string NEW_POP = "newpop|np";
// string N_BEST = "nbest|nb";
// string BEST_FITNESS = "bestfitness|bf";
// string RESET = "reset|r";
// string GEN_NEW_TOUR = "newtour|nt";

Arg arg(argc,argv);
arg.setProgramName("GPX2");
arg.setHelp();

arg.newArgument(NAME,true,"name of the tour.");
// arg.newArgument(SIZE,true,"size of the pop.");
arg.newArgument(LIB,false,"path to the .tsp file.");
// arg.newArgument(ID,false,"numeric id of the run.");
// arg.newArgument(LK,false,"percentage of population to be generated using linkern.");
// arg.newArgument(NEW_POP,false,"method to be used to generate the next generation.");
// arg.newArgument(N_BEST,false,"number of tours to be saved to the next generation.");
// arg.newArgument(BEST_FITNESS,false,"best fitness found to this tour.");
// arg.newArgument(RESET,false,"percentage of the population to reset each generation");
// arg.newArgument(GEN_NEW_TOUR,false,"mode to generate new tour, used to generate first population and reset");

try{
arg.validateArguments();
}catch(std::runtime_error &e){
std::cout<<e.what()<<endl;
exit(0);
}


Config::NAME = arg.getOption(NAME);
string tmp = arg.getOption(LIB);
Config::LIB_PATH = tmp.empty()?Config::LIB_PATH:tmp;
}
Loading