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

refactor: tsp plugin #225

Merged
merged 6 commits into from
Jul 2, 2024
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
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ server/algorithms/src/**/plugins/**/*
.DS_Store

# or-tools
or-tools/**/*
!or-tools/tsp
!or-tools/install.sh
or-tools/*
!or-tools/src
or-tools.tar.gz
32 changes: 17 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18-alpine as client
FROM node:22-alpine as client
WORKDIR /app
COPY ./client .
RUN yarn install
Expand All @@ -11,22 +11,24 @@ COPY ./server .
RUN apt-get update && apt-get install -y
RUN cargo install --path . --locked

FROM debian:bullseye-slim
FROM debian:bullseye-slim as or-tools
RUN mkdir -p /algorithms/src/routing/plugins
RUN apt-get update && apt-get install -y
RUN apt install -y build-essential cmake lsb-release
RUN apt install -y curl build-essential cmake lsb-release
RUN ldconfig
COPY ./or-tools/src ./src
RUN curl -L https://github.com/google/or-tools/releases/download/v9.10/or-tools_amd64_debian-11_cpp_v9.10.4067.tar.gz -o ortools.tar.gz
RUN cat ortools.tar.gz | tar -xzf - && \
mv or-tools_* or-tools && \
cd or-tools && \
mv /src/tsp/ ./examples/koji_tsp && \
make build SOURCE=examples/koji_tsp/koji_tsp.cc && \
mv ./examples/koji_tsp/build/bin/koji_tsp /algorithms/src/routing/plugins/tsp

FROM debian:bullseye-slim as runner
COPY --from=or-tools /algorithms ./algorithms
COPY --from=or-tools /or-tools ./or-tools
COPY --from=client /app/dist ./dist
COPY --from=server /usr/local/cargo/bin/koji /usr/local/bin/koji
RUN apt install curl -y
RUN apt install -y build-essential cmake lsb-release
RUN mkdir -p /algorithms/src/routing/plugins
COPY ./or-tools .
RUN curl -L https://github.com/google/or-tools/releases/download/v9.5/or-tools_amd64_debian-11_cpp_v9.5.2237.tar.gz -o ortools.tar.gz
RUN cat ortools.tar.gz | tar -xzf - && \
cd or-tools_* && \
mkdir examples/koji && \
cp /tsp/tsp.cc ./examples/koji/koji.cc && \
cp /tsp/CMakeLists.txt ./examples/koji/CMakeLists.txt && \
make build SOURCE=examples/koji/koji.cc && \
mv ./examples/koji/build/bin/koji /algorithms/src/routing/plugins/tsp

CMD koji
28 changes: 18 additions & 10 deletions or-tools/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,48 @@

os=$OSTYPE
cpu=$(uname -p)
minor_version="9.10"
patch_version="4067"
full_version="${minor_version}.${patch_version}"

echo Installing or-tools v${full_version}

if [[ $PWD != *"or-tools"* ]]; then
cd ./or-tools
fi

echo Found $os $cpu

if [ ! -d "source" ]; then
if [ ! -d "${full_version}" ]; then
url=""
if [[ "$os" == "linux-gnu"* ]]; then
distro=$(cat /etc/os-release | grep "^ID=" | cut -d '=' -f 2)
version=$(cat /etc/os-release | grep "^VERSION_ID=" | cut -d '=' -f 2)
distro=${distro//\"/}
version=${version//\"/}
echo Found distro $distro $version
url="https://github.com/google/or-tools/releases/download/v9.5/or-tools_amd64_${distro}-${version}_cpp_v9.5.2237.tar.gz"
url="https://github.com/google/or-tools/releases/download/v${minor_version}/or-tools_amd64_${distro}-${version}_cpp_v${full_version}.tar.gz"
elif [[ "$os" == "darwin"* ]]; then
if [[ "$cpu" == "arm" ]]; then
cpu="arm64"
fi
url="https://github.com/google/or-tools/releases/download/v9.5/or-tools_${cpu}_macOS-13.0.1_cpp_v9.5.2237.tar.gz"
url="https://github.com/google/or-tools/releases/download/v${minor_version}/or-tools_${cpu}_macOS-14.4.1_cpp_v${full_version}.tar.gz"
else
echo "Unsupported OS, contact the developer"
exit 1
fi
echo Downloading $url
curl -L $url -o ortools.tar.gz
tar -xzf ortools.tar.gz
mv ./or-tools_* ./source
mv ./or-tools_* ./${full_version}
rm ortools.tar.gz
fi
cd source
mkdir examples/koji
cp ../tsp/tsp.cc ./examples/koji/koji.cc
cp ../tsp/CMakeLists.txt ./examples/koji/CMakeLists.txt
make build SOURCE=examples/koji/koji.cc
mv ./examples/koji/build/bin/koji ../../server/algorithms/src/routing/plugins/tsp

# check if folder exists and remove if so
if [ -d "./${full_version}/examples/koji_tsp" ]; then
rm -r ./${full_version}/examples/koji_tsp
fi
cp -r ./src/tsp ./${full_version}/examples/koji_tsp
cd $full_version
make build SOURCE=examples/koji_tsp
mv ./examples/koji_tsp/build/bin/koji_tsp ../../server/algorithms/src/routing/plugins/tsp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ cmake_minimum_required(VERSION 3.18)

option(CMAKE_EXPORT_COMPILE_COMMANDS "Export compile command" OFF)

project(koji VERSION 1.0 LANGUAGES CXX)
project(koji_tsp VERSION 1.0 LANGUAGES CXX)
message(STATUS "${PROJECT_NAME} version: ${PROJECT_VERSION}")
#message(STATUS "major: ${PROJECT_VERSION_MAJOR}")
#message(STATUS "minor: ${PROJECT_VERSION_MINOR}")
#message(STATUS "patch: ${PROJECT_VERSION_PATCH}")

if(MSVC)
set(CMAKE_CXX_STANDARD 20)
Expand Down Expand Up @@ -68,7 +65,7 @@ endif()

find_package(ortools REQUIRED CONFIG)

add_executable(${PROJECT_NAME} koji.cc)
add_executable(${PROJECT_NAME} koji_tsp.cc memory_limit.cc)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
#target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
target_link_libraries(${PROJECT_NAME} PRIVATE ortools::ortools)
Expand Down
7 changes: 6 additions & 1 deletion or-tools/tsp/tsp.cc → or-tools/src/tsp/koji_tsp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "ortools/constraint_solver/routing_index_manager.h"
#include "ortools/constraint_solver/routing_parameters.h"

#include "memory_limit.h"

typedef std::vector<std::vector<int64_t>> DistanceMatrix;
typedef std::vector<std::vector<double>> RawInput;
typedef std::vector<int> RawOutput;
Expand Down Expand Up @@ -168,6 +170,9 @@ std::vector<std::string> split(const std::string &s, char delimiter)

int main(int argc, char *argv[])
{
set_memory_limit();
// LOG(INFO) << "Memory Set To: " << mem_limit;

std::map<std::string, std::string> args;
RawInput points;
std::vector<std::string> stringPoints;
Expand Down Expand Up @@ -207,4 +212,4 @@ int main(int argc, char *argv[])
}

return EXIT_SUCCESS;
}
}
65 changes: 65 additions & 0 deletions or-tools/src/tsp/memory_limit.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "memory_limit.h"
#include <iostream>
#include <cstdlib>

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#else
#include <sys/resource.h>
#if defined(__APPLE__)
#include <sys/sysctl.h>
#else
#include <sys/sysinfo.h>
#endif
#endif

unsigned long get_total_memory()
{
#if defined(_WIN32) || defined(_WIN64)
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
return status.ullTotalPhys;
#elif defined(__APPLE__)
int mib[2] = {CTL_HW, HW_MEMSIZE};
unsigned long total_memory;
size_t length = sizeof(total_memory);
if (sysctl(mib, 2, &total_memory, &length, NULL, 0) != 0)
{
std::cerr << "Failed to get system memory size" << std::endl;
}
return total_memory;
#else // Linux
struct sysinfo info;
if (sysinfo(&info) != 0)
{
std::cerr << "Failed to get system info" << std::endl;
}
return info.totalram * info.mem_unit;
#endif
}

void set_memory_limit()
{
unsigned long total_memory = get_total_memory();
unsigned long limit_50_percent = total_memory / 2;
unsigned long max_limit = 20UL * 1024 * 1024 * 1024;
unsigned long memory_limit = std::min(max_limit, limit_50_percent);

#if defined(_WIN32) || defined(_WIN64)
SIZE_T minimumWorkingSetSize = memory_limit;
SIZE_T maximumWorkingSetSize = memory_limit;
if (!SetProcessWorkingSetSize(GetCurrentProcess(), minimumWorkingSetSize, maximumWorkingSetSize))
{
std::cerr << "Failed to set memory limit" << std::endl;
}
#else
struct rlimit rl;
rl.rlim_cur = memory_limit;
rl.rlim_max = memory_limit;
if (setrlimit(RLIMIT_AS, &rl) != 0)
{
std::cerr << "Failed to set memory limit" << std::endl;
}
#endif
}
7 changes: 7 additions & 0 deletions or-tools/src/tsp/memory_limit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef MEMORY_LIMIT_H
#define MEMORY_LIMIT_H

unsigned long get_total_memory();
void set_memory_limit();

#endif // MEMORY_LIMIT_H
Loading