WSF
WsfQuantumAllocator.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2003-2015 The Boeing Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11
12#ifndef WSFQUANTUMALLOCATOR_HPP
13#define WSFQUANTUMALLOCATOR_HPP
14
15#include "wsf_mil_export.h"
16
17#include <memory>
18#include <string>
19#include <vector>
20
21#include "UtCloneablePtr.hpp"
22#include "UtMemory.hpp"
23#include "UtOptimalAssignment.hpp"
24class UtScript;
25class UtScriptContext;
26class UtScriptExecutor;
28#include "WsfQuantumMatrix.hpp"
29class WsfQuantumTask;
31
32using AllocationList = std::vector<std::pair<WsfAssetPerception*, ut::CloneablePtr<WsfQuantumTask>>>;
33
34
35class WSF_MIL_EXPORT WsfQuantumAllocator
36{
37public:
38 // implement different types of greedy allocation & optimal profit allocation (simplex/Hungarian algorithm)
39 // enum AllocationType
40 //{
41 // GREEDY_PROFIT, //highest profit awarded first (profit = value * priority)
42 // OPTIMAL_PROFIT, //optimal profit assignment algorithm (Hungarian algorithm using hopkroft-karp method:
43 // O(N^3) complexity) GREEDY_VALUE, //highest value is awarded first (priority used for tie breaks)
44 // GREEDY_PRIORITY, //highest priority is awarded first (value used for tie breaks)
45 // GREEDY_ISOLATED, //all assets awarded the task they valued highest (stacks up on tasks)
46 // IDLE //assets remain unassigned, left idle (useful for secondary allocation type)
47 // };
48
50 virtual ~WsfQuantumAllocator() = default;
52 virtual std::string AllocatorType() = 0; // allocators must define this
53 virtual WsfQuantumAllocator* Clone() = 0;
54 virtual void Initialize(double aSimTime, WsfScriptContext* aParentContextPtr = nullptr){};
55 // allocators must define this
57
60
61 std::string TaskType() { return mTaskType; }
62 bool HasTaskType() { return !mTaskType.empty(); }
63 void SetTaskType(std::string& aType) { mTaskType = aType; }
64
65protected:
66 std::string mTaskType;
67 std::map<WsfAssetPerception*, WsfQuantumTask*> mAssetAllocationMap;
68 std::map<WsfQuantumTask*, WsfAssetPerception*> mTaskAllocationMap;
69};
70
71class WSF_MIL_EXPORT WsfQuantumAllocatorFactory
72{
73public:
78
80 bool Register(std::unique_ptr<WsfQuantumAllocator> aAllocatorPtr);
81 WsfQuantumAllocator* CreateAllocator(const std::string& allocatorName);
82 const std::map<std::string, ut::CloneablePtr<WsfQuantumAllocator>>& Allocators() const { return mFactoryMap; }
83
84private:
87
88 std::map<std::string, ut::CloneablePtr<WsfQuantumAllocator>> mFactoryMap;
89};
90
92{
93public:
94 WsfQuantumAllocatorScript(WsfScriptContext* aContextPtr, std::string& aScriptMethod);
95 ~WsfQuantumAllocatorScript() override = default;
96 void Initialize(double aSimTime, WsfScriptContext* aParentContextPtr = nullptr) override;
97 WsfQuantumAllocator* Clone() override { return new WsfQuantumAllocatorScript(*this); }
98 std::string AllocatorType() override { return mType; }
99 virtual bool Valid() { return (mScriptPtr != nullptr); }
100 AllocationList MakeAllocations(WsfQuantumMatrix& aMatrix) override;
101
102private:
104 WsfQuantumAllocatorScript& operator=(const WsfQuantumAllocatorScript&) = delete;
105
106 std::string mType;
107 UtScript* mScriptPtr;
108 UtScriptContext* mContextPtr;
109 UtScriptExecutor* mExecutorPtr;
110};
111
112
114{
115public:
117 ~WsfQuantumAllocatorSimple() override = default;
118 std::string AllocatorType() override { return "simple"; }
119 WsfQuantumAllocator* Clone() override { return new WsfQuantumAllocatorSimple(*this); }
120 AllocationList MakeAllocations(WsfQuantumMatrix& aMatrix) override;
121};
122
123// this allocator assigns the highest profit task for each asset, regardless of other assignments
124// this could result in several assets being assigned the same task
126{
127public:
130 std::string AllocatorType() override { return "greedy_isolated"; }
132 AllocationList MakeAllocations(WsfQuantumMatrix& aMatrix) override;
133};
134
135// this allocator starts assigning assets to the highest priority tasks first
136// it assigns the highest valued asset (that still remains unassigned) to each task
137// some tasks might be left unassigned
139{
140public:
143 std::string AllocatorType() override { return "greedy_priority"; }
145 AllocationList MakeAllocations(WsfQuantumMatrix& aMatrix) override;
146};
147
148// this allocator starts assigning tasks to the highest value assets first (task priority only used as a tie-breaker)
149// some tasks might be left unassigned
151{
152public:
155 std::string AllocatorType() override { return "greedy_value"; }
157 AllocationList MakeAllocations(WsfQuantumMatrix& aMatrix) override;
158};
159
160// this allocator starts assigning tasks to the highest profit asset pairing first (that still remain)
161// some tasks might be left unassigned
163{
164public:
167 std::string AllocatorType() override { return "greedy_profit"; }
169 AllocationList MakeAllocations(WsfQuantumMatrix& aMatrix) override;
170};
171
172// this allocator finds the optimal profit allocation of tasks to assets
173// some tasks might be left unassigned, some assets might be left without tasks
174// uses class: UtOptimalAssignment
176{
177public:
180 std::string AllocatorType() override { return "optimal_profit"; }
182 AllocationList MakeAllocations(WsfQuantumMatrix& aMatrix) override;
183
184private:
185 UtOptimalAssignment mOptimalAssigner;
186};
187
188#endif
std::vector< std::pair< WsfAssetPerception *, ut::CloneablePtr< WsfQuantumTask > > > AllocationList
Definition WsfQuantumAllocator.hpp:32
Definition WsfAssetPerception.hpp:32
Definition WsfQuantumAllocator.hpp:72
bool Register(std::unique_ptr< WsfQuantumAllocator > aAllocatorPtr)
Definition WsfQuantumAllocator.cpp:64
WsfQuantumAllocatorFactory(WsfQuantumAllocatorFactory &&aSrc)=delete
WsfQuantumAllocatorFactory & operator=(const WsfQuantumAllocatorFactory &)=delete
WsfQuantumAllocatorFactory & operator=(WsfQuantumAllocatorFactory &&)=delete
~WsfQuantumAllocatorFactory()=default
const std::map< std::string, ut::CloneablePtr< WsfQuantumAllocator > > & Allocators() const
Definition WsfQuantumAllocator.hpp:82
static WsfQuantumAllocatorFactory * Instance()
Definition WsfQuantumAllocator.cpp:58
WsfQuantumAllocator * CreateAllocator(const std::string &allocatorName)
Definition WsfQuantumAllocator.cpp:73
WsfQuantumAllocator * Clone() override
Definition WsfQuantumAllocator.hpp:131
~WsfQuantumAllocatorGreedyIsolated() override=default
std::string AllocatorType() override
Definition WsfQuantumAllocator.hpp:130
~WsfQuantumAllocatorGreedyPriority() override=default
std::string AllocatorType() override
Definition WsfQuantumAllocator.hpp:143
WsfQuantumAllocator * Clone() override
Definition WsfQuantumAllocator.hpp:144
WsfQuantumAllocator * Clone() override
Definition WsfQuantumAllocator.hpp:168
std::string AllocatorType() override
Definition WsfQuantumAllocator.hpp:167
~WsfQuantumAllocatorGreedyProfit() override=default
WsfQuantumAllocator * Clone() override
Definition WsfQuantumAllocator.hpp:156
~WsfQuantumAllocatorGreedyValue() override=default
std::string AllocatorType() override
Definition WsfQuantumAllocator.hpp:155
~WsfQuantumAllocatorOptimalProfit() override=default
WsfQuantumAllocator * Clone() override
Definition WsfQuantumAllocator.hpp:181
std::string AllocatorType() override
Definition WsfQuantumAllocator.hpp:180
Definition WsfQuantumAllocator.hpp:92
WsfQuantumAllocator * Clone() override
Definition WsfQuantumAllocator.hpp:97
virtual bool Valid()
Definition WsfQuantumAllocator.hpp:99
~WsfQuantumAllocatorScript() override=default
void Initialize(double aSimTime, WsfScriptContext *aParentContextPtr=nullptr) override
Definition WsfQuantumAllocator.cpp:113
WsfQuantumAllocatorScript(WsfScriptContext *aContextPtr, std::string &aScriptMethod)
Definition WsfQuantumAllocator.cpp:83
std::string AllocatorType() override
Definition WsfQuantumAllocator.hpp:98
std::string AllocatorType() override
Definition WsfQuantumAllocator.hpp:118
~WsfQuantumAllocatorSimple() override=default
WsfQuantumAllocator * Clone() override
Definition WsfQuantumAllocator.hpp:119
WsfQuantumAllocatorSimple()=default
Definition WsfQuantumAllocator.hpp:36
virtual void Initialize(double aSimTime, WsfScriptContext *aParentContextPtr=nullptr)
Definition WsfQuantumAllocator.hpp:54
WsfQuantumAllocator()=default
void SetTaskType(std::string &aType)
Definition WsfQuantumAllocator.hpp:63
std::string mTaskType
Definition WsfQuantumAllocator.hpp:66
virtual WsfAssetPerception * AllocatedTo(WsfQuantumTask *aTaskPtr)
Definition WsfQuantumAllocator.cpp:37
virtual AllocationList MakeAllocations(WsfQuantumMatrix &aMatrix)=0
std::map< WsfQuantumTask *, WsfAssetPerception * > mTaskAllocationMap
Definition WsfQuantumAllocator.hpp:68
virtual std::string AllocatorType()=0
virtual ~WsfQuantumAllocator()=default
WsfQuantumAllocator & operator=(const WsfQuantumAllocator &)=default
virtual WsfQuantumTask * AllocationFor(WsfAssetPerception *aAssetPtr)
Definition WsfQuantumAllocator.cpp:26
virtual WsfQuantumAllocator * Clone()=0
std::string TaskType()
Definition WsfQuantumAllocator.hpp:61
std::map< WsfAssetPerception *, WsfQuantumTask * > mAssetAllocationMap
Definition WsfQuantumAllocator.hpp:67
bool HasTaskType()
Definition WsfQuantumAllocator.hpp:62
Definition WsfQuantumMatrix.hpp:25
Definition WsfQuantumTask.hpp:85
Definition WsfScriptContext.hpp:71
Copyrights Multiple, All Rights Reserved