Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
I
IVMCTrim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fjn1g13
IVMCTrim
Commits
f8e1dfc9
Commit
f8e1dfc9
authored
May 25, 2021
by
fjn1g13
Browse files
Options
Downloads
Patches
Plain Diff
Now producing results identical to M4M
parent
8d82a963
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
IVMCTrim/Program.cs
+36
-14
36 additions, 14 deletions
IVMCTrim/Program.cs
with
36 additions
and
14 deletions
IVMCTrim/Program.cs
+
36
−
14
View file @
f8e1dfc9
...
...
@@ -102,12 +102,12 @@ namespace IVMCTrim
/// <summary>
/// Represents an operation to perform at the end of an episode, for reporting/recording purposes.
/// </summary>
public
delegate
void
EndOfEpisodeReporter
(
IvmcEnvironment
environment
,
Individual
individual
,
Evaluation
Evaluation
,
int
episode
);
public
delegate
void
EndOfEpisodeReporter
(
IvmcEnvironment
environment
,
Individual
Evaluation
individualEvaluation
,
int
episode
);
/// <summary>
/// Represents an operation to perform at the end of an episode, for reporting/recording purposes.
/// </summary>
public
delegate
void
EndOfEvolutionaryStepReporter
(
IvmcEnvironment
environment
,
Individual
individual
,
Evaluation
Evaluation
,
int
episode
,
int
evolutionaryStep
);
public
delegate
void
EndOfEvolutionaryStepReporter
(
IvmcEnvironment
environment
,
Individual
Evaluation
individualEvaluation
,
int
episode
,
int
evolutionaryStep
);
/// <summary>
/// Runs a single experiment with the given parameters in the given context, reporting the completion of each episode.
...
...
@@ -120,7 +120,7 @@ namespace IVMCTrim
individual
.
DevelopInplace
(
ctx
,
parameters
);
// report initial state
endOfEpisode
(
null
,
individual
,
default
,
0
);
endOfEpisode
?.
Invoke
(
null
,
new
IndividualEvaluation
(
individual
,
default
)
,
0
);
// prepare the environment
var
environment
=
new
IvmcEnvironment
(
parameters
.
ModuleSize
,
parameters
.
ModuleCount
);
...
...
@@ -129,10 +129,10 @@ namespace IVMCTrim
for
(
int
episode
=
1
;
episode
<=
parameters
.
TotalEpisodes
;
episode
++)
{
// run one episode
var
evaluation
=
Model
.
RunEpisode
(
episode
,
ctx
,
parameters
,
environment
,
ref
individual
,
null
);
var
result
=
Model
.
RunEpisode
(
episode
,
ctx
,
parameters
,
environment
,
ref
individual
,
null
);
// report state at end of episode
endOfEpisode
(
environment
,
individual
,
evaluation
,
episode
);
endOfEpisode
?.
Invoke
(
environment
,
result
,
episode
);
}
}
...
...
@@ -140,7 +140,7 @@ namespace IVMCTrim
/// Runs an episode with the given parameters, environment, and individual.
/// The individual may be modified or replaced with a different instance.
/// </summary>
public
static
Evaluation
RunEpisode
(
int
episode
,
ExecutionContext
ctx
,
Parameters
parameters
,
IvmcEnvironment
environment
,
ref
Individual
individual
,
EndOfEvolutionaryStepReporter
endOfEvolutionaryStepReporter
)
public
static
Individual
Evaluation
RunEpisode
(
int
episode
,
ExecutionContext
ctx
,
Parameters
parameters
,
IvmcEnvironment
environment
,
ref
Individual
individual
,
EndOfEvolutionaryStepReporter
endOfEvolutionaryStepReporter
)
{
// perform start of episode action (e.g. randomise environment)
parameters
.
EpisodeStartOperation
(
ctx
,
parameters
,
individual
,
environment
);
...
...
@@ -150,8 +150,9 @@ namespace IVMCTrim
// evaluate the current individual
var
evaluation
=
Evaluate
(
ctx
,
parameters
,
environment
,
individual
);
var
result
=
new
IndividualEvaluation
(
individual
,
evaluation
);
endOfEvolutionaryStepReporter
?.
Invoke
(
environment
,
individual
,
evaluation
,
episode
,
0
);
endOfEvolutionaryStepReporter
?.
Invoke
(
environment
,
result
,
episode
,
0
);
// initialise an candidate individual
var
candidate
=
Individual
.
CreateZeroGenome
(
individual
.
Size
);
...
...
@@ -164,6 +165,8 @@ namespace IVMCTrim
// evaluate the candidate
var
candidateEvaluation
=
Evaluate
(
ctx
,
parameters
,
environment
,
candidate
);
result
=
new
IndividualEvaluation
(
individual
,
evaluation
);
// keep the candidate if he is best
if
(
candidateEvaluation
.
Fitness
>
evaluation
.
Fitness
)
{
...
...
@@ -174,10 +177,11 @@ namespace IVMCTrim
}
// report
endOfEvolutionaryStepReporter
?.
Invoke
(
environment
,
individual
,
evaluation
,
episode
,
evolutionaryStep
+
1
);
endOfEvolutionaryStepReporter
?.
Invoke
(
environment
,
new
IndividualEvaluation
(
individual
,
evaluation
)
,
episode
,
evolutionaryStep
+
1
);
}
return
evaluation
;
// NOTE NOTE: we return the penultimate individual for compatibility with M4M
return
result
;
}
/// <summary>
...
...
@@ -478,6 +482,21 @@ namespace IVMCTrim
}
}
/// <summary>
/// Represents an individual and its evaluation.
/// </summary>
public
struct
IndividualEvaluation
{
public
IndividualEvaluation
(
Individual
inidividual
,
Evaluation
evaluation
)
{
Inidividual
=
inidividual
??
throw
new
ArgumentNullException
(
nameof
(
inidividual
));
Evaluation
=
evaluation
;
}
public
Individual
Inidividual
{
get
;
}
public
Evaluation
Evaluation
{
get
;
}
}
/// <summary>
/// Represents the benefit (b), cost (c), and combined fitness (f) of an individual.
/// f = b - λc
...
...
@@ -818,15 +837,18 @@ namespace IVMCTrim
var
_recorders
=
recorders
=
new
Recorders
(
parameters
.
ModuleSize
,
parameters
.
ModuleCount
,
reportingPeriodMultiplier
);
// endOfEpisode reporting actions
void
endOfEpisode
(
IvmcEnvironment
environment
,
Individual
individual
,
Evaluation
Evaluation
,
int
episode
)
void
endOfEpisode
(
IvmcEnvironment
environment
,
Individual
Evaluation
individualEvaluation
,
int
episode
)
{
var
individual
=
individualEvaluation
.
Inidividual
;
var
evaluation
=
individualEvaluation
.
Evaluation
;
// inform recorders
_recorders
.
Sample
(
environment
,
individual
,
E
valuation
,
episode
);
_recorders
.
Sample
(
environment
,
individual
,
e
valuation
,
episode
);
if
(
episode
%
(
reportingPeriodMultiplier
*
1000
)
==
0
)
{
// report progress
Console
.
WriteLine
(
$"
{
outputDirectory
}
:
{
episode
}
/
{
parameters
.
TotalEpisodes
}
(
{(
double
)
episode
/
parameters
.
TotalEpisodes
:
P
}
)\tf=
{
E
valuation
.
Fitness
}
"
);
Console
.
WriteLine
(
$"
{
outputDirectory
}
:
{
episode
}
/
{
parameters
.
TotalEpisodes
}
(
{(
double
)
episode
/
parameters
.
TotalEpisodes
:
P
}
)\tf=
{
e
valuation
.
Fitness
}
"
);
// produce grn plot
var
grnPlot
=
PlotHelpers
.
IntegralCartesianAxes
(
$"Episode
{
episode
}
"
,
"row"
,
"col"
);
...
...
@@ -841,10 +863,10 @@ namespace IVMCTrim
var
fitnessSamples
=
new
List
<
DataPoint
>();
void
sample
(
IvmcEnvironment
_env
,
Individual
_ind
,
Evaluation
e
valuation
,
int
_episode
,
int
evolutionaryStep
)
void
sample
(
IvmcEnvironment
_env
,
IndividualEvaluation
stepIndividualE
valuation
,
int
_episode
,
int
evolutionaryStep
)
{
if
(
evolutionaryStep
%
100
==
0
)
fitnessSamples
.
Add
(
new
DataPoint
(
_episode
*
parameters
.
EvolutionaryStepsPerEpisode
+
evolutionaryStep
,
e
valuation
.
Fitness
));
fitnessSamples
.
Add
(
new
DataPoint
(
_episode
*
parameters
.
EvolutionaryStepsPerEpisode
+
evolutionaryStep
,
stepIndividualEvaluation
.
E
valuation
.
Fitness
));
}
int
traceeEpisodeCount
=
4
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment