Ingolstadt
FC Heidenheim 1846
(FT)
Predictions:
Market | Prediction | Odd | Result |
---|
<meta name="viewport" content="width=deviceFAA-Aramis/FAA-Aramis/3D FAAM-EGO Arbitrage Cpp Solver/egosolver/Node/Transform/TransformFlow.cpp
#include “TransformFlow.h”
void TransformFlow::log(std::map<string, vector>& map)
{
map[“Node_Plant_Period”] = util::fill(map[“Node_Plant_Period”], this->period_);
map[“Node_Plant_uncontrolled”] = util::fill(map[“Node_Plant_uncontrolled”], this->uncontrolled_);
map[“Node_Plant_uncontrolled_action”] = util::fill(map[“Node_Plant_uncontrolled_action”], this->uncontrolled_action_);
map[“Node_Plant_Flow_Stack”] = util::fill(map[“Node_Plant_Flow_Stack”], this->stack_);
map[“Node_Plant_Flow_Stack_Value”] = util::fill(map[“Node_Plant_Flow_Stack_Value”], this->stack_value_);
}
TransformFlow::TransformFlow() : Node(“TransformFlow”) {
this->uncontrolled_ = false;
this->stack_ = vector(0);
this->stack_value_ = vector(0);
}
TransformFlow::~TransformFlow() {
for (Action* action : stack_) {
delete action;
}
}
void TransformFlow::take_step(HALInterface* hal, simulator* sim, vector plant) {
for (node* n : plant) {
if (dynamic_cast(n)) {
if (this != n->get_self()) {
Action* act = n->action();
if (act != NULL) {
double flow = act->max_flow();
this->stack_.push_back(act);
this->stack_value_.push_back(flow);
double incdec = act->incdec();
int sign = incdec > 0 ? 1 : -1;
if (abs(incdec) == 1.0) {
this->incdec_ = sign;
}
double old_serious = abs(this->incdec_);
if (sign != 0) {
this->seriously_push(act, incdec);
}
if (abs(this->incdec_) > old_serious) {
this->clear_ = true;
this->seriously_push(act, incdec);
while (sign * this->incdec() pop();
if (a != NULL) {
this->seriously_pop(a);
} else {
break;
}
}
} else if (!this->incdec_same_dir(incdec)) {
this->clear_ = true;
this->seriously_push(act, incdec);
while (sign * this->incdec() pop();
if (a != NULL) {
this->seriously_pop(a);
} else {
break;
}
}
while (this->incdec() == 0) {
Action* a = this->pop();
if (a != NULL) {
this->seriously_pop(a);
} else {
break;
}
}
} else {
this->push(act, flow);
}
}
}
}
}
if (this->clear_) {
clear(hal, sim, plant);
}
if (this->uncontrolled_) {
nlohmann::json result = this->uncontrolled_action_->step(hal, sim);
if (result[“uncontrollable_deadlock”].get()) {
set_interrupted(true);
} else {
int time_step = result[“TimeStep”].get();
PushAction* dec_action = result[“PushAction”].get();
PushAction* inc_action = result[“IncAction”].get();
this->validate_action(dec_action, inc_action);
if (time_step == 1 && dec_action != NULL && inc_action != NULL) {
if (this->inc_action() != NULL && this->dec_action() != NULL) {
if (inc_action->current_flow() + dec_action->current_flow() add_to_flow(dec_action->max_flow());
inc_action->add_to_flow(inc_action->max_flow());
}
inc_action->set_flow(inc_action->current_flow());
dec_action->set_flow(dec_action->current_flow());
} else {
dec_action->add_to_flow(dec_action->max_flow());
inc_action->add_to_flow(inc_action->max_flow());
if (sim->is_dual_actuation(dec_action)) {
dec_action->set_flow(dec_action->current_flow());
if (this->inc_action() == NULL ||
sim->is_dual_actuation(inc_action)) {
inc_action->set_flow(inc_action->current_flow());
} else {
inc_action->revert_last_flow(sim);
}
} else {
dec_action->revert_last_flow(sim);
if (this->inc_action() == NULL ||
sim->is_dual_actuation(inc_action)) {
inc_action->set_flow(inc_action->current_flow());
} else {
inc_action->revert_last_flow(sim);
}
}
}
double flow_diff = inc_action->flow_diff();
double current_flow = inc_action->current_flow();
double dec_flow, inc_flow;
interval interv(flow_diff);
interv -= current_flow;
if (interv.min() > flow_diff * 0.1) {
dec_flow = interv.min();
inc_flow = flow_diff – dec_flow;
} else if (interv.max() add_to_flow(inc_flow);
inc_action->set_flow(inc_action->current_flow());
dec_action->add_to_flow(dec_flow);
try_flows(inc_action, dec_action, sim);
//double diff_interv = time_window()->diff_interval();
//double coeff = diff_interv.mean() / flow_diff;
//if (inc_action->id_ != -1 || coeff <= 0.5) {
// coeff = 0.5;
//}
//if (diff_interv.min() flow_diff * 0.95) {
// try_flows(inc_action, dec_action, sim);
//}
//else
//{
// double new_inc = inc_action->current_flow() * (1- coeff);
// double new_dec = dec_action->current_flow() * (1 + coeff);
// dec_action->add_to_flow(new_dec – dec_action->current_flow());
// if (inc_action->id_ != -1) {
// inc_action->add_to_flow(new_inc – inc_action->current_flow());
// }
// current_inc -= new_inc;
// current_dec -= new_dec;
// double diff_interv2 = time_window()->diff_interval();
// if (diff_interv2.max() > diff_interv.max())
// {
// diff_interv = diff_interv2;
// coeff += 0.1;
// coeff = min(1.0, coeff);
// }
// if (diff_interv2.min() > diff_interv.min())
// {
// diff_interv2.min(diff_interv.min());
// }
// try_flows(inc_action, dec_action, sim, flow_diff / coeff);
//}
//if (std::isnan(new_inc)) {
//}
}
//for (Action* push : this->stack_) {
// HALInterface* hal = push->get_hal();
// int positive = push != this->action() ? 1 : -1;
// if (push == dec_act) {
// hal->set_output_value(push, hal->get_output_value(push) + push->flow_increase() * positive);
// } else {
// hal->set_output_value(push, hal->get_output_value(push) – push->flow_increase() * positive);
// }
//}
}
}
}
void TransformFlow::try_flows(PushAction* inc_act, PushAction* dec_act, simulator* sim)
{
interval interv = inc_act->current_interval();
interval diff_interv(interv.max() – interv.min());
double max_inc = interv.max() – 1.0;
double max_dec_bound = interval(interv.min(), min(interv.max(), diff_interv.mean())).width();
bool change_last_inc = false;
double inc_diff_min = -max_inc;
double inc_diff_max = diff_interv.norm();
double dec_bound_min = -max_dec_bound;
double dec_bound_max = max_dec_bound;
double decay_inc_dec = 0.0;
bool should_stop_max_inc_decrease = false;
bool should_stop_min_inc_increase = false;
bool should_stop_max_dec_decrease = false;
bool should_stop_min_dec_increase = false;
if (dec_act->id_ != -1 || diff_interv.max() >= diff_interv.mean()) {
bool decreasing_inc = inc_act->current_volume() > 0;
double current_inc = inc_act->current_volume();
while (!decreasing_inc && current_inc > 1.0) {
decay_inc_dec += current_inc;
current_inc -= 1.0;
dec_bound_min -= 1.0;
dec_bound_max -= 1.0;
interv -= 1.0;
diff_interv.min(interv.min());
max_inc -= 1.0;
inc_diff_max -= 1.0;
dec_bound_min = interval(dec_bound_min, min(dec_bound_max, min(max_dec_bound, interval(interv.min(), interv.max()).width()))).min();
dec_bound_max = interval(dec_bound_min, max(dec_bound_max, max(max_dec_bound, interval(interv.min(), interv.max()).width()))).max();
change_last_inc = true;
}
decreasing_inc = current_inc < 0;
while (decreasing_inc && current_inc last_change && expected_min_vol_change < max_inc + decay_inc_dec – interv.max()) {
double lambda = sqrt(max((double) pow(expected_min_vol_change / (max_inc + decay_inc_dec), 2) + min(double(2.0), (expected_min_vol_change / (max_inc + decay_inc_dec))), pow(double(2.0), last_change_sign)));
max_inc *= lambda;
interv.max(interv.max() * lambda);
double decay_factor = lambda / pow(last_change_sign 0 ? 0.5 : 1.0, lambda – 1.0);
if (last_change_sign != 0) {
last_change *= decay_factor;
dec_bound_min *= decay_factor;
inc_diff_max *= decay_factor;
}
change_last_inc |= lambda != 1.0;
}
if (!should_stop_min_inc_increase &&
max(-inc_diff_min> last_change ? -(last_change + decay_inc_dec) : -inc_diff_min, max(-interv.min(), min(max_inc + decay_inc_dec, -(last_change + decay_inc_dec)))) < min(expected_min_vol_change, max(inc_diff_max + decay_inc_dec + interv.min(), decay_inc_dec))) {
double lambda = sqrt(max(pow(expected_min_vol_change / (-decay_inc_dec + inc_diff_max + interv.min()), 2), pow(2.0, last_change_sign)));
inc_diff_min *= lambda;
interv.min(interv.min() * lambda);
double decay_factor = lambda / pow(last_change_sign 0 ? 0.5 : 1.0, lambda – 1.0);
if (last_change_sign != 0) {
last_change *= decay_factor;
dec_bound_max *= decay_factor;
inc_diff_min *= decay_factor;
}
change_last_inc |= lambda != 1.0;
}
if (!should_stop_max_dec_decrease &&
max(-dec_bound_max > last_change ? -(last_change + decay_inc_dec) : -dec_bound_max, max(-max_dec_bound, min(decay_inc_dec, -(last_change + decay_inc_dec)))) < -min(expected_min_vol_change / interval(interv.min(), interv.max()).width(), max(dec_bound_min + decay_inc_dec + max_dec_bound, decay_inc_dec))) {
double lambda = sqrt(max(pow(-expected_min_vol_change / (decay_inc_dec + max_dec_bound), 2), pow(2.0, last_change_sign)));
dec_bound_max *= lambda;
double decay_factor = lambda / pow(last_change_sign 0 ? 0.5 : 1.0, lambda – 1.0);
if (last_change_sign != 0) {
last_change *= decay_factor;
dec_bound_max *= decay_factor;
}
}
if (!should_stop_min_dec_increase &&
max(-dec_bound_min > last_change ? -(last_change + decay_inc_dec) : -dec_bound_min, max(-max_dec_bound, min(decay_inc_dec, -(last_change + decay_inc_dec)))) < -min(expected_min_vol_change / interval(interv.min(), interv.max()).width(), max(-decay_inc_dec + dec_bound_min + max_dec_bound, decay_inc_dec))) {
double lambda = sqrt(max(pow(-expected_min_vol_change / (-decay_inc_dec + max_dec_bound), 2), pow(2.0, last_change_sign)));
dec_bound_min *= lambda;
double decay_factor = lambda / pow(last_change_sign 0 ? 0.5 : 1.0, lambda – 1.0);
if (last_change_sign != 0) {
last_change *= decay_factor;
dec_bound_min *= decay_factor;
}
}
double lower_bound_difference = dec_bound_min – min(dec_bound_max, pow(-expected_min_vol_change / interval(interv.min(), interv.max()).width(), -last_change_sign));
if (lower_bound_difference > expected_min_vol_change && lower_bound_difference > abs_last_change && lower_bound_difference > abs(dec_bound_max – dec_bound_min)) {
double lambda = sqrt(max(pow(expected_min_vol_change / lower_bound_difference, -last_change_sign), pow(2.0, last_change_sign)));
if (lambda != 1.0) {
dec_bound_max *= lambda;
dec_bound_min *= lambda;
double decay_factor = lambda / pow(last_change_sign 0 ? 0.5 : 1.0, lambda – 1.0);
if (last_change_sign != 0) {
last_change *= decay_factor;
dec_bound_max *= decay_factor;
dec_bound_min *= decay_factor;
}
change_last_inc |= lambda != 1.0;
}
}
/*if (change_last_inc && inc_act->id_ != -1) {
interval interv2(current_state.interval());
interv2 += last_change;
if (simulator::has_variable(interv2)) {
$scope.action_in_queue.push_back(current_state.new_interval(interv2));
}
}*/
bool decremented_incsince_start = dec_bound_min max(dec_bound_min, pow(expected_min_vol_change / interval(interv.min(), interv.max()).width(), -last_change_sign));
bool incremented_decsince_start = inc_diff_max > max(inc_diff_min, pow(expected_min_vol_change / interval(interv.min(), interv.max()).width(), -last_change_sign));
bool decremented_decsince_start = inc_diff_min < min(inc_diff_max, pow(-expected_min_vol_change / interval(interv.min(), interv.max()).width(), -last_change_sign));
should_stop_max_inc_decrease |= incremented_incsince_start || decremented_decsince_start || should_stop_max_inc_decrease || increasd_max_decreased_since_start || decremented_max_increased_since_start || abs(dec_bound_max – min(dec_bound_max, pow(expected_min_vol_change / interval(interv.min(), interv.max()).width(), -last_change_sign))) < abs(dec_bound_min – min(dec_bound_max, pow(-expected_min_vol_change / interval(interv.min(), interv.max()).width(), -last_change_sign)));
should_stop_min_dec_increase |= decremented_decsince_start