-
Notifications
You must be signed in to change notification settings - Fork 0
Addition of ACK functionality #6
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
Open
kaszczech
wants to merge
3
commits into
main
Choose a base branch
from
ack
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
|
|
||
|
|
||
| def no_transmission(args): | ||
| action, buffer_state, _, _, no_tx = args | ||
| action, buffer_state, _, _, no_tx, _ = args | ||
| return jax.lax.cond( | ||
| action == Actions.IDLE.value, | ||
| lambda: jax.lax.cond(buffer_state == 0, idle_empty_buffer, idle_full_buffer, args), | ||
|
|
@@ -21,34 +21,34 @@ def idle_empty_buffer(_): | |
|
|
||
|
|
||
| def idle_full_buffer(args): | ||
| _, _, ret_c, _, no_tx = args | ||
| _, _, ret_c, _, no_tx, _ = args | ||
| reward = NO_TX_REWARD | ||
| no_tx = no_tx + 1 | ||
| return reward, ret_c, no_tx | ||
|
|
||
|
|
||
| def no_transmission_short(args): | ||
| _, buffer_state, ret_c, _, no_tx = args | ||
| _, buffer_state, ret_c, _, no_tx, _ = args | ||
| reward = NO_TX_REWARD | ||
| no_tx = jnp.where(buffer_state == 0, 0, no_tx + 1) | ||
| return reward, ret_c, no_tx | ||
|
|
||
|
|
||
| def no_transmission_long(args): | ||
| _, _, ret_c, _, no_tx = args | ||
| _, _, ret_c, _, no_tx, _ = args | ||
| scale = jax.lax.min(1., (no_tx - SAFE_IDLE_PERIOD + 1) / PENALIZED_IDLE_PERIOD) | ||
| reward = scale * NO_TX_PENALTY | ||
| no_tx = no_tx + 1 | ||
| return reward, ret_c, no_tx | ||
|
|
||
|
|
||
| def transmission(args): | ||
| _, _, _, channel_state, _ = args | ||
| _, _, _, channel_state, _, _ = args | ||
| return jax.lax.cond(channel_state == 1, transmission_without_collision, transmission_with_collision, args) | ||
|
|
||
|
|
||
| def transmission_with_collision(args): | ||
| _, _, ret_c, _, _ = args | ||
| _, _, ret_c, _, _, _ = args | ||
| return jax.lax.cond(ret_c < MAX_RETRANSMISSION, retransmission, max_retransmission_collision, args) | ||
|
|
||
|
|
||
|
|
@@ -60,15 +60,15 @@ def max_retransmission_collision(_): | |
|
|
||
|
|
||
| def retransmission(args): | ||
| _, _, ret_c, _, _ = args | ||
| _, _, ret_c, _, _, _ = args | ||
| reward = COLLISION_PENALTY | ||
| ret_c = ret_c + 1 | ||
| no_tx = 0 | ||
| return reward, ret_c, no_tx | ||
|
|
||
|
|
||
| def transmission_without_collision(args): | ||
| _, buffer_state, _, _, _ = args | ||
| _, buffer_state, _, _, _, _ = args | ||
| return jax.lax.cond(buffer_state > 0, successful_transmission, empty_buffer_transmission, args) | ||
|
|
||
|
|
||
|
|
@@ -80,18 +80,61 @@ def empty_buffer_transmission(_): | |
|
|
||
|
|
||
| def successful_transmission(args): | ||
| _, _, ret_c, _, _ = args | ||
| _, _, ret_c, _, _, _ = args | ||
| reward = TX_REWARD / (ret_c + 1) | ||
| ret_c = 0 | ||
| no_tx = 0 | ||
| return reward, ret_c, no_tx | ||
|
|
||
|
|
||
| def process_output_i(buffer_state, new_buffer_state, power_state, channel_state, obs, action, terminal): | ||
| _, _, ret_c, no_tx, _ = obs[-1] | ||
| args = (action, buffer_state, ret_c, channel_state, no_tx) | ||
| def transmission_ack(args): | ||
| _, _, _, channel_state, _, _ = args | ||
| return jax.lax.cond(channel_state == 1, transmission_ack_without_collision, transmission_with_collision, args) | ||
|
|
||
|
|
||
| def transmission_ack_without_collision(args): | ||
| _, _, _, _, _, ack_flag = args | ||
| return jax.lax.cond(ack_flag == 1, successful_ack_transmission, empty_ack_transmission, args) | ||
|
|
||
|
|
||
| def empty_ack_transmission(args): | ||
| _, _, ret_c, _, no_tx, _ = args | ||
| reward = EMPTY_ACK_PENALTY | ||
| ret_c = ret_c | ||
| no_tx = no_tx + 1 | ||
| return reward, ret_c, no_tx | ||
|
|
||
|
|
||
| def successful_ack_transmission(args): | ||
| _, _, ret_c, _, _, _ = args | ||
| reward = ACK_REWARD / (ret_c + 1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nie wiem czy dzielenie |
||
| ret_c = 0 | ||
| no_tx = 0 | ||
| return reward, ret_c, no_tx | ||
|
|
||
|
|
||
| def process_output_i(buffer_state, new_buffer_state, power_state, channel_state, obs, action, terminal, | ||
| transmission_history): | ||
| _, _, ret_c, no_tx, _, my_id = obs[-1] | ||
| mask = (transmission_history[:, TransmissionIndex.DESTINATION.value] == my_id) & ( | ||
| transmission_history[:, TransmissionIndex.ACK.value] == AckState.NOT_SENT.value) | ||
| matching_rows = jnp.nonzero(mask, size=mask.shape[0], fill_value=-1)[0] | ||
| print(matching_rows) | ||
| ack_flag = jnp.where(jnp.all(matching_rows == -1), -1, 1) | ||
| args = (action, buffer_state, ret_c, channel_state, no_tx, ack_flag) | ||
|
|
||
| reward, ret_c, no_tx = jax.lax.cond( | ||
| action == Actions.ACK.value, | ||
| transmission_ack, | ||
| lambda a: jax.lax.cond( | ||
| action == Actions.TX.value, | ||
| transmission, | ||
| no_transmission, | ||
| operand=a | ||
| ), | ||
| operand=args | ||
| ) | ||
|
|
||
| reward, ret_c, no_tx = jax.lax.cond(action == Actions.TX.value, transmission, no_transmission, args) | ||
| reward = jnp.where(terminal, 0., reward) | ||
|
|
||
| channel_state = jnp.where(action == Actions.CS.value, channel_state, -1) | ||
|
|
@@ -101,18 +144,24 @@ def process_output_i(buffer_state, new_buffer_state, power_state, channel_state, | |
| action == Actions.CS.value, power_state - CS_CONSUMPTION, | ||
| jnp.where( | ||
| action == Actions.IDLE.value, power_state - IDLE_CONSUMPTION, | ||
| power_state | ||
| jnp.where( | ||
| action == Actions.ACK.value, power_state - ACK_CONSUMPTION, | ||
| power_state | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| obs_t = jnp.array([new_buffer_state, channel_state, ret_c, no_tx, power]) | ||
| obs_t = jnp.array([new_buffer_state, channel_state, ret_c, no_tx, power, my_id]) | ||
| obs = jnp.roll(obs, -1, axis=0) | ||
| obs = obs.at[-1].set(obs_t) | ||
|
|
||
| return obs, reward, power | ||
|
|
||
|
|
||
| def process_output(buffer_states, new_buffer_states, power_states, channel_state, obs, actions, terminals): | ||
| def process_output(buffer_states, new_buffer_states, power_states, channel_state, obs, actions, terminals, transmission_history): | ||
| channel_states = jnp.full(buffer_states.shape[0], channel_state) | ||
| return jax.vmap(process_output_i)(buffer_states, new_buffer_states, power_states, channel_states, obs, actions, terminals) | ||
| return jax.vmap(process_output_i, in_axes=(0, 0, 0, 0, 0, 0, 0, None))(buffer_states, new_buffer_states, | ||
| power_states, channel_states, obs, actions, | ||
| terminals, transmission_history | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.