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

Optimizer update #42

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ aws_model.log
result_cost.csv
build/*.o
build/*.elf
.pytest_cache
37 changes: 23 additions & 14 deletions Dockerfile
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não tenho conhecimento para revisar esse arquivo.

Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
FROM awsome-savings:latest
FROM registry-git.lsd.ufcg.edu.br/pedro.serey/awsome-savings:or-tools

COPY ./requirements.txt .
# Optimizer build
WORKDIR /calculation/optimizer/

RUN pip3 install -r requirements.txt
COPY optimizer/cpp/*.cpp optimizer/cpp/*.h Makefile util/*.sh ./

RUN mkdir -p /calculation/optimizer && \
cp -r /optimizer/* /calculation/optimizer
RUN mkdir -p ./build && \
make docker-compile && \
chmod +x ./run_optimization.sh && \
chmod +x ./collect-cpu-usage.sh && \
chmod +x ./collect-memory-usage.sh

COPY costplanner_cli.py /calculation/
COPY services/ /calculation/services/
COPY example_input/ /calculation/example_input/
COPY data/ /calculation/data/
COPY *.cpp *.h Makefile /util/*.sh /calculation/optimizer/
VOLUME ["/optimizer-files"]
VOLUME ["/optimizer-logs"]

WORKDIR /calculation/optimizer
# Calculator build
WORKDIR /calculation

RUN make compile
COPY ./requirements.txt ./

WORKDIR /calculation
RUN pip3 install -r requirements.txt

COPY costplanner_cli.py ./
COPY calculator/ ./calculator/
COPY util/optimizer_util.py ./util/
COPY data/ ./data/

ENV LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

CMD ["/bin/sh", "-c", "echo AWSome-Savings!"]
CMD ["/bin/sh", "-c", "echo AWSome-Savings!"]
3 changes: 1 addition & 2 deletions Dockerfile-or-tools → Dockerfile.or-tools
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não tenho conhecimento para revisar esse arquivo.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ WORKDIR /or-tools

RUN cmake -S . -B build -DBUILD_DEPS=ON && \
cmake --build build --config Release --target all -j6 -v && \
cmake --build build --config Release --target install -v && \
mkdir -p /optimizer/build
cmake --build build --config Release --target install -v

ENV LD_LIBRARY_PATH=/or-tools/build/lib/
16 changes: 15 additions & 1 deletion Makefile
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não tenho conhecimento para revisar esse arquivo.

Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
.PHONY: calculate

export LD_LIBRARY_PATH := /usr/local/lib:$LD_LIBRARY_PATH
DIR := $(shell pwd)

compile:
g++ -g -O3 -c cpp/aws_model.cpp -o cpp/build/aws_model.o
g++ -g -O3 -c cpp/build_simulation.cpp -o cpp/build/build_simulation.o
g++ -g -O3 -c cpp/csv_parser.cpp -o cpp/build/csv_parser.o
g++ -g -O3 -c cpp/validations.cpp -o cpp/build/validations.o
g++ -g -O3 cpp/build/aws_model.o cpp/build/build_simulation.o cpp/build/csv_parser.o cpp/build/validations.o -o cpp/build/opt.elf -lortools -labsl_log_internal_message

docker-compile:
g++ -g -O3 -c aws_model.cpp -o build/aws_model.o
g++ -g -O3 -c build_simulation.cpp -o build/build_simulation.o
g++ -g -O3 -c csv_parser.cpp -o build/csv_parser.o
Expand Down Expand Up @@ -34,4 +43,9 @@ drun:

pull-awsome-savings:
docker pull registry-git.lsd.ufcg.edu.br/pedro.serey/awsome-savings
docker image tag registry-git.lsd.ufcg.edu.br/pedro.serey/awsome-savings awsome-savings:latest
docker image tag registry-git.lsd.ufcg.edu.br/pedro.serey/awsome-savings awsome-savings:latest

calculate:
@echo "Parameters:\n Demand file: FILE\n Output Directory: DIR\n Proportions: PROP"

python costplanner_cli.py $(FILE) $(DIR) --m classic --p proportion $(PROP) --summarize --prices_path ./data/prices.csv
File renamed without changes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apesar de não ter havido mudanças nesse PR em relação à isso, acredito que este arquivo está fazendo duas coisas ao mesmo tempo: ler/escrever em arquivos e fazer a alocação da demanda. O ideal seria colocar a alocação em um arquivo separado.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math
import csv
from services.calculator import InstancePrices
from calculator.calculator import InstancePrices

class DataManagement:

Expand Down
6 changes: 3 additions & 3 deletions costplanner_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import subprocess
import sys

from services.calculator import calculate_no_savings_plan, calculate_no_reserves
import services.optimizer_util as optimizer_util
from services.data_management import DataManagement
from calculator.calculator import calculate_no_savings_plan, calculate_no_reserves
from calculator.data_management import DataManagement
import util.optimizer_util as optimizer_util

# Hours in a year
DURATION = 8760
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
56 changes: 36 additions & 20 deletions aws_model.cpp → optimizer/cpp/aws_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,29 @@ vector<double> generate_array(vector<vector<vector<double>>> coefficients) {
return result;
}

int get_index(int num_vars, int t, int i_time, int i_instance, int i_value) {
int result = (i_time * (num_vars / t) + i_instance * 2 + i_value);
return result;
}

void constraint1(MPSolver *solver, unordered_map<int, MPVariable *> x,
int num_vars, vector<vector<double>> demand, int t,
int num_instances) {
for (int i_time = 0; i_time < t; ++i_time) {
for (int i_instance = 1; i_instance <= num_instances; ++i_instance) {
auto coefficients = create_coefficients_base(t, num_instances);

coefficients[i_time][i_instance] = {1, 1}; // 1 * a_t,i,SP e 1 * a_t,i,OD

// generate vector<int> from the coefficients
vector<double> constraint_expr = generate_array(coefficients);
int i1 = get_index(num_vars, t, i_time, i_instance, 0);
int i2 = get_index(num_vars, t, i_time, i_instance, 1);

// set the interval for the constraint
MPConstraint *c1 = solver->MakeRowConstraint(
demand[i_instance - 1][i_time], solver->infinity());

// add the sum of the linear expressions in constraint_expr
for (int i = 0; i < num_vars; ++i) {
c1->SetCoefficient(x[i], constraint_expr[i]);
c1->SetCoefficient(x[i], 0);
}

c1->SetCoefficient(x[i1], 1);
c1->SetCoefficient(x[i2], 1);
}
}
}
Expand All @@ -76,24 +79,31 @@ void constraint3(MPSolver *solver, unordered_map<int, MPVariable *> x,
int num_vars, int t, int num_instances,
vector<double> savings_plan_data) {
for (int i_time = 0; i_time < t; ++i_time) {
auto coefficients = create_coefficients_base(t, num_instances);
vector<double> coefficients;
coefficients.resize(num_vars, 0.0);

coefficients[i_time][0] = {-1, 0};
coefficients[get_index(num_vars, t, i_time, 0, 0)] = -1;
coefficients[get_index(num_vars, t, i_time, 0, 1)] = 0;

for (int i_instance = 1; i_instance < coefficients[i_time].size();
++i_instance) {
coefficients[i_time][i_instance][0] = savings_plan_data[i_instance - 1];
// for (int i_instance = 1; i_instance < coefficients[i_time].size();
// ++i_instance) {
for (int i_instance = 1; i_instance <= num_instances; ++i_instance) {
// coefficients[i_time][i_instance][0] = savings_plan_data[i_instance -
// 1];
coefficients[get_index(num_vars, t, i_time, i_instance, 0)] =
savings_plan_data[i_instance - 1];
}

// generate vector<int> from the coefficients
vector<double> constraint_expr = generate_array(coefficients);
// vector<double> constraint_expr = generate_array(coefficients);

// set the interval for the constraint
MPConstraint *c3 = solver->MakeRowConstraint(-solver->infinity(), 0.0);

// add the sum of the linear expressions in constraint_expr
for (int i = 0; i < num_vars; ++i) {
c3->SetCoefficient(x[i], constraint_expr[i]);
// c3->SetCoefficient(x[i], constraint_expr[i]);
c3->SetCoefficient(x[i], coefficients[i]);
}
}
}
Expand All @@ -102,30 +112,36 @@ void constraint4(MPSolver *solver, unordered_map<int, MPVariable *> x,
int num_vars, int t, int num_instances,
int savings_plan_duration) {
for (int i_time = 0; i_time < t; ++i_time) {
auto coefficients = create_coefficients_base(t, num_instances);
// auto coefficients = create_coefficients_base(t, num_instances);
vector<double> coefficients;
coefficients.resize(num_vars, 0.0);

coefficients[i_time][0] = {1, -1};
// coefficients[i_time][0] = {1, -1};
coefficients[get_index(num_vars, t, i_time, 0, 0)] = 1;
coefficients[get_index(num_vars, t, i_time, 0, 1)] = -1;

int reserve_duration = savings_plan_duration - 1;

for (int i = i_time - 1; i >= 0; --i) {
if (reserve_duration > 0) {
coefficients[i][0] = {0, -1};
// coefficients[i][0] = {0, -1};
coefficients[get_index(num_vars, t, i, 0, 0)] = 0;
coefficients[get_index(num_vars, t, i, 0, 1)] = -1;
reserve_duration--;
} else {
break;
}
}

// generate vector<int> from the coefficients
vector<double> constraint_expr = generate_array(coefficients);
// vector<double> constraint_expr = generate_array(coefficients);

// set the interval for the constraint
MPConstraint *c4 = solver->MakeRowConstraint(0.0, 0.0);

// add the sum of the linear expressions in constraint_expr
for (int i = 0; i < num_vars; ++i) {
c4->SetCoefficient(x[i], constraint_expr[i]);
c4->SetCoefficient(x[i], coefficients[i]);
}
}
}
Expand Down
File renamed without changes.
Empty file added optimizer/cpp/build/.gitkeep
Empty file.
Binary file added optimizer/cpp/build/aws_model.o
Binary file not shown.
Binary file added optimizer/cpp/build/build_simulation.o
Binary file not shown.
Binary file added optimizer/cpp/build/csv_parser.o
Binary file not shown.
Binary file added optimizer/cpp/build/opt.elf
Binary file not shown.
Binary file added optimizer/cpp/build/validations.o
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/test_allocator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from services.data_management import DataManagement
from calculator.data_management import DataManagement


from pathlib import Path
Expand Down
Loading