def from_reserved_name(cls, name, meta_agent=None, bucket=CAMD_S3_BUCKET): """ Invokes a MetaAgent Campaign from a reserved name Args: name (str): name of the campaign to be run meta_agent (HypothesisAgent): meta-agent with which to select the agent simulations to be run, defaults to a random agent with n_query of 1 bucket (str): name of the bucket from which to invoke the campaign Returns: (MetaAgentCampaign) - meta-agent campaign corresponding to the campaign """ agent_pool, experiment, analyzer = cls.load_pickled_objects( name, bucket) s3_prefix = "{}/{}".format(META_AGENT_PREFIX, name) candidate_data = convert_parameter_table_to_dataframe(agent_pool) meta_agent = meta_agent or RandomAgent(n_query=1) return cls( candidate_data=candidate_data, agent=meta_agent, experiment=experiment, analyzer=analyzer, s3_prefix=s3_prefix, s3_bucket=bucket, create_seed=1, )
def test_mp_loop(self): df = pd.read_csv(os.path.join(CAMD_TEST_FILES, 'test_df_analysis.csv')) df['id'] = [int(mp_id.replace("mp-", "").replace('mvc-', '')) for mp_id in df['id']] df.set_index("id") df['Composition'] = df['formula'] # Just use the Ti-O-N chemsys seed = df.iloc[:38] candidates = df.iloc[38:209] agent = RandomAgent(n_query=20) analyzer = StabilityAnalyzer(hull_distance=0.05, parallel=False) experiment = ATFSampler(dataframe=df) new_loop = Campaign( candidates, agent, experiment, analyzer, seed_data=seed ) new_loop.initialize() for iteration in range(6): new_loop.run() self.assertTrue(os.path.isfile("hull.png")) if iteration >= 1: self.assertTrue( os.path.isfile("history.pickle")) # Testing the continuation new_loop = Campaign(df, agent, experiment, analyzer) self.assertTrue(new_loop.initialized) self.assertEqual(new_loop.iteration, 6) self.assertEqual(new_loop.loop_state, None) new_loop.run() self.assertTrue(True) self.assertEqual(new_loop.iteration, 7)
def test_random_agent_loop(self): df = load_default_atf_data() n_seed = 200 # Starting sample size agent = RandomAgent(n_query=10) analyzer = StabilityAnalyzer(hull_distance=0.05, parallel=False) experiment = ATFSampler(dataframe=df) candidate_data = df new_loop = Campaign(candidate_data, agent, experiment, analyzer, create_seed=n_seed) new_loop.initialize() self.assertFalse(new_loop.create_seed) for _ in range(6): new_loop.run() self.assertTrue(True) # Testing the continuation new_loop = Campaign(candidate_data, agent, experiment, analyzer, create_seed=n_seed) self.assertTrue(new_loop.initialized) self.assertEqual(new_loop.iteration, 6) self.assertEqual(new_loop.loop_state, None) new_loop.run() self.assertTrue(True) self.assertEqual(new_loop.iteration, 7)
def initialize(self, random_state=42): """ Initializes a campaign. The primary goal of initialization is to ensure a proper seed exists. If create_seed is set in Campaign, it creates the seed by deploying the RandomAgent before the user-provided agent is deployed in the regular campaign iterations. random_state (int): ensures reproducible results. """ if self.initialized: raise ValueError("Initialization may overwrite existing loop data. Exit.") if not self.seed_data.empty and not self.create_seed: print( "{} {} state: Agent {} hypothesizing".format( self.type, "initialization", self.agent.__class__.__name__ ) ) suggested_experiments = self.agent.get_hypotheses( self.candidate_data, self.seed_data ) elif self.create_seed: np.random.seed(seed=random_state) _agent = RandomAgent(self.candidate_data, n_query=self.create_seed) print( "{} {} state: Agent {} hypothesizing".format( self.type, "initialization", _agent.__class__.__name__ ) ) suggested_experiments = _agent.get_hypotheses(self.candidate_data) else: raise ValueError( "No seed data available. Either supply or ask for creation." ) self.analyzer._initial_seed_indices = self.seed_data.index.tolist() print("{} {} state: Running experiments".format(self.type, self.iteration)) self.job_status = self.experiment.submit(suggested_experiments) self.consumed_candidates = suggested_experiments.index.values.tolist() self.create_seed = False self.initialized = True self.save("job_status") self.save("seed_data", method="pickle") self.save("experiment", method="pickle") self.save("consumed_candidates") self.save("iteration") if self.s3_prefix: self.s3_sync()
def test_run(self): with ScratchDir('.'): dataframe = get_oqmd_data_by_chemsys("Fe-O") cand, seed = partition_intercomp(dataframe, n_elements=1) agents_df = pd.DataFrame({"agent": [RandomAgent()]}) simulation = LocalAgentSimulation( cand, iterations=5, seed_data=seed, analyzer=StabilityAnalyzer()) simulation.submit(agents_df) simulation.monitor() results = simulation.get_results() self.assertTrue(True)
def test_random_agent_loop(self): df = load_dataframe("oqmd1.2_exp_based_entries_featurized_v2") n_seed = 5000 agent = RandomAgent(n_query=200) analyzer = StabilityAnalyzer(hull_distance=0.05, parallel=False) experiment = ATFSampler(dataframe=df) candidate_data = df new_loop = Campaign(candidate_data, agent, experiment, analyzer, create_seed=n_seed) new_loop.initialize() self.assertFalse(new_loop.create_seed) for _ in range(6): new_loop.run() self.assertTrue(True)
def test_simulated(self): exp_dataframe = pd.read_pickle( os.path.join(CAMD_TEST_FILES, "mn-ni-o-sb.pickle")) experiment = ATFSampler(exp_dataframe) candidate_data = exp_dataframe.iloc[:, :-11] agent = RandomAgent(n_query=2) analyzer = StabilityAnalyzer(hull_distance=0.2) # Reduce seed_data seed_data = load_dataframe("oqmd1.2_exp_based_entries_featurized_v2") seed_data = filter_dataframe_by_composition(seed_data, "MnNiOSb") with ScratchDir('.'): campaign = ProtoDFTCampaign(candidate_data=candidate_data, agent=agent, experiment=experiment, analyzer=analyzer, seed_data=seed_data, heuristic_stopper=5) campaign.autorun()
def from_chemsys(cls, chemsys): """ Args: chemsys: Returns: """ s3_prefix = "oqmd-atf/runs/{}".format(chemsys) df = pd.read_csv(os.path.join(CAMD_TEST_FILES, 'test_df.csv')) n_seed = 200 # Starting sample size n_query = 10 # This many new candidates are "calculated with DFT" (i.e. requested from Oracle -- DFT) agent = RandomAgent(n_query=n_query) analyzer = StabilityAnalyzer(hull_distance=0.05) experiment = ATFSampler(dataframe=df) candidate_data = df return cls(candidate_data, agent, experiment, analyzer, create_seed=n_seed, s3_prefix=s3_prefix)
def test_run(self): agent_pool = ParameterTable(RANDOM_TEST_AGENT_PARAMS) # Construct experiment dataframe = get_oqmd_data_by_chemsys("Fe-O") cand, seed = partition_intercomp(dataframe, n_elements=1) experiment = LocalAgentSimulation( atf_candidate_data=cand, seed_data=seed, analyzer=StabilityAnalyzer(), iterations=10, ) analyzer = StabilityCampaignAnalyzer(checkpoint_indices=[2, 5, 10]) MetaAgentCampaign.reserve( name="test_meta_agent", experiment=experiment, agent_pool=agent_pool, analyzer=analyzer ) with ScratchDir('.'): print("Testing meta agent") campaign = MetaAgentCampaign.from_reserved_name( "test_meta_agent", meta_agent=RandomAgent(n_query=1), ) campaign.autorun() self.assertTrue(True)
# Copyright Toyota Research Institute 2019 from camd.campaigns.base import Campaign from camd.agent.base import RandomAgent from camd.analysis import StabilityAnalyzer from camd.experiment.base import ATFSampler from camd.utils.data import load_default_atf_data ########################################################## # Load dataset and filter by n_species of 2 or less ########################################################## df = load_default_atf_data() ########################################################## # Binary stable material discovery Random Agent example ########################################################## n_seed = 5000 # Starting sample size n_query = 200 # This many new candidates are "calculated with DFT" (i.e. requested from Oracle -- DFT) agent = RandomAgent(n_query=n_query) analyzer = StabilityAnalyzer(hull_distance=0.05) experiment = ATFSampler(dataframe=df) candidate_data = df new_loop = Campaign(candidate_data, agent, experiment, analyzer, create_seed=n_seed) new_loop.auto_loop(n_iterations=4, initialize=True)