Skip to content

Commit

Permalink
csp - initial algorithm and new test dlg
Browse files Browse the repository at this point in the history
  • Loading branch information
kataklinger committed Oct 22, 2013
1 parent 3e7a0e5 commit f605557
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
*.sdf
*.suo
*.aps
*.user
2 changes: 1 addition & 1 deletion Problems/CSP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Problems
{
namespace BPP
namespace CSP
{

Point& Point::operator =(const Point& rhs)
Expand Down
2 changes: 1 addition & 1 deletion Problems/CSP.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Problems
{
namespace BPP
namespace CSP
{

class Size;
Expand Down
18 changes: 14 additions & 4 deletions TestApps/App11/App11.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<UseOfMfc>Static</UseOfMfc>
<UseOfMfc>Dynamic</UseOfMfc>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<UseOfMfc>Static</UseOfMfc>
<UseOfMfc>Dynamic</UseOfMfc>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand All @@ -53,7 +53,7 @@
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
Expand All @@ -77,7 +77,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
Expand All @@ -104,6 +104,7 @@
<ItemGroup>
<ClInclude Include="App11.h" />
<ClInclude Include="ChildView.h" />
<ClInclude Include="CSPAlgorithm.h" />
<ClInclude Include="MainFrm.h" />
<ClInclude Include="NewTestDlg.h" />
<ClInclude Include="Resource.h" />
Expand All @@ -113,6 +114,7 @@
<ItemGroup>
<ClCompile Include="App11.cpp" />
<ClCompile Include="ChildView.cpp" />
<ClCompile Include="CSPAlgorithm.cpp" />
<ClCompile Include="MainFrm.cpp" />
<ClCompile Include="NewTestDlg.cpp" />
<ClCompile Include="stdafx.cpp">
Expand All @@ -129,6 +131,14 @@
<ItemGroup>
<Image Include="res\App11.ico" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\GALex.vcxproj">
<Project>{55d67b0e-145c-4cdd-bc27-0de87a86d821}</Project>
</ProjectReference>
<ProjectReference Include="..\..\Problems\Problems.vcxproj">
<Project>{93560795-9abc-4748-a161-f9166987b4d4}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
6 changes: 6 additions & 0 deletions TestApps/App11/App11.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<ClInclude Include="NewTestDlg.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CSPAlgorithm.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="App11.cpp">
Expand All @@ -53,6 +56,9 @@
<ClCompile Include="NewTestDlg.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="CSPAlgorithm.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="App11.rc">
Expand Down
99 changes: 99 additions & 0 deletions TestApps/App11/CSPAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

#include "CSPAlgorithm.h"

CSPAlgorithm::CSPAlgorithm(Common::Observing::GaEventHandler* handler) : _populationFitnessOperation(&_fitnessOperation),
_workflow(NULL)
{
GaInitialize();

Chromosome::GaMatingConfig matingConfiguration(
Chromosome::GaCrossoverSetup( &_crossover, &Chromosome::GaCrossoverParams( 1.0f, 2 ), NULL ),
Chromosome::GaMutationSetup( &_mutation, &Chromosome::GaMutationSizeParams( 0.66f, true, 2L ), NULL ) );

Fitness::GaFitnessComparatorSetup fitnessComparatorSetup( &_fitnessComparator,
&Fitness::Comparators::GaSimpleComparatorParams( Fitness::Comparators::GACT_MAXIMIZE_ALL ), NULL );

Algorithm::Stubs::GaSimpleGAStub::GaStatTrackersCollection trackers;
trackers[ Population::GaPopulationSizeTracker::TRACKER_ID ] = &_sizeTracker;
trackers[ Population::GaRawFitnessTracker::TRACKER_ID ] = &_rawTracker;
trackers[ Population::GaScaledFitnessTracker::TRACKER_ID ] = &_scaledTracker;

Population::GaSelectionSetup selectionSetup( &_selection,
&Population::SelectionOperations::GaTournamentSelectionParams( 2, -1, 2, 2, Population::SelectionOperations::GaTournamentSelectionParams::GATST_ROULETTE_WHEEL_SELECTION ),
&Population::SelectionOperations::GaTournamentSelectionConfig( fitnessComparatorSetup, Chromosome::GaMatingSetup() ) );

Population::GaCouplingSetup couplingSetup( &_coupling, &Population::GaCouplingParams( 50, 1 ),
&Population::GaCouplingConfig( Chromosome::GaMatingSetup( &_mating, NULL, &matingConfiguration ) ) );

Population::GaReplacementSetup replacementSetup( &_replacement, &Population::GaReplacementParams( 50 ), &Population::GaReplacementConfig() );
Population::GaScalingSetup scalingSetup( &_scaling, NULL, &Population::GaScalingConfig() );

Algorithm::Stubs::GaSimpleGAStub simpleGA( WDID_POPULATION, WDID_POPULATION_STATS,
Chromosome::GaInitializatorSetup(),
Population::GaPopulationFitnessOperationSetup( &_populationFitnessOperation, NULL,
&Fitness::GaFitnessOperationConfig( NULL ) ),
fitnessComparatorSetup,
Population::GaPopulationParams( 100, 0, Population::GaPopulationParams::GAPFO_FILL_ON_INIT ),
trackers,
Chromosome::GaMatingSetup(),
selectionSetup,
couplingSetup,
replacementSetup,
scalingSetup,
Population::GaFitnessComparatorSortingCriteria( fitnessComparatorSetup, Population::GaChromosomeStorage::GAFT_RAW ) );
simpleGA.SetBranchCount( 2 );

_workflow.RemoveConnection( *_workflow.GetFirstStep()->GetOutboundConnections().begin(), true );

Common::Workflows::GaWorkflowBarrier* br1 = new Common::Workflows::GaWorkflowBarrier();
simpleGA.Connect( _workflow.GetFirstStep(), br1 );

Common::Workflows::GaBranchGroup* bg1 = (Common::Workflows::GaBranchGroup*)_workflow.ConnectSteps( br1, _workflow.GetLastStep(), 0 );

Algorithm::StopCriteria::GaStopCriterionStep* stopStep = new Algorithm::StopCriteria::GaStopCriterionStep(
Algorithm::StopCriteria::GaStopCriterionSetup( &_stopCriterion,
&Algorithm::StopCriteria::GaStatsChangesCriterionParams(
Population::GADV_BEST_FITNESS, 100), NULL ), _workflow.GetWorkflowData(), WDID_POPULATION_STATS );

Common::Workflows::GaBranchGroupTransition* bt1 = new Common::Workflows::GaBranchGroupTransition();

bg1->GetBranchGroupFlow()->SetFirstStep( stopStep );
bg1->GetBranchGroupFlow()->ConnectSteps( stopStep, bt1, 0 );
_workflow.ConnectSteps( bt1, simpleGA.GetStubFlow().GetFirstStep(), 1 );

Common::Workflows::GaDataCache<Population::GaPopulation> population( _workflow.GetWorkflowData(), WDID_POPULATION );

population.GetData().GetEventManager().AddEventHandler( Population::GaPopulation::GAPE_NEW_GENERATION, handler );
}

CSPAlgorithm::~CSPAlgorithm()
{
_workflow.Wait();

GaFinalize();
}

void CSPAlgorithm::SetParameters(int sWidth, int sHeight, int iMinWidth, int iMaxWidth, int iMinHeight, int iMaxHeight, int iCount)
{
Common::Data::GaSingleDimensionArray<Problems::CSP::Item> items( iCount );
for( int i = 0; i < iCount; i++ )
items[ i ] = Problems::CSP::Item(
Problems::CSP::Size(GaGlobalRandomFloatGenerator->Generate( iMinWidth, iMaxWidth ), GaGlobalRandomFloatGenerator->Generate( iMinHeight, iMaxHeight ) ),
std::string( "L" ), i );

Chromosome::GaInitializatorSetup initializatorSetup( &_initializator, NULL, &Chromosome::GaInitializatorConfig(
&Problems::CSP::CspConfigBlock( items, Problems::CSP::Size( sWidth, sHeight ) ) ) );

Common::Workflows::GaDataCache<Population::GaPopulation> population( _workflow.GetWorkflowData(), WDID_POPULATION );
population.GetData().SetInitializator( initializatorSetup );
}

void CSPAlgorithm::Start()
{
_workflow.Start();
}

void CSPAlgorithm::Stop()
{
_workflow.Stop();
}
61 changes: 61 additions & 0 deletions TestApps/App11/CSPAlgorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

#pragma once

#include "..\..\source\Initialization.h"
#include "..\..\Problems\CSP.h"
#include "..\..\source\Crossovers.h"
#include "..\..\source\SimpleStub.h"
#include "..\..\source\Matings.h"
#include "..\..\source\PopulationStatistics.h"
#include "..\..\source\StopCriteria.h"

#include "..\..\source\Selections.h"
#include "..\..\source\Couplings.h"
#include "..\..\source\Replacements.h"
#include "..\..\source\FitnessSharing.h"
#include "..\..\source\Scalings.h"
#include "..\..\source\StopCriteria.h"

class CSPAlgorithm
{
private:
enum WorkflowDataIDs
{
WDID_POPULATION,
WDID_POPULATION_STATS
};

Problems::CSP::CspInitializator _initializator;
Problems::CSP::CspCrossoverOperation _crossover;
Problems::CSP::CspMutationOperation _mutation;
Problems::CSP::CspFitnessOperation _fitnessOperation;
Fitness::Comparators::GaSimpleComparator _fitnessComparator;

Population::GaCombinedFitnessOperation _populationFitnessOperation;

Chromosome::MatingOperations::GaBasicMatingOperation _mating;

Population::GaPopulationSizeTracker _sizeTracker;
Population::GaRawFitnessTracker _rawTracker;
Population::GaScaledFitnessTracker _scaledTracker;

Population::SelectionOperations::GaTournamentSelection _selection;
Population::CouplingOperations::GaSimpleCoupling _coupling;
Population::ReplacementOperations::GaWorstReplacement _replacement;
Population::ScalingOperations::GaNoScaling _scaling;

Algorithm::StopCriteria::GaStatsChangesCriterion _stopCriterion;

Common::Workflows::GaWorkflow _workflow;


public:
CSPAlgorithm(Common::Observing::GaEventHandler* handler);
~CSPAlgorithm();

void SetParameters(int sWidth, int sHeight, int iMinWidth, int iMaxWidth, int iMinHeight, int iMaxHeight, int iCount);

void Start();
void Stop();

};
27 changes: 27 additions & 0 deletions TestApps/App11/ChildView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "App11.h"
#include "ChildView.h"

#include "NewTestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
Expand All @@ -24,6 +26,9 @@ CChildView::~CChildView()

BEGIN_MESSAGE_MAP(CChildView, CWnd)
ON_WM_PAINT()
ON_COMMAND(ID_FILE_NEW_TEST, &CChildView::OnFileNewTest)
ON_COMMAND(ID_FILE_START, &CChildView::OnFileStart)
ON_COMMAND(ID_FILE_STOP, &CChildView::OnFileStop)
END_MESSAGE_MAP()


Expand Down Expand Up @@ -52,3 +57,25 @@ void CChildView::OnPaint()
// Do not call CWnd::OnPaint() for painting messages
}



void CChildView::OnFileNewTest()
{
CNewTestDlg dlg;
if( dlg.DoModal() == IDOK )
{
/* set new algorithm */
}
}


void CChildView::OnFileStart()
{
// TODO: Add your command handler code here
}


void CChildView::OnFileStop()
{
// TODO: Add your command handler code here
}
4 changes: 4 additions & 0 deletions TestApps/App11/ChildView.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ class CChildView : public CWnd
protected:
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnFileNewTest();
afx_msg void OnFileStart();
afx_msg void OnFileStop();
};

16 changes: 8 additions & 8 deletions TestApps/App11/NewTestDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ IMPLEMENT_DYNAMIC(CNewTestDlg, CDialogEx)

CNewTestDlg::CNewTestDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CNewTestDlg::IDD, pParent)
, m_sheetWidth(0)
, m_sheetHeight(0)
, m_itemMinWidth(0)
, m_itemMaxWidth(0)
, m_itemMinHeight(0)
, m_itemMaxHeight(0)
, m_itemCount(0)
, m_sheetWidth(1)
, m_sheetHeight(1)
, m_itemMinWidth(1)
, m_itemMaxWidth(1)
, m_itemMinHeight(1)
, m_itemMaxHeight(1)
, m_itemCount(1)
{

}
Expand All @@ -39,7 +39,7 @@ void CNewTestDlg::DoDataExchange(CDataExchange* pDX)
DDX_Text(pDX, IDC_ITEM_MAX_HEIGHT, m_itemMaxHeight);
DDX_Text(pDX, IDC_ITEM_COUNT, m_itemCount);
DDV_MinMaxInt(pDX, m_sheetWidth, 1, INT_MAX);
DDV_MinMaxInt(pDX, m_sheetHeight, m_sheetWidth + 1, INT_MAX);
DDV_MinMaxInt(pDX, m_sheetHeight, 1, INT_MAX);
DDV_MinMaxInt(pDX, m_itemMinWidth, 1, m_sheetWidth);
DDV_MinMaxInt(pDX, m_itemMaxWidth, m_itemMinWidth, m_sheetWidth);
DDV_MinMaxInt(pDX, m_itemMinHeight, 1, m_sheetHeight);
Expand Down

0 comments on commit f605557

Please sign in to comment.