-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved generate_agents_from_file out of model and into its own function
- Loading branch information
1 parent
5143d58
commit d9a0595
Showing
9 changed files
with
90 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
#include "util/misc.hpp" | ||
#include <cstddef> | ||
#include <vector> | ||
|
||
namespace Seldon::Agents | ||
{ | ||
|
||
template<typename AgentT> | ||
std::vector<AgentT> generate_from_file( const std::string & file ) | ||
{ | ||
std::vector<AgentT> agents{}; | ||
|
||
std::string file_contents = get_file_contents( file ); | ||
bool finished = false; | ||
size_t start_of_line = 0; | ||
|
||
while( !finished ) | ||
{ | ||
// Find the end of the current line | ||
auto end_of_line = file_contents.find( '\n', start_of_line ); | ||
if( end_of_line == std::string::npos ) | ||
{ | ||
finished = true; | ||
} | ||
// Get the current line as a substring | ||
auto line = file_contents.substr( start_of_line, end_of_line - start_of_line ); | ||
start_of_line = end_of_line + 1; | ||
// TODO: check if empty or comment | ||
if( line.empty() ) | ||
{ | ||
break; | ||
} | ||
if( line[0] == '#' ) | ||
{ | ||
continue; | ||
} | ||
|
||
agents.push_back( AgentT() ); | ||
|
||
// First column is the index of the agent | ||
auto end_of_first_column = line.find( ',', 0 ); | ||
auto opinion_substring = line.substr( end_of_first_column + 1, end_of_line ); | ||
agents.back().from_string( opinion_substring ); | ||
} | ||
|
||
return agents; | ||
} | ||
|
||
} // namespace Seldon::Agents |
This file contains 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 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 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 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 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 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 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 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