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

✨ Added function to generate random SiDB layouts and write simulation results to file. #200

Merged
merged 141 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 87 commits
Commits
Show all changes
141 commits
Select commit Hold shift + click to select a range
9622e83
:arrow_up: Bump libs/Catch2 from `6783411` to `1f881ab` (#27)
dependabot[bot] Apr 2, 2023
2da4f7e
Merge branch 'marcelwa:main' into main
Drewniok Apr 14, 2023
8f5f52f
:arrow_up: Bump libs/parallel-hashmap from `7883cb6` to `d2bed96` (#33)
dependabot[bot] Apr 14, 2023
a8cbc27
Merge branch 'marcelwa:main' into main
Drewniok Apr 18, 2023
3b11f93
Merge branch 'marcelwa:main' into main
Drewniok Apr 18, 2023
e9e2fa3
Merge branch 'marcelwa:main' into main
Drewniok Apr 18, 2023
37c881e
Merge branch 'marcelwa:main' into main
Drewniok Apr 19, 2023
4aa8489
Merge branch 'marcelwa:main' into main
Drewniok Apr 19, 2023
1587ef3
Merge branch 'marcelwa:main' into main
Drewniok Apr 20, 2023
04ed1c3
Merge branch 'marcelwa:main' into main
Drewniok Apr 21, 2023
9eaaf1f
Merge branch 'marcelwa:main' into main
Drewniok Apr 25, 2023
0dca9cf
Merge branch 'marcelwa:main' into main
Drewniok Apr 26, 2023
ce8b2e2
Merge branch 'marcelwa:main' into main
Drewniok May 1, 2023
cf70745
:sparkles: random layout generator.
Drewniok May 4, 2023
58d5f12
:sparkles: random layout generator.
Drewniok May 4, 2023
8a5efa3
:white_check_mark: add test for random layout generator.
Drewniok May 4, 2023
0109e2b
:white_check_mark: update test.
Drewniok May 4, 2023
5174cc9
:sparkles: experiment file to generate random layouts.
Drewniok May 4, 2023
6e7e8b3
:sparkles: experiment file to simulate the randomly-generated files a…
Drewniok May 4, 2023
0236e84
:memo: add RST.
Drewniok May 4, 2023
25e8b6b
:memo: small change in RST.
Drewniok May 4, 2023
6053844
:memo: remove superfluous semicolon.
Drewniok May 4, 2023
bb2d756
:memo: add missing @param.
Drewniok May 4, 2023
17377a4
:memo: add missing doxygenstruct.
Drewniok May 4, 2023
9859049
:art: add ``const`` keyword.
Drewniok May 4, 2023
f38f6bd
:art: remove ``const`` keyword.
Drewniok May 4, 2023
5bd0e28
:art: small changes.
Drewniok May 4, 2023
d70a8a6
:art: add ``const``.
Drewniok May 4, 2023
dd9ccad
:art: reformat code and clang-tidy suggestions.
Drewniok May 5, 2023
fb44a2d
:art: rename variables.
Drewniok May 8, 2023
64501c1
Merge branch 'marcelwa:main' into main
Drewniok May 10, 2023
3eac8a3
Merge branch 'main' into random_layout_generator
Drewniok May 10, 2023
a9fa94f
Merge branch 'main' into random_layout_generator
Drewniok May 11, 2023
68f6885
Merge branch 'marcelwa:main' into main
Drewniok May 11, 2023
84634cd
Merge branch 'marcelwa:main' into main
Drewniok May 12, 2023
d3f830f
Merge branch 'main' into random_layout_generator
Drewniok May 12, 2023
00d9472
Merge branch 'main' into random_layout_generator
Drewniok May 12, 2023
597b1a7
Merge branch 'marcelwa:main' into main
Drewniok May 12, 2023
9eb04bf
Merge branch 'main' into random_layout_generator
Drewniok May 12, 2023
1920794
Merge branch 'marcelwa:main' into main
Drewniok May 14, 2023
cc8ab8f
:art: change return type of simulation.
Drewniok May 15, 2023
e8c088f
:fire: remove redeclared method.
Drewniok May 15, 2023
c5cfada
Merge branch 'main' into random_layout_generator
Drewniok May 15, 2023
5d74ac6
Merge branch 'marcelwa:main' into main
Drewniok May 21, 2023
f408962
Merge branch 'marcelwa:main' into main
Drewniok May 24, 2023
b4138de
Merge branch 'marcelwa:main' into main
Drewniok May 25, 2023
930fcf2
Merge branch 'marcelwa:main' into main
Drewniok Jun 5, 2023
5916d3e
:art: implement Marcel's suggestions.
Drewniok Jun 7, 2023
cbd8345
Merge branch 'marcelwa:main' into main
Drewniok Jun 7, 2023
98f9638
:sparkles: function to generate a random cell between two given cells.
Drewniok Jun 7, 2023
866f3d5
Merge branch 'main' into random_layout_generator
Drewniok Jun 7, 2023
d99ba9d
:art: add docu.
Drewniok Jun 7, 2023
cd6d946
:fire: remove getter function.
Drewniok Jun 7, 2023
689a2a6
:sparkles: Add function to write the position of the SiDBs in nm to a…
Drewniok Jun 9, 2023
eb5acf0
:white_check_mark: Add test for write_txt_sim_result.cpp.
Drewniok Jun 9, 2023
d4577df
:art: small changes here and there.
Drewniok Jun 9, 2023
6e0909d
:art: remove type information in std::uniform_int_distribution.
Drewniok Jun 9, 2023
e181ade
:memo: Add documentation.
Drewniok Jun 9, 2023
3dc23bd
:memo: Add RST.
Drewniok Jun 9, 2023
5119304
:art: Major change of the functional structure.
Drewniok Jun 9, 2023
41a117f
:memo: change RTS-format of random_layout_generator.hpp.
Drewniok Jun 9, 2023
4ae68fe
:art: correct typo.
Drewniok Jun 9, 2023
226610a
:art: using constexpr const char*.
Drewniok Jun 9, 2023
2903532
Merge branch 'marcelwa:main' into main
Drewniok Jun 12, 2023
9b631d1
Merge branch 'main' into random_cell
Drewniok Jun 12, 2023
2ed5208
:art: implement Marcel's suggestions.
Drewniok Jun 12, 2023
89ff1ee
:art: reformat code.
Drewniok Jun 12, 2023
b39fd60
:art: implement Marcel's suggestions.
Drewniok Jun 13, 2023
a186e39
:art: remove ``const`` and pass parameters by copy.
Drewniok Jun 13, 2023
5b9da4f
:art: implement Marcel's suggestions.
Drewniok Jun 13, 2023
a93dd69
:art: implement Marcel's suggestions.
Drewniok Jun 13, 2023
4d4b946
:art: add siqad exception.
Drewniok Jun 13, 2023
5c4bd39
:art: use more elegant way.
Drewniok Jun 13, 2023
a93813d
:art: change order of if branches.
Drewniok Jun 13, 2023
ef5c52c
Merge branch 'random_cell' into random_layout_generator
Drewniok Jun 13, 2023
51efb49
:art: make use of new function to generate random coordinate.
Drewniok Jun 13, 2023
53c4872
Merge branch 'main' into random_layout_generator
Drewniok Jun 13, 2023
07c32bd
:sparkles: Layouts based on siqad and fiction coordinates can be writ…
Drewniok Jun 14, 2023
f0c133b
:sparkles: add static_assert and swap function.
Drewniok Jun 14, 2023
a37262e
:fire: remove superfluous while.
Drewniok Jun 14, 2023
ccb7bbb
:art: add const keyword.
Drewniok Jun 14, 2023
f70ceb6
Merge branch 'marcelwa:main' into main
Drewniok Jun 14, 2023
24ff3a7
Merge branch 'marcelwa:main' into main
Drewniok Jun 16, 2023
a4f3150
Merge branch 'marcelwa:main' into main
Drewniok Jun 19, 2023
de4962e
:art: add a layout as input to add random cells to this given layout.
Drewniok Jun 19, 2023
04eb35f
Merge branch 'main' into random_layout_generator
Drewniok Jun 19, 2023
6a7df5e
:art: add input layout to experiment file.
Drewniok Jun 19, 2023
9c78984
Merge branch 'marcelwa:main' into main
Drewniok Jun 23, 2023
0f54bfc
Merge branch 'main' into random_layout_generator
Drewniok Jun 26, 2023
c3f4e96
Merge branch 'marcelwa:main' into main
Drewniok Jun 28, 2023
853be74
Merge branch 'cda-tum:main' into main
Drewniok Jul 5, 2023
13eab3c
Merge branch 'cda-tum:main' into main
Drewniok Jul 10, 2023
2194c1a
Merge branch 'main' into random_layout_generator
Drewniok Jul 10, 2023
002fb41
:art: Simulation results for layouts where no physically valid charge…
Drewniok Jul 10, 2023
1c59a58
Merge branch 'cda-tum:main' into main
Drewniok Jul 18, 2023
a18d52c
Merge branch 'main' into random_layout_generator
Drewniok Jul 18, 2023
7354ef0
:art: implement Marcel's suggestions.
Drewniok Jul 18, 2023
b63a348
:white_check_mark: art more decimal places in unit test.
Drewniok Jul 19, 2023
f5eb41d
:construction: make random_layout_generator.cpp unused to find out wh…
Drewniok Jul 19, 2023
a265a7e
:construction: make random_layout_generator.cpp unused to find out wh…
Drewniok Jul 19, 2023
2cfaeb3
:construction: add only one TEST_CASE in random_layout_generator.cpp.
Drewniok Jul 19, 2023
6eea330
:construction: not using tests for ``generate_multiple_random_layouts…
Drewniok Jul 19, 2023
bfd3604
:construction: using a few tests for ``generate_multiple_random_layou…
Drewniok Jul 19, 2023
c2a973e
:construction: using a few more tests for ``generate_multiple_random_…
Drewniok Jul 19, 2023
305fbbb
:construction: using a few more tests for ``generate_multiple_random_…
Drewniok Jul 19, 2023
0b7bd9c
:construction: using a few more tests for ``generate_multiple_random_…
Drewniok Jul 19, 2023
5986de8
:construction: all test are enabled.
Drewniok Jul 19, 2023
9b6ca3f
:construction: change ``CHECK`` statement.
Drewniok Jul 20, 2023
c45c354
:construction: change ``CHECK`` statement.
Drewniok Jul 20, 2023
763a069
:art: change ``CHECK`` statement.
Drewniok Jul 20, 2023
1f87c22
Merge branch 'cda-tum:main' into main
Drewniok Jul 20, 2023
73719cd
Merge branch 'main' into random_layout_generator
Drewniok Jul 20, 2023
b638a34
:construction: not using ``uniqueness check``.
Drewniok Jul 20, 2023
977d5ff
:white_check_mark: remove last unit test.
Drewniok Jul 21, 2023
d300bc1
Merge branch 'cda-tum:main' into main
Drewniok Jul 24, 2023
2dc75ac
Merge branch 'cda-tum:main' into main
Drewniok Jul 25, 2023
9fc5819
Merge branch 'cda-tum:main' into main
Drewniok Jul 26, 2023
bd5cfc3
Merge branch 'cda-tum:main' into main
Drewniok Jul 26, 2023
51b93cb
Merge branch 'cda-tum:main' into main
Drewniok Jul 26, 2023
751369c
Merge branch 'cda-tum:main' into main
Drewniok Aug 3, 2023
311339b
Merge branch 'cda-tum:main' into main
Drewniok Aug 10, 2023
16c12e4
Merge branch 'cda-tum:main' into main
Drewniok Aug 27, 2023
9f4426b
Merge branch 'cda-tum:main' into main
Drewniok Aug 29, 2023
6eccd58
Merge branch 'cda-tum:main' into main
Drewniok Aug 30, 2023
918b755
Merge branch 'cda-tum:main' into main
Drewniok Aug 30, 2023
89bc9ae
Merge branch 'cda-tum:main' into main
Drewniok Sep 1, 2023
11b61ae
Merge branch 'main' into random_layout_generator
Drewniok Sep 4, 2023
eb84e84
:twisted_rightwards_arrows: merge ``main``.
Drewniok Sep 4, 2023
fe94fd5
:twisted_rightwards_arrows: merge ``main``.
Drewniok Sep 4, 2023
8625fc8
:art: implement review suggestions.
Drewniok Sep 4, 2023
b9c531f
:art: implement review suggestions.
Drewniok Sep 4, 2023
86dd321
:bug: wrong Header in sidb_simulation.rst.
Drewniok Sep 4, 2023
4c18389
:art: renaming for documentation.
Drewniok Sep 4, 2023
188929b
:art: renaming for documentation.
Drewniok Sep 4, 2023
3fcd565
:art: reformat code.
Drewniok Sep 4, 2023
654f049
:art: Incorporate the bots' comments.
Drewniok Sep 4, 2023
13444d6
:art: more consistency
Drewniok Sep 5, 2023
8a90cdf
:art: remove static_assert
Drewniok Sep 5, 2023
dd5da41
:art: use ``std::fabs`` to check equality.
Drewniok Sep 5, 2023
64c4862
Merge branch 'cda-tum:main' into main
Drewniok Sep 5, 2023
4746672
Merge branch 'main' into random_layout_generator
Drewniok Sep 5, 2023
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
10 changes: 10 additions & 0 deletions docs/algorithms/sidb_simulation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,13 @@ Time-to-Solution (TTS) Statistics
**Header:** ``fiction/algorithms/simulation/sidb/time_to_solution.hpp``

.. doxygenfunction:: fiction::sim_acc_tts


Random Layout Generator
#######################

**Header:** ``fiction/algorithms/simulation/sidb/random_layout_generator.hpp``

.. doxygenstruct:: fiction::random_layout_params
.. doxygenfunction:: fiction::generate_random_layout
.. doxygenfunction:: fiction::generate_multiple_random_layout
5 changes: 5 additions & 0 deletions docs/io/physical_simulation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ SiQAD
.. doxygenfunction:: fiction::write_sqd_sim_result(const sidb_simulation_result<Lyt>& sim_result, std::ostream& os)
.. doxygenfunction:: fiction::write_sqd_sim_result(const sidb_simulation_result<Lyt>& sim_result, const std::string_view& filename)

**Header:** ``fiction/io/write_txt_sim_result.hpp``

.. doxygenfunction:: fiction::write_txt_sim_result(const sidb_simulation_result<Lyt>& sim_result, std::ostream& os)
.. doxygenfunction:: fiction::write_txt_sim_result(const sidb_simulation_result<Lyt>& sim_result, const std::string_view& filename)

**Header:** ``fiction/io/read_sqd_layout.hpp``

.. doxygenfunction:: fiction::read_sqd_layout(std::istream& is, const std::string_view& name = "")
Expand Down
96 changes: 96 additions & 0 deletions experiments/random_layout_generation/random_layout_generation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Created by Jan Drewniok on 04.05.23.
//

#include <fiction/algorithms/simulation/sidb/random_layout_generator.hpp>
#include <fiction/io/read_sqd_layout.hpp>
#include <fiction/types.hpp>

#include <filesystem>
#include <iomanip>
#include <iostream>
#include <string>
Drewniok marked this conversation as resolved.
Show resolved Hide resolved

using namespace fiction;

int main() // NOLINT
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
// This script generates random layouts as .sqd file.
using cell_level_layout = cell_level_layout<sidb_technology, clocked_layout<cartesian_layout<cube::coord_t>>>;
constexpr const char* folder_name = "random_layouts_test_new/";
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
std::filesystem::path folder_path(EXPERIMENTS_PATH);

folder_path /= folder_name;

if (std::filesystem::exists(folder_path))
{
std::cout << "Folder *" << folder_path << "* exists!" << std::endl;
}
else
{
if (std::filesystem::create_directory(folder_path))
{
std::cout << "Folder *random_layouts* created successfully" << std::endl;
}
else
{
std::cout << "Failed to create folder" << std::endl;
}
}
// number of randomly generated layouts for a given number of placed SiDBs.
const uint64_t number_of_layouts = 10;
// number of SiDBs of the first bunch of randomly generated layouts.
uint64_t number_of_sidbs_lower_limit = 20;
// number of SiDBs of the final bunch of randomly generated layouts.
uint64_t number_of_sidbs_upper_limit = 21;
Drewniok marked this conversation as resolved.
Show resolved Hide resolved

if (number_of_sidbs_upper_limit < number_of_sidbs_lower_limit)
{
std::swap(number_of_sidbs_upper_limit, number_of_sidbs_lower_limit);
}
Drewniok marked this conversation as resolved.
Show resolved Hide resolved

for (uint64_t num_sidbs = number_of_sidbs_lower_limit; num_sidbs < number_of_sidbs_upper_limit; num_sidbs++)
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
{
const std::filesystem::path dir_path = folder_path.string() + "number_sidbs_" + std::to_string(num_sidbs);
const std::filesystem::path dir_path_sqd =
folder_path.string() + "number_sidbs_" + std::to_string(num_sidbs) + "/sqd";
const std::filesystem::path dir_path_loc =
folder_path.string() + "number_sidbs_" + std::to_string(num_sidbs) + "/loc";
try
{
if (!std::filesystem::exists(dir_path))
{
std::filesystem::create_directory(dir_path);
std::cout << "Folder created." << std::endl;
std::filesystem::create_directory(dir_path_sqd);
std::filesystem::create_directory(dir_path_loc);
}
else
{
std::cout << "Folder already exists." << std::endl;
}

const random_layout_params<cell_level_layout> params{{{0, 0}, {20, 20}}, num_sidbs, false};
Drewniok marked this conversation as resolved.
Show resolved Hide resolved

const auto unique_lyts =
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
generate_multiple_random_layout<cell_level_layout>(params, cell_level_layout{}, number_of_layouts);
for (auto i = 0u; i < unique_lyts.size(); i++)
{
write_sqd_layout(unique_lyts[i], dir_path_sqd.string() + "/layout_" + std::to_string(i) + ".sqd");
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
}
}
catch (const std::filesystem::filesystem_error& ex)
{
// Exception is handled.
std::cerr << "Filesystem error: " << ex.what() << std::endl;
}
}
}
catch (const std::filesystem::filesystem_error& ex)
{
// Exception occurred, handle it here
std::cerr << "Filesystem error: " << ex.what() << std::endl;
}
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
}
80 changes: 80 additions & 0 deletions experiments/random_layout_generation/simulation_result_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// Created by Jan Drewniok on 04.05.23.
//

#include "fiction/algorithms/simulation/sidb/exhaustive_ground_state_simulation.hpp"
#include "fiction/io/read_sqd_layout.hpp"
#include "fiction/io/write_txt_sim_result.hpp"
#include "fiction/technology/charge_distribution_surface.hpp"
#include "fiction/types.hpp"

#include <cstdlib>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
Drewniok marked this conversation as resolved.
Show resolved Hide resolved

using namespace fiction;

int main() // NOLINT
{
// This script uses the randomly generated layouts (hence, random_layout_generation.cpp should be executed first),
// simulates them, and collects the simulation results as a text file. The text file has three columns: x,y, charge
// state (as integer) of the ground state.
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
try
{
constexpr const char* folder_name = "random_layouts_test_new/";
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
std::filesystem::path folder_path(EXPERIMENTS_PATH);
folder_path /= folder_name;

if (std::filesystem::exists(folder_path))
{
for (const auto& folder : std::filesystem::directory_iterator(folder_path))
{
if (std::filesystem::exists(folder_path))
{
for (const auto& file : std::filesystem::directory_iterator(folder.path().string() + "/sqd"))
{
const auto& benchmark = file.path();

// When random layouts are generated with the script random_layout_generation.cpp, they have the
// following file name format: layout_xxx (xxx is the numbering). In the following code lines,
// the file name of the current file is extracted from the file path.
const std::string path = benchmark.string();
const uint64_t start = path.rfind("layout") + 7;
const uint64_t end = path.rfind(".sqd") - 1;
const std::string name = path.substr(start, end - start + 1);

std::cout << benchmark << std::endl;

auto lyt = read_sqd_layout<sidb_cell_clk_lyt_siqad>(benchmark.string());

const sidb_simulation_parameters params{2, -0.32};
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
const auto simulation_results =
exhaustive_ground_state_simulation<sidb_cell_clk_lyt_siqad>(lyt, params);
std::stringstream ss;
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
ss << std::fixed << std::setprecision(3) << -params.mu;
std::string const file_path =
folder.path().string() + "/loc/" + name + "_sim_µ_minus_" + ss.str() + ".txt";
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
write_txt_sim_result(simulation_results, file_path);
}
}
else
{
std::cout << "Folder" + std::string("/sqd") + "does not exist." << std::endl;
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
else
{
std::cout << "Folder" + std::string(folder_name) + "does not exist." << std::endl;
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
}
}
catch (const std::filesystem::filesystem_error& ex)
{
std::cerr << "Error accessing folder: " << ex.what() << std::endl;
}

return EXIT_SUCCESS;
}
178 changes: 178 additions & 0 deletions include/fiction/algorithms/simulation/sidb/random_layout_generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
//
// Created by Jan Drewniok on 05.04.23.
//

#ifndef FICTION_RANDOM_LAYOUT_GENERATOR_HPP
#define FICTION_RANDOM_LAYOUT_GENERATOR_HPP

#include "fiction/algorithms/path_finding/distance.hpp"
#include "fiction/io/write_sqd_layout.hpp"
#include "fiction/layouts/cell_level_layout.hpp"
#include "fiction/technology/cell_technologies.hpp"
#include "fiction/technology/charge_distribution_surface.hpp"
#include "fiction/technology/sidb_nm_position.hpp"
#include "fiction/traits.hpp"
#include "fiction/types.hpp"
#include "fiction/utils/hash.hpp"
#include "fiction/utils/layout_utils.hpp"

#include <cstdint>
#include <iostream>
#include <random>
#include <string_view>
#include <unordered_set>
#include <vector>
Drewniok marked this conversation as resolved.
Show resolved Hide resolved

namespace fiction
{
/**
* This struct stores the parameters for the *generate_random_layout* algorithm.
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
*/
template <typename Lyt>
struct random_layout_params
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* Two coordinates that span the region where SiDBs may be placed (order is not important).
*/
std::pair<typename Lyt::cell, typename Lyt::cell> coordinate_pair;
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
/**
* Number of SiDBs that are placed on the layout.
*/
uint64_t number_of_sidbs = 0;
/**
* If positively charged SiDBs should be prevented, SiDBs are not placed closer than the minimal_spacing.
*/
bool prevent_positive_charges = true;
/**
* If positively charged SiDBs should be prevented, SiDBs are not placed closer than this value (2 cells as
* Euclidean distance by default).
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
*/
double minimal_spacing = 2;
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
/**
* Maximal number of steps to place the given number of SiDBs.
*/
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
uint64_t maximal_attempts = 10E6;
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
};

/**
* Generates a random layout of SiDBs based on the provided parameters.
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
*
* @tparam Lyt The layout type.
* @param params The parameters for generating the random layout.
* @param lyt_skeleton A layout to which random cells are added (useful if you need to add random cells to a given
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
* layout).
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
* @return A randomly generated layout of SiDBs.
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
*/
template <typename Lyt>
Lyt generate_random_layout(const random_layout_params<Lyt>& params, const Lyt& lyt_skeleton)
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
{
static_assert(is_cell_level_layout_v<Lyt>, "Lyt is not a cell-level layout");
Drewniok marked this conversation as resolved.
Show resolved Hide resolved

const uint64_t number_of_sidbs_of_final_layout = lyt_skeleton.num_cells() + params.number_of_sidbs;

Lyt lyt{};

if (lyt_skeleton.num_cells() != 0)
{
lyt_skeleton.foreach_cell([&lyt, &lyt_skeleton](const auto& cell)
{ lyt.assign_cell_type(cell, lyt_skeleton.get_cell_type(cell)); });
}

bool successful_generation = false;
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
uint64_t attempt_counter = 0;

// Stops if either all SiDBs are placed or the maximum number of attempts were performed.
while (lyt.num_cells() < number_of_sidbs_of_final_layout && attempt_counter < params.maximal_attempts)
{
const auto random_coord = random_coordinate(params.coordinate_pair.first, params.coordinate_pair.second);

bool constraint_violation_positive_sidbs = false;
marcelwa marked this conversation as resolved.
Show resolved Hide resolved

if (params.prevent_positive_charges)
{
// Checks if the new coordinate is not closer than 2 cells (Euclidean distance) from an already
// placed SiDB.
lyt.foreach_cell(
[&lyt, &random_coord, &constraint_violation_positive_sidbs, &params](const auto& c1)
{
if (euclidean_distance<Lyt>(lyt, c1, random_coord) < params.minimal_spacing)
{
constraint_violation_positive_sidbs = true;
}
});
}
// If the constraint that no positive SiDBs occur is satisfied, the SiDB is added to the layout.
if (!constraint_violation_positive_sidbs)
{
lyt.assign_cell_type(random_coord, Lyt::cell_type::NORMAL);
}
marcelwa marked this conversation as resolved.
Show resolved Hide resolved
attempt_counter += 1;
}

return lyt;
}
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
Fixed Show fixed Hide fixed
Drewniok marked this conversation as resolved.
Show resolved Hide resolved

/**
* Generates multiple unique random layouts of SiDBs based on the provided parameters.
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
*
* @tparam Lyt The layout type.
* @param params The parameters for generating the random layouts.
* @param lyt_skeleton A layout to which random cells are added (useful if you need to add random cells to a given
* layout).
* @param number_of_unique_generated_layouts The desired number of unique layouts to be generated.
* @param maximal_attempts The maximum number of attempts allowed to generate a unique layout (default: \f$ 10^{6} \f$).
* @return A vector containing the unique randomly generated layouts.
*/
template <typename Lyt>
std::vector<Lyt> generate_multiple_random_layout(const random_layout_params<Lyt>& params, const Lyt& lyt_skeleton,
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
const uint64_t number_of_unique_generated_layouts,
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
const uint64_t maximal_attemps = 10E6)
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
{
static_assert(is_cell_level_layout_v<Lyt>, "Lyt is not a cell-level layout");
Drewniok marked this conversation as resolved.
Show resolved Hide resolved

std::vector<Lyt> unique_lyts{};
unique_lyts.reserve(number_of_unique_generated_layouts);
uint64_t counter = 0;
while (unique_lyts.size() < number_of_unique_generated_layouts && counter < maximal_attemps)
{
const auto random_lyt = generate_random_layout(params, lyt_skeleton);

uint64_t identical_layout_counter = 0;
for (const auto& old_lyt : unique_lyts)
{
// Checks if two layouts have the same cell.
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
uint64_t identical_cell_counter = 0;
old_lyt.foreach_cell(
[&identical_cell_counter, random_lyt](const auto& cell_old)
{
random_lyt.foreach_cell(
[&identical_cell_counter, &cell_old](const auto& cell_new)
{
if (cell_new == cell_old)
{
identical_cell_counter += 1;
}
});
});
Drewniok marked this conversation as resolved.
Show resolved Hide resolved

// all cells are identical, so the new layout is a duplicate.
if (identical_cell_counter == random_lyt.num_cells())
{
identical_layout_counter += 1;
}
}

if (identical_layout_counter == 0)
{
unique_lyts.push_back(random_lyt);
}

counter += 1;
}

return unique_lyts;
}

} // namespace fiction

#endif // FICTION_RANDOM_LAYOUT_GENERATOR_HPP
Loading