init commit
This commit is contained in:
37
.clang-format
Normal file
37
.clang-format
Normal file
@@ -0,0 +1,37 @@
|
||||
Language: Cpp
|
||||
BasedOnStyle: LLVM
|
||||
|
||||
AlignAfterOpenBracket: DontAlign
|
||||
BreakConstructorInitializers: AfterColon
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
PointerAlignment: Left
|
||||
SortIncludes: true
|
||||
SpacesBeforeTrailingComments: 2
|
||||
UseTab: Always
|
||||
MaxEmptyLinesToKeep: 5
|
||||
|
||||
TabWidth: 4
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
ContinuationIndentWidth: 4
|
||||
IndentWidth: 4
|
||||
IndentCaseLabels: true
|
||||
|
||||
ColumnLimit: 135
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
|
||||
AllowShortFunctionsOnASingleLine: Empty
|
||||
BreakBeforeBraces: Custom
|
||||
BraceWrapping:
|
||||
AfterClass: false
|
||||
AfterControlStatement: false
|
||||
AfterEnum: false
|
||||
AfterExternBlock: false
|
||||
AfterFunction: false
|
||||
AfterNamespace: false
|
||||
AfterStruct: false
|
||||
AfterUnion: false
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
SplitEmptyRecord: true
|
||||
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
# Xmake cache
|
||||
.xmake/
|
||||
build/
|
||||
|
||||
# MacOS Cache
|
||||
.DS_Store
|
||||
|
||||
|
||||
.vscode
|
||||
86447
RhoneCities_100.flpb
Normal file
86447
RhoneCities_100.flpb
Normal file
File diff suppressed because it is too large
Load Diff
26
src/Model.h
Normal file
26
src/Model.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
struct Facility
|
||||
{
|
||||
int m_Capacity;
|
||||
int m_Cost;
|
||||
float m_X;
|
||||
float m_Y;
|
||||
};
|
||||
|
||||
struct Customer
|
||||
{
|
||||
int m_Demand;
|
||||
float m_X;
|
||||
float m_Y;
|
||||
};
|
||||
|
||||
|
||||
struct Cost
|
||||
{
|
||||
Customer m_Customer;
|
||||
Facility m_Facility;
|
||||
int m_Cost;
|
||||
};
|
||||
65
src/Parser.cpp
Normal file
65
src/Parser.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "Parser.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
FileData ParseFile(const std::string& a_FileName) {
|
||||
std::ifstream in(a_FileName);
|
||||
if (!in) {
|
||||
std::cerr << "Impossible d'ouvrir " << a_FileName << "!\n";
|
||||
return {};
|
||||
}
|
||||
|
||||
FileData fileData;
|
||||
|
||||
std::string line;
|
||||
while (getline(in, line))
|
||||
{
|
||||
if (line == "FACILITIES [capacity cost x y]") {
|
||||
std::cout << "Reading facilities ...\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// facilities
|
||||
while (getline(in, line))
|
||||
{
|
||||
if (line == "CUSTOMERS [demand x y]") {
|
||||
std::cout << "Reading customers ...\n";
|
||||
break;
|
||||
}
|
||||
Facility facility;
|
||||
std::stringstream stream(line);
|
||||
stream >> facility.m_Capacity >> facility.m_Cost >> facility.m_X >> facility.m_Y;
|
||||
fileData.m_Facilities.push_back(facility);
|
||||
}
|
||||
|
||||
// customers
|
||||
while (getline(in, line))
|
||||
{
|
||||
if (line == "COSTS CUSTOMERS/FACILITIES [id_cus id_fac cost]") {
|
||||
std::cout << "Reading costs ...\n";
|
||||
break;
|
||||
}
|
||||
|
||||
Customer customer;
|
||||
std::stringstream stream(line);
|
||||
stream >> customer.m_Demand >> customer.m_X >> customer.m_Y;
|
||||
fileData.m_Customers.push_back(customer);
|
||||
}
|
||||
|
||||
// costs
|
||||
while (getline(in, line))
|
||||
{
|
||||
Cost cost;
|
||||
int facilityID, customerID;
|
||||
std::stringstream stream(line);
|
||||
stream >> customerID >> facilityID >> cost.m_Cost;
|
||||
cost.m_Customer = fileData.m_Customers[customerID - 1];
|
||||
cost.m_Facility = fileData.m_Facilities[facilityID - 1];
|
||||
fileData.m_Costs.push_back(cost);
|
||||
}
|
||||
|
||||
return fileData;
|
||||
}
|
||||
13
src/Parser.h
Normal file
13
src/Parser.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "Model.h"
|
||||
#include <vector>
|
||||
|
||||
struct FileData
|
||||
{
|
||||
std::vector<Facility> m_Facilities;
|
||||
std::vector<Customer> m_Customers;
|
||||
std::vector<Cost> m_Costs;
|
||||
};
|
||||
|
||||
FileData ParseFile(const std::string& a_FileName);
|
||||
56
src/main.cpp
Normal file
56
src/main.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "Parser.h"
|
||||
#include <fmt/format.h>
|
||||
#include <iostream>
|
||||
#include <kiwi/kiwi.h>
|
||||
|
||||
using namespace kiwi;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
auto fileData = ParseFile("RhoneCities_100.flpb");
|
||||
|
||||
std::vector<Variable> y;
|
||||
std::vector<std::vector<Variable>> x(fileData.m_Facilities.size());
|
||||
|
||||
std::cout << "Adding variables ...\n";
|
||||
Solver solver;
|
||||
|
||||
for (std::size_t j = 0; j < fileData.m_Facilities.size(); j++) {
|
||||
y.emplace_back(fmt::format("y_{}", j));
|
||||
for (std::size_t i = 0; i < fileData.m_Customers.size(); i++) {
|
||||
x[i].emplace_back(fmt::format("x_{}{}", i, j));
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Adding constraints 1...\n";
|
||||
for (std::size_t j = 0; j < fileData.m_Facilities.size(); j++) {
|
||||
Expression sum;
|
||||
|
||||
for (std::size_t i = 0; i < fileData.m_Customers.size(); i++) {
|
||||
sum = sum + fileData.m_Customers[i].m_Demand * x[i][j];
|
||||
}
|
||||
|
||||
solver.addConstraint({sum <= fileData.m_Facilities[j].m_Capacity * y[j]});
|
||||
}
|
||||
|
||||
std::cout << "Adding constraints 2...\n";
|
||||
for (std::size_t i = 0; i < fileData.m_Customers.size(); i++) {
|
||||
Expression sum;
|
||||
|
||||
for (std::size_t j = 0; j < fileData.m_Facilities.size(); j++) {
|
||||
sum = sum + x[i][j];
|
||||
}
|
||||
|
||||
solver.addConstraint({sum == 1});
|
||||
}
|
||||
|
||||
std::cout << "Updating variables...\n";
|
||||
solver.updateVariables();
|
||||
|
||||
for (std::size_t j = 0; j < fileData.m_Facilities.size(); j++)
|
||||
{
|
||||
std::cout << y[j].name() << "=" << y[j].value() << "\n";
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
81
xmake.lua
Normal file
81
xmake.lua
Normal file
@@ -0,0 +1,81 @@
|
||||
add_rules("mode.debug", "mode.release")
|
||||
|
||||
add_requires("kiwisolver", "fmt")
|
||||
|
||||
-- https://github.com/Qix-/kiwi/blob/int-solver/kiwi/variable.h
|
||||
|
||||
target("RhoneCities")
|
||||
set_kind("binary")
|
||||
add_packages("kiwisolver", "fmt")
|
||||
add_files("src/*.cpp")
|
||||
set_rundir(".")
|
||||
|
||||
--
|
||||
-- If you want to known more usage about xmake, please see https://xmake.io
|
||||
--
|
||||
-- ## FAQ
|
||||
--
|
||||
-- You can enter the project directory firstly before building project.
|
||||
--
|
||||
-- $ cd projectdir
|
||||
--
|
||||
-- 1. How to build project?
|
||||
--
|
||||
-- $ xmake
|
||||
--
|
||||
-- 2. How to configure project?
|
||||
--
|
||||
-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
|
||||
--
|
||||
-- 3. Where is the build output directory?
|
||||
--
|
||||
-- The default output directory is `./build` and you can configure the output directory.
|
||||
--
|
||||
-- $ xmake f -o outputdir
|
||||
-- $ xmake
|
||||
--
|
||||
-- 4. How to run and debug target after building project?
|
||||
--
|
||||
-- $ xmake run [targetname]
|
||||
-- $ xmake run -d [targetname]
|
||||
--
|
||||
-- 5. How to install target to the system directory or other output directory?
|
||||
--
|
||||
-- $ xmake install
|
||||
-- $ xmake install -o installdir
|
||||
--
|
||||
-- 6. Add some frequently-used compilation flags in xmake.lua
|
||||
--
|
||||
-- @code
|
||||
-- -- add debug and release modes
|
||||
-- add_rules("mode.debug", "mode.release")
|
||||
--
|
||||
-- -- add macro definition
|
||||
-- add_defines("NDEBUG", "_GNU_SOURCE=1")
|
||||
--
|
||||
-- -- set warning all as error
|
||||
-- set_warnings("all", "error")
|
||||
--
|
||||
-- -- set language: c99, c++11
|
||||
-- set_languages("c99", "c++11")
|
||||
--
|
||||
-- -- set optimization: none, faster, fastest, smallest
|
||||
-- set_optimize("fastest")
|
||||
--
|
||||
-- -- add include search directories
|
||||
-- add_includedirs("/usr/include", "/usr/local/include")
|
||||
--
|
||||
-- -- add link libraries and search directories
|
||||
-- add_links("tbox")
|
||||
-- add_linkdirs("/usr/local/lib", "/usr/lib")
|
||||
--
|
||||
-- -- add system link libraries
|
||||
-- add_syslinks("z", "pthread")
|
||||
--
|
||||
-- -- add compilation and link flags
|
||||
-- add_cxflags("-stdnolib", "-fno-strict-aliasing")
|
||||
-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
|
||||
--
|
||||
-- @endcode
|
||||
--
|
||||
|
||||
Reference in New Issue
Block a user