<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dion Beetson on Medium]]></title>
        <description><![CDATA[Stories by Dion Beetson on Medium]]></description>
        <link>https://medium.com/@dionbeetson?source=rss-879af07f2512------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*cCqDbBnbQ7JsBqzB3_dUyA.jpeg</url>
            <title>Stories by Dion Beetson on Medium</title>
            <link>https://medium.com/@dionbeetson?source=rss-879af07f2512------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 09:47:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dionbeetson/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Where Does a Software Engineering Head Invest Its Time?]]></title>
            <link>https://medium.com/better-programming/where-does-a-head-of-software-engineering-role-invest-its-time-bd8b796d50ee?source=rss-879af07f2512------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd8b796d50ee</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[work]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[leadership]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Dion Beetson]]></dc:creator>
            <pubDate>Sat, 15 Jan 2022 01:00:02 GMT</pubDate>
            <atom:updated>2022-01-17T07:26:30.922Z</atom:updated>
            <content:encoded><![CDATA[<h4>Based on how I invested my time in leading a team of around 125 engineers across 15 teams</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QagV2HWkZAWWHCoW" /><figcaption>Photo by <a href="https://unsplash.com/@airfocus?utm_source=medium&amp;utm_medium=referral">airfocus</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The role of head of software engineering looks different across organisations. This is due to team size, organisational structure, direct managers preference, as well as supporting roles that already exist.</p><p>It’s not a 9–5 job, but that doesn’t mean it’s a 70 hour a week role either. As a role leads larger and larger teams, it is pulled in numerous directions which can be exhausting. A balance needs to be found between day-to-day work, strategic work, learning time as well as personal time.</p><p>Over the last 2 quarters, I tracked where I invested my time in leading a team of around 125 engineers over 15 teams. Why? I was curious where my time was actually being spent (outside of gut feeling), and ultimately, was investing my time in the right areas. I decided to blog about this for 2 reasons:</p><ul><li>To give individuals interested in stepping into a head of software engineering role, visibility on what the role might look like.</li><li>To start a conversation on what other aspects a head of software engineering role could also be investing time on (or not investing time on).</li></ul><h3>Accountabilities of the role</h3><p>First, let’s discuss the outcomes of a head of software engineering role. I’ve broken it down into 3 sections:</p><h4>Strategic</h4><ul><li>Ensuring a technical target state exists for teams and the wider engineering group so every individual is building towards a common north star</li><li>Ensuring guiding engineering principles are defined, collaborated on, and known, so the team can scale without chaos (or reduced chaos)</li><li>Facilitating goal setting so teams understand how they are being measured</li><li>Identifying and uplifting engineering practices to improve team effectiveness</li><li>Aligning outcomes and roadmaps between product and engineering</li><li>Defining engineering metrics to measure the success of the team</li></ul><h4>People</h4><ul><li>Guiding and coaching engineering leaders to enable them to grow within their role while delivering organisational value</li><li>Putting focus on identifying key person risks and unnecessary knowledge silos, as well as helping resolve</li><li>Ensuring the voice of all engineering team members is heard and relevant improvements are actually being implemented</li><li>Implementing succession plans where relevant and possible</li><li>Ensuring individuals are performing and being challenged in their role</li></ul><h4>Team</h4><ul><li>Implementing processes to encourage <a href="https://medium.com/@dionbeetson/empowering-software-engineering-teams-8158ac5a93e">decision-making at the team level</a>.</li><li>Streamlining recruitment and onboarding processes (and being part of the process)</li><li>Being the voice of the engineering team to the organisation (as well as coaching leaders within your team to do the same)</li><li>Identifying gaps and weaknesses within teams and driving uplift in these areas</li><li>Sponsoring or running working groups for cross-team knowledge sharing and collaboration</li><li>Working to connect and create alignment between teams, and reducing unnecessary silos</li><li>Managing escalations within your team, or coming into your team (possibly one of the most exhausting tasks if you cannot keep it under control)</li></ul><h3>Where is time spent?</h3><p>With the above breakdown defined, what does a week, and a month look like as head of software engineering in my shoes?</p><p>Disclaimer: I have ignored yearly items that come up like FY budget planning, FY formal performance conversations, etc…</p><h4>Weekly breakdown (average 40 hours)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U5Ypg3Ump341fVfzZEmYBQ.png" /></figure><ul><li>5 hours of 1:on:1s</li><li>4 hours on strategic, target state and guiding principle work items</li><li>4 hours of fire fighting</li><li>4 hours of recruitment (process and interviews)</li><li>3 hours supporting key projects (normally helping unblock, or pushing a project forward)</li><li>2 hours organising team training initiatives</li><li>2 hours collaborating on engineering improvement ideas with team members</li><li>2 hours of 1:on:1s with peers</li><li>1 hour meeting with stakeholders</li><li>1 hour managing up</li><li>1 hour self-learning</li><li>12 hours ‘other’ (see monthly breakdown below)</li></ul><p>Recruitment fluctuated quite a bit over the last 6 months, so I have taken an average. As well as this, some recruitment happens after hours for obvious reasons.</p><h4>Monthly breakdown</h4><p>The 12 hours of ‘other’ in the weekly breakdown, equates to around ~50 hours a month which is broken down below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*grZMEG1vLGkh2J7BZuQXZA.png" /></figure><ul><li>10 hours managing budgets</li><li>6 hours on group-wide communications (eg: emails/messages/meetings)</li><li>5 hours organizing working groups (eg: guilds/chapters etc)</li><li>3 hours of “blue sky” thinking</li><li>2 hours of skip meetings</li><li>2 hours reviewing and working on team goals</li><li>2 hours managing and investigating incidents</li><li>2 hours improving onboarding processes or onboarding new team members</li><li>2 hours on succession planning and resolving key people risks</li><li>2 hours attending team demos</li><li>2 hours connecting dots between people and teams to help unblock issues</li><li>2 hours supporting and talking team members down from a ledge due to troubling situations</li><li>2 hours maintaining our career framework for engineering</li><li>2 hours of mentoring sessions</li><li>2 hours supporting and working through formal performance issues</li><li>1 hour of informal 1:on:1s with ad-hoc team members to gather feedback</li><li>1 hour doing credit card expenses :-(</li><li>1 hour of company training</li></ul><h3>What have I learned from the above data?</h3><p>I spend more time on fire-fighting tasks than I want to. That time would be better spent on needle-moving initiatives. I’m now aiming to reduce this by 50% over the next 2 quarters.</p><p>I consistently spend more time managing team financials than I want to. Some of what I work on can be implemented as self-service capabilities as well as automating manual reporting that I have put off doing for years. While I expect a large time investment for FY budget planning, I aim to bring this down from 10 hours to 4 hours a month.</p><p>I need to increase the time I spend managing up. Why? Although everyone has a part to play in being the voice of software engineering, the buck does stop with my role. The more I can be out there in the organisation sharing team wins, standing up for the team, sharing the progress of key projects, sharing upcoming roadmap initiatives, the better the perception of the software engineering team across the organisation. This in addition supports in building trust between the software engineering team and the wider organisation. I aim to triple this to 3 hours a week, which includes time to coach other software leaders on how to do the same.</p><p>I would like to see a small increase in the time spent on managing engineering-wide working groups. I aim to increase this to 7 hours. There is also an opportunity to bring more software leaders into the inner circle to lead more of these groups.</p><p>Although I want to allocate more work time to self-learning, I have resided to the fact this will continue to be an after-hours investment for me. Currently, I aim to (and often do) spend 6 hours a week on self-learning initiatives outside of work.</p><p>I track everything I work on through my calendar — it’s a process that works for me. It allowed me to analyse my weekly and monthly time investments shared in this blog, however, it also enabled me to block out time for important work items like:</p><ul><li>Working on strategic projects</li><li>Self learning time</li><li>Organising working groups</li></ul><p>Without this approach, I would have found my days filled up with less important meetings. When time is blocked out, members in your organisation often respect that. If they don’t, perhaps think if you are in the right organisation.</p><p>In summary, there is a huge amount of context shifting when leading multiple teams, much different from an engineering manager role that is generally focused on leading a specific team. It is important to accept this and evolve how you work day-to-day to ensure you protect your time ruthlessly. This will improve your chances of having a more balanced role, enabling you to invest time on long-term strategic initiatives as well as self learning — ultimately reducing becoming stuck in the day-to-day weeds.</p><p>If you are in a similar software leadership role, feel free to share your opinion.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd8b796d50ee" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/where-does-a-head-of-software-engineering-role-invest-its-time-bd8b796d50ee">Where Does a Software Engineering Head Invest Its Time?</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Encouraging a culture of internal open sourcing]]></title>
            <link>https://medium.com/@dionbeetson/encouraging-a-culture-of-internal-open-sourcing-b6179041eb6c?source=rss-879af07f2512------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6179041eb6c</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Dion Beetson]]></dc:creator>
            <pubDate>Wed, 05 Jan 2022 11:50:55 GMT</pubDate>
            <atom:updated>2022-01-05T11:50:55.386Z</atom:updated>
            <content:encoded><![CDATA[<p>Over the last 5 or so years I have been intrigued by the challenges of implementing and encouraging a culture of internal open sourcing. A process that appears trivial to implement, has on occasions puzzled me as to why it is more challenging than expected to gain adoption within a corporate environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L_FZ2k6nrUjv7ZD1V3IgyA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@bamagal">https://unsplash.com/@bamagal</a></figcaption></figure><h3>The challenges of internal open sourcing</h3><p>Internal open sourcing enables any team to contribute code to a codebase they don’t officially own or are accountable for in production. It allows teams that have dependencies on other teams platforms to support unblocking themselves, and in doing so, enables cross team knowledge sharing to flourish. It seems like a win-win situation right? So why is it hard to implement? The challenges I have noticed include:</p><ol><li>Teams are too protective of their products/platforms and don’t trust other teams to commit code, thus don’t encourage it and build walls — this is not the mindset we aspire to encourage.</li><li>Teams view platforms in others team as too complicated to up-skill in, and thus believe it’s easier to code a work around in their own platform, or in some instances defer a value-add feature to the backlog.</li><li>Teams don’t understand how to contribute code outside of their teams codebases, thus don’t.</li></ol><p>The above encourage a rather dismal situation which can result in greater silo’ing between software teams.</p><h3>Let’s tackle these three problems</h3><p>As a software engineering leader you need to be candidate. If your goal is to introduce a culture of internal open sourcing, let it be known, explain the reasons why, and start taking your teams through the journey. If software engineers understand the why, they are more likely to help drive its success, or at least disagree and commit.</p><h3>1. Teams are too protective</h3><p>For long-lived teams, clear ownership boundaries are important. Teams (and the organisation) need to know who is ultimately accountable for the quality and stability of a products codebase at all times. In open source projects this team of developers is referred to as the core maintainers. While it is important for engineering teams to have clear end-to-end ownership boundaries, the side effect can result in teams becoming overly protective of their codebase. They believe they should be the only ones writing code.</p><blockquote>This doesn’t scale</blockquote><p>I’ve experienced this defensive mindset occurring due to 2 primary reasons. The receiving team has a higher quality bar that isn’t being met by external contributors, and secondly, the receiving team believes a level of ownership is being taken away from them (against their will) where they will no longer be able to control the quality of their platform.</p><p>At its core, it’s often a result of a lack of documented process around how external code contributions should be managed within teams, as well as an agreed upon ownership model based on an internal open sourcing culture.</p><p>To mitigate these challenges, teams should look to:</p><ul><li>Document a process on how code can be contributed to their teams codebase. <em>For example: what is the branching strategy? When should an external team inform the team of an incoming commit (eg: at MR time, or prior to branching)?</em></li><li>Define a public facing DoD (Definition of Done). <em>This can represent what level of testing is expected, is performance testing expected, what documentation is expected (eg: release notes, architectural diagrams) etc.</em></li><li>Define a process for who will deploy external contributions into production. <em>Is this the receiving team or the contributing team?</em></li><li>Define who will support the code once in production. <em>Generally this is the receiving team, but options can exist for the contributing team to support for a specific time boxed warranty period</em></li><li>Define SLAs on external code review for merge requests. <em>Teams should have alerts in place for new branches being created, as well as ageing reports on open branches for review/deploy.</em></li></ul><h3>2. It’s too complicated to contribute</h3><p>A symptom of point 1 above, is receiving teams are not providing adequate documentation or processes for external teams to contribute. Thus, are not encouraging external teams to fix a bug, or add a new capability. Because teams are not proactively encouraging internal open sourcing, external teams don’t feel a sense of invitation to collaborate.</p><p>To mitigate these challenges, teams should look to:</p><ul><li>Run technical talks about their products and platforms, talking through key functionality, upcoming roadmap, ways of working within their team</li><li>Setup onboarding sessions to coach external software engineers on how to contribute to their platform in the future</li><li>Generate as much self-service documentation as it makes sense to, to allow external teams to onboard without taking time away from the receiving team</li><li>When an external team requests a new backlog item, the receiving team should offer to support that team in implementing the capability themselves. Ensuring that it aligns to their team standards, and architectural target state</li></ul><h3>3. Teams don’t know how to contribute</h3><p>To a software engineer, contributing to another teams codebase can almost feel like starting a new role in a new team. What does this team expect from me as a developer working on their codebase? The good news is, by implementing many of the points mentioned above, it will help mitigate the hurdles external contributors can face.</p><p><a href="https://gitlab.com/">GitLab.com</a>, an organisation well known for their culture of open sourcing, has a great write up on their “<a href="https://about.gitlab.com/job-families/expert/merge-request-coach/">Merge Request Coach</a>”. The Merge Request Coach is a role members within a receiving team play, to help encourage and ensure external code contributions are either merged, provided feedback on, or closed if not relevant.</p><h3>Bonus idea to kickstart internal open sourcing</h3><p>If you are looking to kick start internal open sourcing within your team, I’ve had success running the below event which attempts to gamify it. The event runs as below:</p><ul><li>Time box a 6 week period (timeframe can change based on what works for your team)</li></ul><h4>Week 1–2:</h4><ul><li>Brief the team on the event (2 weeks is primarily to allow the teams the time to self prepare)</li><li>Announce event challenge: Every external code contribution your team gives or receives will earn points. The team with the most points at the end of the event wins X [choose an exciting prize!]</li><li>5 points to both teams for an internal open source solution conversation</li><li>5 points for contributing to another teams codebase through a MR</li><li>5 points to both teams involved in getting a MR integrated into ain branch</li></ul><h4>Week 3–4:</h4><ul><li>Allow 2 weeks for teams to initiate and run discussions between themselves on external code contribution ideas</li></ul><h4>Week 5–6:</h4><ul><li>Allow 2 weeks for teams to contribute MRs from the solution conversations above (or other new ideas)</li><li>Run a leaderboard that you update twice a week</li><li>Weeks 3–6 don’t necessarily have to be diligently enforced. You can reward teams that have been able to race ahead, but the structure does help teams plan out their sprints (if they are running sprints).</li></ul><p>In one of the events I’ve run, our teams were able to take part in and complete over 15 internal open source contributions. These included simple bug fixes, to adding new capabilities to existing APIs (with version bumps of course). This event enabled numerous conversations between teams which helped build trusting connections between individuals. The real value however, was teams began to understand how under prepared they were for receiving internal code commits from other teams. With this realisation, teams were able to begin updating and defining their internal contribution documentation for their platforms.</p><h3>In summary</h3><p>Building a culture of internal open sourcing isn’t always a light switch you can turn on. It’s a mindset within the team that leaders need to invest time in cultivating and coaching. Although in some organisations it can be challenging to implement, a culture of internal open sourcing will better enable teams to unblock themselves in cross team dependant projects.</p><p>If you found this post useful, check out my book “<a href="https://leadingsoftwareteamswithcontextnotcontrol.com/"><strong>Leading software teams with context, not control</strong></a>” on <a href="https://leanpub.com/leading-software-teams-with-context-not-control">Leanpub</a> as well as <a href="https://leadingsoftwareteamswithcontextnotcontrol.com/">other online stores</a> like Amazon in eBook or print format.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6179041eb6c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Empowering software engineering teams]]></title>
            <link>https://medium.com/@dionbeetson/empowering-software-engineering-teams-8158ac5a93e?source=rss-879af07f2512------2</link>
            <guid isPermaLink="false">https://medium.com/p/8158ac5a93e</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-team]]></category>
            <category><![CDATA[software-leadership]]></category>
            <dc:creator><![CDATA[Dion Beetson]]></dc:creator>
            <pubDate>Sat, 01 Jan 2022 08:50:06 GMT</pubDate>
            <atom:updated>2022-01-09T23:11:24.912Z</atom:updated>
            <content:encoded><![CDATA[<p>Software engineering is a creative role. As software leaders we aspire to hire great engineers who can solve complex problems. How can we craft a culture, and implement practices to support empowering software teams and pushing as much decision making down to them as it makes sense to do so? Below are 10 approaches that can be utilised to accomplish empowering software teams.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2X0tBks8a3AfdVl2qFeiEg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@zeynorjane">https://unsplash.com/@zeynorjane</a></figcaption></figure><h3>1. Be clear your objective is to empower engineering teams</h3><p>If empowering engineering teams is something new to the group, you need to articulate this change. Teams need to be aware what is changing, and why it is changing. Most importantly they must understand the value this change will bring to their role.</p><p>In small organisations, with well formed teams, this could be orchestrated through a simple team meeting to explain the change and being open to feedback. For example:</p><blockquote><em>“Our objective is to ensure teams own as much decision making as possible as they are closest to their products and platforms. We are going to achieve this by bringing outcomes to teams, and empowering you all to own the solution. Hold us (leadership roles) accountable when you notice this is not occurring.”</em></blockquote><p>However, in a larger organisation with many software teams, momentum often needs to be built before wide spread change. It can be beneficial to have early conversations with engineering leaders to understand thoughts/feedback, to locate early supporters, as well as to resolve major concerns before wider communication to the group. This will ensure you have enough support from your leaders regarding the change. You can also start the roll out with a small subset of teams who are open to the change as way to build early momentum and ensure a few wins on the board.</p><h3>2. Bring outcomes to teams, not solutions</h3><p>If you’re an expert in your domain, or a seasoned employee, it can often seem easier to unintentionally direct the team on exactly what needs to be done to solve a problem. For example:</p><blockquote><em>“We need to support Bitcoin as a new payment method. You can implement it into [platform x], just extend [component x] and deploy it into our K8s stack. We can re-use the existing reporting features, and the UI should be pretty similar.”</em></blockquote><p>You might have implemented this capability before and believe you’re fast tracking the teams progress. However, instead you could be limiting the teams problem solving potential, and encouraging a culture where they will rely on you as their manager to always bring solutions. This results in an increased risk of losing high performing engineers, and creating a team of doers, not thinkers.</p><p>Instead, practice articulating the problem in the eyes of the customer, and let the team solution. For example:</p><blockquote><em>“We need to support Bitcoin as a new payment method for our customers. As a business we need to convert all transactions into FIAT (not hold Bitcoin) so it integrates into existing workflows. We have a timeframe of 2 months to deploy a PoC.”</em></blockquote><p>After this, pause and let the team digest. Then ask:</p><blockquote><em>“What questions do you have? What isn’t clear? What more information do you think you need?”</em></blockquote><p>If you have solved this problem before, state that. For example:</p><blockquote><em>“I’ve solved this problem in the past, which may or may not still be relevant. I’m happy to share that experience with the team now or after you have had time to do your own research.”</em></blockquote><p>By explaining the situation in this way, you are setting context and boundaries, and allowing the team to dive into the implementation specifics. The benefits here are you are empowering the software teams to own a solution, and investigate options that you may not have even envisaged. You are not constraining the teams thinking. As a result, they may choose to implement this capability into a new standalone service to ensure functionality is more maintainable and more testable. They may deploy into a serverless architecture instead of K8s to simplify architecture and reduce running costs.</p><h3>3. Coach stakeholders on how to bring outcomes, not solutions</h3><p>An extension of the above that I’ve experienced many times in the past which is harder to solve, is business experts coming to the team with a pre-defined solution. These are individuals outside of your direct line of control. They may be product managers, pre-sales engineers, or knowledgable domain experts. It can sometimes be presented as</p><blockquote>We signed up a new [customer X], and we need [API X] to be adjusted to have new fields [a, b &amp; c] to satisfy their requirements. We have told them we can implement it before they integrate in 2 months time.</blockquote><p>These requests are typically not malicious in nature, however, can be problematic and time consuming to solve. Stakeholders tend to do this because they genuinely feel like they know the right solution and are saving the engineering team time. However, what they are missing, is software engineering teams can take a holistic view and more than likely solve the problem using an alternative approach. This alternative approach could be made relevant to all customers, or be solved in a more maintainable way to reduce technical debt.</p><p>Below are 3 approaches to coach stakeholders on how to bring outcomes to teams, instead of solutions:</p><ul><li>Invest the time to run training sessions to coach stakeholders on how to bring outcomes to teams (coupled with the 2 points below) will create significant process. Explain how to define an outcome, the benefits it has to the team, and importantly to them as stakeholders. <em>I’ve found that you need to run these sessions continuously throughout the year, especially when you identify stakeholders are reverting back to old habits.</em></li><li>Template how to bring outcomes to teams. Bringing an outcome to a team requires a stakeholder to think about the problem they are trying to solve. By defining a template on how to document this in a succinct way can allow a stakeholder to quickly define without over thinking.</li><li>Document a DoR (Definition of Ready). A well documented DoR can clearly articulates how an item of work should be presented to a team and in a format the team can work with.</li></ul><h3>4. Define what success looks like (how will the team be measured)</h3><p>With more empowerment, comes more responsibility. This requires teams to understand how they will be measured on the success of their work. This could include a combination of:</p><ul><li>A specific scalability metric is met (eg: TPS for an endpoint or service)</li><li>The solution brings the team 1 step closer to an architectural target state</li><li>The solution must be less than a specific monthly cloud hosting cost</li><li>It must meet a specific reliability or redundancy measure (eg: can support 1 datacenter going offline)</li><li>The work needs to be delivered within a specific timeframe</li></ul><p>Whichever measures you choose, it is important your measures are defined and communicated to the team who will be owning this work.</p><h3>5. Be upfront and transparent on boundaries</h3><p>Boundaries and constraints always exist, it is your role to communicate them to the team early. Delaying will almost always cost team cycles in re-solutioning, or re-developing. Boundaries to think about could include:</p><ul><li>Are there specific technologies that need to be used?</li><li>Are their resource constraints in another team that need to be worked around?</li><li>Are there specific internal/external platform APIs this project needs to interface with that have rate limited thresholds?</li><li>Is a specific platform being deprecated in the near future that teams need to be aware of, so as to not make unnecessary improvements to?</li><li>Is this a short term solution that will be replaced in the near term, hence not requiring teams to architect and implement a long lived solution?</li></ul><h3>6. Be transparent on known timeframes</h3><p>There is nothing worse than empowering a software team to solve a problem, only as a leader to simply make the decision for them 2 weeks later when they have missed an unknown deadline. This will almost certainly erode trust. As a leader, it rests on you to clearly, let me repeat, clearly, articulate any known deadlines that exist when briefing your team. You should communicate if a deadline is a hard deadline (eg: marketing campaign launch), or soft deadline (eg: an internal deadline that could possibly shift).</p><p>Even if deadlines seems too short, still communicate them to the team. This doesn’t need to represent a negative, it can be used to allow the team to solve a short term tactical outcome to meet a deadline that cannot move, while working on a longer term solution without the added time pressure.</p><h3>7. Evangelise and drive end-to-end ownership</h3><p>Ownership of a product, a user journey, a platform, a business domain is in my view fundamental to empowering a team. A team needs to be accountable for as much of the SDLC as makes sense in a specific organisation. This represents teams owning their software architecture to development, their testing to deployment, as well as supporting their products in production.</p><p>When teams have end-to-end ownership, it encourages them to continually improve their platforms and processes as they know the buck stops with them. They are the ones who will be woken up at 2am if the application crashes, they will be the ones who will have to resolve technical debt due to a decision made in the solutioning phase, they will be the team spending effort manually deploying an application. These risks all work as motivators to deliver scalable, maintainable and reliable products into production.</p><h3>8. Provide teams the time to solution the problem</h3><p>Whether it’s the team, a technical lead, a stakeholder, an architect, or a CTO, someone needs to invest time to understand the problem, explore possible solutions and devise a plan. I’ve witnessed organisations develop a perception that teams spending time solutioning to a problem is too disruptive, and would rather a role outside of the team do this work — leaving the team to “just cut code”. This however, goes against empowering a software team to own their products and platforms. When teams solution together, they understand their products better, which makes future solutioning more efficient. It also encourages knowledge to be spread amongst the team, not held with a specific individual. Approaches to provide teams time to solution can include:</p><ul><li>Encourage teams to leverage spikes. Spikes are used to provide time for teams to look at multiple solutions, explore in more detail and ultimately arrive at a more accurate next step and work effort estimate. Word of caution, in general, a spike should avoid creating additional spikes for teams. A spikes output should be a work effort estimate. Spikes should also be time boxed.</li><li>Encourage Proof of Concepts when there are too many unknowns. It is advisable to time box PoCs as well as having clear outcomes defined.</li><li>Run team solutioning sessions (or solution review sessions), and encourage team members to prepare by bringing potential solution ideas to the group</li></ul><h3>9. Ensure teams have the right skills and roles</h3><p>Although any team can be empowered, to setup for success you need to ensure the team has the right skills and experience. Every team could have a different makeup of skills and experience based on their remit and the organisation. Areas to think about include:</p><ul><li>Do you have the right balance of graduate to senior engineers within the team?</li><li>Do you have the right skills needed within the team to satisfy QA as part of DoD?</li><li>Do you need a dedicated role for cloud infrastructure in the team?</li><li>Do you have experienced individuals who can guide in complex software architecture decisions? This doesn’t have to be a software architect, perhaps a principal engineer or staff engineer.</li><li>Do you have product management or product ownership within the team This can be matrixed in to avoid organisational structure changes.</li></ul><h3>10. Keep your hand on the pulse</h3><p>Ultimately as a software engineering leader you are accountable for your teams effectiveness and delivery. Just because you are empowering teams, doesn’t mean you can sit back. Your role is to be aware on when your team is spinning wheels, becoming distracted, or solutioning to the wrong problem. Your role exists to jump in and provide guidance, be there to help unblock, be a decision maker when teams are in deadlock, or sometimes just connect the right people within the organisation. Below are 5 techniques that can be utilised:</p><ul><li><strong>1:on:1s:</strong> You will often hear about blockers or frustrations in 1:on:1s without even asking. However, if not, always probe just incase your team member is holding back. Remember, run 1:on:1s frequently, at least every fortnight.</li><li><strong>Attend demos:</strong> End of sprint demos, or product demos in general are a great way to stay in the loop in a casual environment. If teams are not running demos — encourage them to!</li><li><strong>Show a genuine interest: </strong>Being a leader that is genuinely interested in your teams projects (typically by just asking questions), will craft an environment where members will more openly discuss what is going on in their day-to-day.</li><li><strong>Follow relevant JIRA boards:</strong> Replace “JIRA” with whichever application the team uses. Following status updates on JIRA can provide you great intel on project statuses in an asynchronous way.</li><li><strong>Follow the documentation:</strong> Most teams use a wiki or similar, so following the relevant pages/spaces you will often be able to stay up to date on high level solutions and architectures. If you aren’t noticing documentation, this may be a red flag to begin asking questions</li></ul><p>Building a culture of empowering software engineering teams is a rewarding opportunity. It will allow you to scale your team and your role as a leader within the organisation. If you found this post useful, check out my book “<a href="https://leadingsoftwareteamswithcontextnotcontrol.com/"><strong>Leading software teams with context, not control</strong></a>” on <a href="https://leanpub.com/leading-software-teams-with-context-not-control">Leanpub</a> as well as <a href="https://leadingsoftwareteamswithcontextnotcontrol.com/">other online stores</a> like Amazon in eBook or print format.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8158ac5a93e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How I wrote my first non-fiction software leadership book]]></title>
            <link>https://medium.com/@dionbeetson/how-i-wrote-my-first-non-fiction-software-leadership-book-93386303873d?source=rss-879af07f2512------2</link>
            <guid isPermaLink="false">https://medium.com/p/93386303873d</guid>
            <category><![CDATA[authors]]></category>
            <category><![CDATA[software-leadership]]></category>
            <category><![CDATA[writing]]></category>
            <dc:creator><![CDATA[Dion Beetson]]></dc:creator>
            <pubDate>Wed, 23 Sep 2020 13:00:00 GMT</pubDate>
            <atom:updated>2022-02-20T02:19:55.405Z</atom:updated>
            <content:encoded><![CDATA[<p>Around 10 months ago I began work on a new side project to author a book on software engineering leadership. I was confident I went in eyes wide open, knowing it wouldn’t be an easy undertaking, but in the end it was considerably harder than I had even imagined. As a retrospective I wrote this post to share the 18 (yes way too many) stages I took when writing my first book “ <a href="https://leadingsoftwareteamswithcontextnotcontrol.com/">Leading Software Teams with Context, Not Control</a>”. In hindsight, I could have easily reduced the amount of stages I went through, however, you always learn considerably the first time you do anything. I’m sure there are many more effective ways to author a book, but as a first time author this is the approach I took (which did evolve as I worked through the project). So yes, you probably shouldn’t use this as a definitive guide if starting out on writing your own first book!</p><p><strong>Here is a visualisation of % of book completed against hours invested:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*0tkkTcYvyD5rUGkj.png" /></figure><h3>Where it all began…</h3><p>It all started with a relatively innocent Google Doc working a few hours every few days, and ended with a GitHub repo with each chapter in Markdown, a bunch of CSS and an automated pipeline to generate epub, mobi and PDF file outputs for each of the platform I was to publish the book to. Oh, and dedicating 3 hours every night to get this project over the line.</p><h3>Part 1 — Defining the chapters</h3><p><strong>Completed:</strong> 1%</p><p><strong>Time taken:</strong> 12 hours</p><p>I spent the first 12 hours defining the structure of the book. This involved breaking it down into 20 chapters focused around aspects I believed were important in relation to software engineering leadership. Within each chapter I listed a small amount of dot points on topics I planned to focus on.</p><p><strong>Lesson learned:</strong> Start with a GitHub repository and use Markdown from the very beginning! Also set yourself up on <a href="https://leanpub.com/">https://leanpub.com/</a> — it’s an amazing self publishing platform that encourages you to publish early and publish often.</p><h3>Part 2 — Defining the chapter structure</h3><p><strong>Completed:</strong> 2%</p><p><strong>Time taken:</strong> 6–7 hours</p><p>After analysing a selection of well written books I had previously read, it was important for me to ensure a consistent structure and flow throughout each chapter for the user. I defined a 5 part structure for each chapter to follow. You can think of these as top level headings within the chapter. However, you will notice in my book these headings don’t exist, yet each chapter follows that structure :</p><ul><li><strong>What:</strong> An overview of the topic the chapter was about</li><li><strong>Why:</strong> An explain why this topic is important in relation to software engineering leadership</li><li><strong>Story (optional):</strong> An example or story I could share from my own experiences</li><li><strong>How:</strong> Approaches on how to implement this topic within your own software engineering team</li><li><strong>TL;DR:</strong> 3 important takeaways from the chapter</li></ul><p><strong>Lesson learned:</strong> After initially spending a few hours on this, I ended up reviewing 6 of the best books I had read and defined a structure that took the best of each.</p><h3>Part 3 — Brainstorming each chapter</h3><p><strong>Completed:</strong> 5%</p><p><strong>Time taken:</strong> 40 hours</p><p>At this point in time I began drafting out each chapter as a set of more refined bullet points that aligned to each of the structure headings listed above. Spending around 2 hours on each chapter allowed me to get as many details as I could out of my brain and into the Google doc as quickly as possible.</p><p><strong>Lesson learned:</strong> Choose your language up front and stick to it. I started with English (AUS), then moved to English (US), then later converted it to English (British). Wasn’t the best use of time…</p><h3>Part 4 — Drafting out each chapter</h3><p><strong>Completed:</strong> 15%</p><p><strong>Time taken:</strong> 80 hours</p><p>With each chapter now a set of refined bullet points ranging from 1–4 pages, this part involved bringing each chapter together to represent a roughly formed draft. Each chapter was partially readable and had a level of flow to it. This took around 3–5 hours per chapter. At this point in time I had 18 chapters that were coming together nicely, but had also outlined 12 additional possible chapters as notes down the bottom of the document — which was clearly too many. I had also begun reordering chapters to improve the narrative of the book.</p><h3>Intermission</h3><p>At around this point in time my wife and I discovered we had a baby due in 7 months. It was at this point I began to realise I needed to finish this project before then — I now had a deadline. But… as it was still a fair way out, I didn’t take the deadline as seriously as I should have. However, in a few months time I would.</p><p><strong>Lesson learned:</strong> Set yourself a deadline for the project, as well as a deadline for each smaller milestone. Then commit ‘X’ number of hours per week to dedicate to authoring your book.</p><h3>Part 5 — Adding in data points and examples</h3><p><strong>Completed:</strong> 24%</p><p><strong>Time taken:</strong> 80 hours</p><p>It was now the time to bring hard facts, data points and career examples into each chapter to back up the claims I was making. For some chapters this was around reinforcing claims I was making from experiences I had had throughout my career. For others it was crawling through my twitter history that I use to share articles I have found value out of over the years, to find the specific content that helped support my books content. This took around 4 hours per chapter.</p><p><strong>Lesson learned:</strong> Correctly collate and document your books references at this point, not at the the end of the project.</p><h3>Part 6 — Research break</h3><p><strong>Completed:</strong> 25%</p><p><strong>Time taken:</strong> 2 hours</p><p>At this point as I had the perception I was progressing well. I began to research more tips and tricks around publishing a non-fiction book. One valuable insight I gathered was when writing a book you should start of by draft 4–5 ‘fake reviews’ that you would theoretically like people write about your book once they have read it. This is done in an effort to continually refer back to so you can keep yourself honest in regards to the content you are authoring into your book. You want to write positive and negative reviews. I came up with:</p><h3>A quick break</h3><p>At this point I was feeling rather positive. I had written most of the book, and had convincing data to back up my content. All that was left was to do a few rounds of clean up and generate an ebook for publishing. Easy right? At this point I thought I was about 65% through the project, in reality I was about 20% through.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*DEvu5vetSS-pPd1z.png" /></figure><h3>Part 7 — Clean up stage 1</h3><p><strong>Completed:</strong> 40%</p><p><strong>Time taken:</strong> 125 hours</p><p>Starting from chapter 1, I began working through each chapter to fix up grammar, improve the flow of sentences, include additional detail where needed and deleted content that was just unnecessary. The first 6 chapters within the book were sequential, whereas the subsequent chapters were all independent and could be read in the readers preferred order. I invested a lot of time attempting to make chapters 1–6 to flow seamlessly so as to not confuse the reader. I spent over 16 hours cleaning up just the first 6 chapters, rewriting, and rewriting more. This is when I hit my first wall, when I began to realise I was definitely not 65% through and more like 20% through authoring this book. I now realised I needed a more structured time investment to progress. I began to dedicate 3 hours, 3 nights a week on this project. Chapters 7 onwards were slightly easier to clean up — but still a considerable amount of work.</p><p><strong>Lesson learned:</strong> Set up a schedule for time investment earlier on that you can commit to.</p><h3>Part 8 — Clean up stage 2</h3><p><strong>Completed:</strong> 55%</p><p><strong>Time taken:</strong> 98 hours</p><p>The book was now at around 55,000 words and my Google doc was beginning to struggle from a performance perspective. But I persisted with it (for the moment). Starting again from chapter 1, I began a second pass on improving grammar, removing duplication and general tidy up. I eventually had to condense my first 6 chapters into 5 chapters as it just wasn’t working the way I had originally authored it. This took up a lot of time and was mentally challenging. I also ended up deleting an entire chapter (chapter 18 to be specific) as the book was becoming too lengthy and that chapter added the least value. Again, mentally very hard to delete large blocks of content that I had already sunk many hours into. However, in the end it was the correct decision. At this point I also made the hard decision to remove all other ‘potential chapters’ that I still had as notes down the bottom of the book. Maybe this will appear in my second book sometime in the future. I spent around 2 hours on each chapter, and again more time refining chapters 1–6.</p><p><strong>Lesson learned:</strong> It is entirely ok to delete large amounts of content within your book if it will lead to be a better experience for your readers — but yes it will be mentally challenging to do so.</p><h3>Part 9 — Defining and setting up ‘the matrix’</h3><p><strong>Completed:</strong> 57%</p><p><strong>Time taken:</strong> 12 hours</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*OpoJP_Tu3ztrDS-e.png" /></figure><p>I was now spending 3 hours each night, 6 days a week authoring this book. I had around 8 weeks to go until the baby deadline would arrive, and I was running very short on time. The few people I was talking about my book with to I would confidently let them know I was tracking well, however, personally I was not so sure. Throughout the previous month, I was furthering my investigation into what makes a great non-fiction book. Again, something in hindsight I should have undertaken at the start of the project. I came to the conclusion that I needed to define 8 measures each chapter should satisfy for the reader. They were:</p><ul><li>Clearly state the problem the chapter was written to provide a solution for</li><li>Provide an actual solution the reader could adopt within their own team/role</li><li>Include two data point facts in each chapter</li><li>Include 1 example or personal story</li><li>Relate to the readers experience to connect with them throughout the book</li><li>Share something important the reader probably wasn’t aware of</li><li>Include a call to action and/or summary at the end of each chapter</li><li>Make claims that are as strong as they can be made without becoming false</li></ul><p>I put together a simple matrix (eg: table), with those 8 measures above as columns and each chapter as rows so I could track which measures I had accomplished.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*MbBbBeO8W9MTeY1h.png" /></figure><p><strong>Lessons learned:</strong> Spend a week or so researching what makes a great non-fiction book before you begin. As well as this create your own matrix to help keep you honest to your readers in each chapter.</p><h3>Part 10 — The matrix phase 1</h3><p><strong>Completed:</strong> 65%</p><p><strong>Time taken:</strong> 69 hours</p><p>With the matrix defined, I worked through each chapter ensuring it tackled each measure, and where it might not be relevant marked it as N/A. In this phase, I skipped over any item that had a potential to require considerable time investment. I spent around 2–4 hours on each chapter.</p><h3>Part 11 — The matrix phase 2</h3><p><strong>Completed:</strong> 70%</p><p><strong>Time taken:</strong> 46 hours</p><p>This part involved going back through each chapter and filling in the missing gaps within the matrix. Although some chapters took longer than others, I averaged 2 hours per chapter. At this point in the project, I began to feel as though I had a book that tackled the key problems I was aiming to.</p><h3>Part 12 — Crafting the title</h3><p><strong>Completed:</strong> 75%</p><p><strong>Time taken:</strong> 12 hours</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*W-7VyZ54EYyBBRJ1.png" /></figure><p>I knew I needed to proof read the book again (at least once or twice more), however, I needed a break. I had hit a writing exhaustion wall. It was now time to come up with a title.. Easy right? Ok probably easier than finding a name for a new startup with an available domain name. But still hard!</p><p>I wanted something I could relate to, that would catch the interest of a potential reader. After around 12 hours of brainstorming many different titles and variations, as well as gathering feedback from a few individuals within my network, I settled on “Leading software teams with context, not control”. Context over control was a chapter in my book, and it is a leadership methodology I have followed for many years — so it seemed quite applicable.</p><p><strong>Lesson learned:</strong> Procrastinate on a title until you have one that resonates well with you.</p><h3>Part 13 — Authoring surrounding chapters</h3><p><strong>Completed:</strong> 82%</p><p><strong>Time taken:</strong> 17 hours</p><p>With a title in place, I now needed to author the surrounding chapters within the book which included:</p><ul><li>About the author</li><li>Why the book was written</li><li>Acknowledgments</li><li>Glossary</li><li>Notes</li></ul><p><strong>Lesson learned:</strong> References are still as hard and time consuming to create as they were in the university assignment days.</p><h3>Part 14 — The final proof read</h3><p><strong>Completed:</strong> 90%</p><p><strong>Time taken:</strong> 56 hours</p><p>With the book mostly complete (90%), it was time for the final proof read. I spent just over 2 hours reading through each chapter very slowly, in large font to ensure it flowed well for the reader. I also began using <a href="http://grammarly.com/">grammarly.com</a>, a great tool to improve grammar and sentence structure.</p><p><strong>Lesson learned:</strong> Start using the <a href="http://grammarly.com/">grammarly.com</a> app from the very beginning.</p><h3>Part 15 — Creating a cover page</h3><p><strong>Completed:</strong> 91%</p><p><strong>Time taken:</strong> 28 hours</p><p>What can I say, I spent too much time finding the ‘right’ shade of blue. I used a great online tool called figma.com to design the cover page. I ended up creating 4 cover pages for the different stores I published the book to.</p><p><strong>Lesson learned:</strong> Probably a good idea to outsource this to a freelancer.</p><h3>Part 16 — Building the epub files</h3><p><strong>Completed:</strong> 94%</p><p><strong>Time taken:</strong> 46 hours</p><p>I had now written the ebook, how hard could it be to create a beautifully formatted ebook? Well first of all if you want any control over your styling you best be using markdown files with CSS. That was 16 hours lost converting my entire google doc to markdown. Then you need a tool to convert markdown to epub. I ended up using pandoc.org — a very handy tool. If you are planning on using pandoc, be sure to read the documentation carefully and add in all the correct CLI arguments to create page numbers, TOC, title page etc… It can’t do everything, but it can do enough.</p><p>After spending a many days experimenting with different approaches, I ended up scripting what I was doing into a repeatable process to generate epub files on the go. I then spent a few days crafting CSS to carefully format the epub into a well presented and readable format.</p><p>This was definitely more time consuming that I had expected, but it was a nice change to jump back into the code.</p><p><strong>Lesson learned:</strong> Start with markdown, accept that large tables are near impossible to format well in ebooks, and finally script your build process for the different ebook formats and sample formats. I regenerated my ebook over 200 times while testing out formatting.</p><h3>Part 17 — Professional copy editing</h3><p><strong>Completed:</strong> 97%</p><p><strong>Time taken:</strong> 38 hours</p><p><strong>Cost:</strong> Approximately $1,600</p><p>Every book requires professional copy editing to perfect it for your readers. There are so many options out there, in the end used the <a href="http://reedsy.com/">reedsy.com</a> marketplace to find a great copy editor.</p><p><strong>Lesson learned:</strong> Pre-book in a copy editor, as it took around 4 weeks for me to fit into their schedule once agreeing on terms.</p><h3>Part 18 — Submitting the ebook</h3><p><strong>Completed:</strong> 98%</p><p><strong>Time taken:</strong> 7 hours</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*Apujo_LM61ivd1Oj.png" /></figure><p>It was now time to publish my ebook to the relevant online stores. I started with <a href="http://leanpub.com/">LeanPub.com</a>, then <a href="http://amazon.com/">Amazon.com</a>, Apple books &amp; finally Google play. It took some time setting up relevant accounts, tax information, author bios etc…</p><p><strong>Lesson learned:</strong> Upload your epub to <a href="http://amazon.com/">Amazon.com</a>, then download the generated mobi file and use that for platforms like LeanPub. Also mobi will require some minor CSS additions for formatting.</p><h3>Part 19 — Preparing for print on KDP</h3><p><strong>Completed:</strong> 100%</p><p><strong>Time taken:</strong> 19 hours</p><p>After spending nearly a year authoring my book into an ebook format, why not publish a paperback as well. Amazon KDP (Kindle Direct Publishing) is a great self publishing platform for on-demand printing of paperback books. You need to upload a print ready PDF, with a cover. To do this I converted the epub file into a PDF using ‘ebook-convert’ which is part of the <a href="https://manual.calibre-ebook.com/generated/en/ebook-convert.html">calibre application</a>. I also needed to inject in additional CSS for print specific styles. Preparing for print is a rather simple process using KFP, the only downside is a 72 hours approval window after each new upload. However, KDP scans your PDF for formatting issues and alerts you to fix before publishing. Once published you can order cheap author copies for your review.</p><p>That’s it, 10 months, 792 hours later, I had a 23 chapter book with 349 pages, 68,000 words available in eBook and print formats available globally. It was quite a journey, where at multiple points I thought about pulling the pin, but eventually pushed through and completed the book (unlike many other side projects I still have ‘in-progress’). All that is left is to promote the book :-)</p><h3>What were my final take aways?</h3><ul><li>Authoring a book is harder and more time consuming than you will ever realise. Go in prepared for this realisation.</li><li>Carve out consistent time out each week, break your project into smaller milestones and set deadlines for each.</li><li>Research aspects of successful books, and draft a plan before starting. Still evolve it as you go though.</li><li>You will learn to correctly spell some of those words that you used to always use spell checker for.</li><li>Automate you ebook and print generation. I probably regenerated my ebook over 200 times, automating the process will be worth your time investment.</li></ul><p>If you are interested in a copy of my book “<a href="https://leadingsoftwareteamswithcontextnotcontrol.com/">Leading Software Teams with Context, Not Control</a>” you can purchase on <a href="https://leadingsoftwareteamswithcontextnotcontrol.com/">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93386303873d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Balancing technical uplift with product development]]></title>
            <link>https://medium.com/@dionbeetson/balancing-technical-uplift-with-product-development-61fa6391f8a7?source=rss-879af07f2512------2</link>
            <guid isPermaLink="false">https://medium.com/p/61fa6391f8a7</guid>
            <category><![CDATA[agile]]></category>
            <category><![CDATA[software-leadership]]></category>
            <dc:creator><![CDATA[Dion Beetson]]></dc:creator>
            <pubDate>Thu, 30 Jul 2020 13:00:00 GMT</pubDate>
            <atom:updated>2022-01-10T03:00:03.882Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sQcCC8-qBy43gf8_tjO2kg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@hautier">https://unsplash.com/@hautier</a></figcaption></figure><p>It is important to note that when referring to technical uplift, it is not specifically referring to only technical debt. Although technical uplift sometimes needs to occur due to long standing technical debt, technical debt is the result of specific historical decisions knowingly being made that results in a platform becoming more expensive to maintain or potentially scale. Some technical debt is bad debt, in that the cost of repaying those decisions becomes exponentially higher as time passes. Whereas other debt is more easily repaid at a later point in time. A crucial call out to understand is that new technical debt is accepted and taken on as a team, it isn’t forced onto the team by stakeholders — that is not a software engineering team culture you want to let evolve.</p><h3>Why the decision is not binary</h3><p>Balancing technical uplift and product feature build rarely needs to be a binary decision. From a software developer’s perspective, they work day in and day out within the platform’s codebase. They understand many of the pain points that result in scalability issues, areas of the code that every software developer avoids working on because the slightest change can cause unknown production issues, or the frustration of repetitive and manual processes that have evolved over time making the team ineffective. By failing to ensure the software team has a voice in prioritising technical uplift can result in three key impacts. Firstly, the organisation is not trusting the most experienced people working on the platform who understand the key technical issues being faced and how to improve them. Secondly, the organisation is not solving fundamental problems within the platform that will enable faster and more effective development and deployment of new product features into production. Lastly, without trusting the team and empowering them to improve the codebase and platform, it will be detrimental to the culture within the team and ultimately the higher performing software developers will disengage and move on.</p><p>From the product perspective, product owners and product managers attempt to deliver the best possible customer experience. They listen to customers, gather feedback and aspire to release a range of new features to continually attract new customers or retain existing ones. They understand that doing this well will lead to an increase in profitability for the organisation. By not prioritising the development of new capabilities, a platform will be irrelevant as it eventually loses customers to competing organisations and platforms. The thought of losing customers is demotivating for anyone working within a product role and can lead to an increased turnover of product owners and managers within the organisation.</p><p>As mentioned above this is not a binary decision, in that an organisation does not have to focus 100% on technical uplift or 100% on product feature build. A healthy balance needs to be found between the two to ensure platform scalability, new product development and retention of team members within the organisation. Even an early stage startup focused entirely on growth needs to dedicate a percentage of time on technical uplift to ensure platforms don’t become too expensive to scale and maintain which is known to have crippled many startups throughout their scale-up phase. Twitter shared an insightful post on their blog many years ago that spoke to serious performance issues in relation to their search capability. It was a result of years of technical debt in their Ruby codebase which made it challenging to add features and improve the reliability of their search engine. However, the organisation trusted and empowered the software engineering team to implement a solution which resulted in the rewrite of their search capability which improved searching speeds on <a href="https://blog.twitter.com/engineering/en_us/a/2011/twitter-search-is-now-3x-faster.html">twitter three fold</a>.</p><h3>Don’t be binary</h3><p>In a previous software leadership role, I remember walking into a team facing considerable technical challenges specific to the scalability of the platforms running in production. This was due to the organisation scaling up incredibly quickly over their recent years which had resulted in unfortunate architectural decisions being made, as well as a continual focus on product growth with little to no priority on technical uplift. The software team was frustrated and they were beginning to see a higher than normal number of resignations. This needed to be resolved. Over the next 24 months the team began focusing their efforts on fundamental technical uplift. Platforms were decommissioned, others were re-architected, all alongside a much needed modernisation of software engineering tools and practices. The team was investing effort into rapidly evolving their platforms to support the organisation’s expansion aspirations. The first 18 months brought a breath of fresh air within the software team as there was now motivation and support to improve. At this point in time, the software teams were focusing on average 80% of their efforts on technical uplift which was the nearly exact opposite 18 months prior. Throughout the next six months though, the team that had once only wanted to improve their technology and platforms began to question their purpose within the organisation. Sure, their platforms were considerably more stable and scalable, they were even able to deploy production changes within hours instead of months, but something was missing. The organisation’s product vision was missing and teams started asking questions:</p><ul><li>Why are we building this product?</li><li>Who are our actual customers?</li><li>What is our vision for this product?</li></ul><p>The mindset across the group began showing signs of drifting back to where it was 18 months prior, which leads me to the point. It is unhealthy to have a software team focus 100% of their effort on technical uplift and ignore product feature development. Likewise you cannot expect a software developer to remain engaged if focusing solely on product feature development and disregarding any technical uplift or hygiene. A balance is required, and this balance is different in every team. This balance may also evolve every 6–12 months due to an organisation’s ever changing environment. As a software leader, you need to be constantly pulse checking where teams are spending their effort. Your role is to motivate teams with a balance of technical uplift and product feature development and ensure a purpose for both exists, and is known.</p><h3>How to find a healthy balance</h3><p>To solve the challenge between technical uplift versus product feature development it is important to accept that there needs to be a balance of the two at all times. Rarely if ever, will a team need to focus 100% of their effort on one or the other, nor should you want a team to due to the reasons discussed above. As with most aspects of software leadership there isn’t a single approach that can be taken to achieve this outcome. However, the below approaches act as guidelines that can help ensure a balance between technical uplift and product feature development.</p><h4>Clarify and make clear responsibilities</h4><p>It is important the software team is responsible and given ownership for two distinct areas. The first being how a feature is solutioned. Software developers are the closest to the code, they understand the impacts different solutioning decisions have on the maintainability and scalability of the codebase. Ensuring software teams own solutioning, it avoids non-technical individuals outside of the team who are less qualified making fundamental technical decisions that could be detrimental to the health of the platform. As part of solutioning, teams should always gather input from relevant stakeholders but this is purely in a consultative capacity. This will also reduce the potential of corners being cut in solutions that normally always results in incurring technical debt.</p><p>The second is owning work effort. The software team undertakes the development, testing and deployment of work items, thus are the only ones qualified to estimate the work effort required. Is it impossible and naive to believe a stakeholder can define the work effort required by the team when they do not understand the low level intricacies of the platform. Stakeholders are in their right and should share constraints that may be related to compliance, marketing deadlines or similar with the team. If a stakeholder attempts to define work effort and commit a team to it, as a software leader you need to clearly set expectations that they do not have the relevant expertise to make this decision and that it is up to the team to own work effort estimation. It can be necessary to communicate this message with senior leaders across the organisation, while keeping individuals honest when noticing it occurring.</p><p>Where a stakeholder may get involved in conversations is around scaling back feature scope to reduce work effort required if there are legitimate time constraints. By allowing the software team to own work effort estimation you are ensuring that non-functionals are taken into account, that technical unknowns are accounted for, and that the team will solution in line with the software team’s target state.</p><h4>Making technical uplift visible</h4><p>If technical uplift is not documented, not prioritised and not made visible, team’s will always struggle to make progress in it. It would be like walking up an escalator in the wrong direction, the amount of additional effort required is considerably more. Teams wouldn’t and shouldn’t start work on a new product feature without first understanding the outcomes, the priority, defining relevant user stories and documenting a solution. Why should technical uplift work be any different? Documenting technical uplift involves the team defining their most important initiatives, determining the value, prioritising them relative to each other and then work effort estimating them as a team. Try to focus on the immediate top 10 technical uplift initiatives compared to hundreds to avoid overwhelming not just the team but the wider organisation. With technical uplift documented it can now have an opportunity to be discussed and prioritised alongside other priorities coming into the team. It is no longer a hidden list of vague work items in the minds of people within the team.</p><h4>Justifying technical uplift initiatives smarter</h4><p>Outside of making technical uplift visible, the other single reason why it can struggle to be prioritised comes down to an inability to justify it in a way that is understood by the organisation. As a software leader you are responsible for being the voice of reason and support when it comes to prioritising technical uplift. Your role needs to understand how the organisation justifies and prioritises initiatives so you can apply the same approach to technical uplift. Below are three common ways organisations prioritise:</p><p><strong>Return on Investment (ROI):</strong></p><ul><li>ROI = total estimated value / cost to implement</li><li>Cost to implement includes all costs associated to develop and maintain including people costs, infrastructure costs, license costs etc..</li></ul><p><strong>Competitor feature parity.</strong></p><p><strong>Cost of Delay (CoD)</strong> is the cost to the organisation of not implementing the feature by a certain point in time. This may be lost revenue, compliance fines or lost customers due to a competitor moving faster.</p><p>Organisations will quite often use a combination of prioritisation methods as they mature to ensure a more complete justification. By understanding and applying the same justification to technical uplift initiatives it creates a level playing field allowing apples to be compared with apples.</p><p>Let’s explore a simple example using ROI as the only method to prioritise both a technical uplift or product initiative across an organisation. The product initiative will bring $240,000 of profit in the first year, at a total work effort cost of $75,000 (which equates to three software engineers for three months earning $100,000 a year). There is also a yearly cloud infrastructure cost of $12,000. ROI for the first year is calculated by:</p><ul><li>ROI = total profit / total cost</li><li>ROI = $240,000 / ($75,000 + $12,000)</li><li>ROI = 2.75</li></ul><p>This provides an ROI of <strong>2.75</strong>.</p><p>The technical uplift initiative will deliver $420,000 of savings by deprecating a legacy platform reducing the need for a small team currently supporting it. The total work effort cost is $100,000 (which equates to 2 software engineers for 6 months earning $100,000). ROI is calculated by:</p><ul><li>ROI = total profit / total cost</li><li>ROI = $420,000 / $100,000</li><li>ROI = 4.2</li></ul><p>This provides an ROI of <strong>4.2</strong>.</p><p>Without taking into account any other prioritisation approach it is clear that the technical uplift initiative provides a higher ROI to the organisation and should potentially be prioritised first.</p><p>It is also within your best interest to think carefully about the real costs of managing platforms that technical uplift will simplify and improve. If an initiative is wanting to automate deployment pipelines for a specific platform, it is valuable to understand the costs in maintaining the existing manual deployment processes. You may have four teams spending three hours each week manually releasing code. Over the course of just one year the team would spend $31,200 worth of effort manually releasing. This cost will continue to be incurred every year until the deployment process is automated. The cost of manually deploying also takes precious capacity away from developing new product features. When describing impacts and value in this way, it becomes even easier to rally support from product management as they are selfishly (and rightly so) focused on delivering more product features, faster.</p><p>Finally, it is in your best interest to coach software leaders within your team on these prioritisation approaches. This will result in many of the justification and prioritisation conversations being able to happen directly within the team and avoid the need for your role to become involved in every single one of them.</p><h4>Coupling technical uplift with product features</h4><p>Sounds simple right? If your organisation is product led, there will be a never ending list of new product initiatives in the backlog. One of your responsibilities of a software leader is to continually look for opportunities and synergies between technical uplift initiatives and new product initiatives. By coupling a new product feature with a relevant technical uplift improvement that resides in the same area of the codebase, it can often reduce effort around testing which creates efficiencies when compared to delivering them both in isolation.</p><p>For example, there may be a product feature to implement an additional payment provider to the customer. Within the technical uplift backlog there could be an initiative to improve the level of unit testing that exists within the platforms payment service. There is a clear relationship between these two items of work. In terms of efficiency, there is a benefit to increase the unit testing coverage within the payment service while adding an additional payment provider. Not only do these two initiatives reside in the same area of the codebase, but the work effort in completing them together is also less than the sum of both their work efforts in isolation. This is a result of the effort required to test the platform’s payment implementation can be performed just once, not twice. Secondly, the software developers will already be familiar with the payment section of the codebase thus being more efficient, rather than context switching back at a future point in time.</p><p>Another example may revolve around the need to support demand in customer growth which requires introducing additional servers into the rotation. The software team also has a technical uplift initiative in the backlog to move all cloud infrastructure into code. Taking a short term tactical approach would see the team simply add an additional server manually into the rotation pool. However, this wouldn’t move the software team any closer to their target state of infrastructure as code. An approach could be to implement the additional server as infrastructure as code, but manually add the server into the rotation. This provides the benefit of meeting the product requirement but also aligning a portion of the work to the software teams target state without re-implementing the entire load balancing capability as code. Of course, if it is possible to also move the load balancer to infrastructure as code, it should be considered.</p><p>Identifying synergies between technical uplift and product initiatives has been one of the most successful approaches I have been able to follow to ensure a healthy balance.</p><h4>Larger technical uplift sometimes requires a project</h4><p>In some instances, a technical uplift initiative is simply too large in work effort to be coupled into a product initiative. A fundamental goal of software teams is to ensure they release small and release often. This implies avoiding weeks or even months of code not being released into production. When a technical uplift initiative requires considerable work effort to see it through to completion, it needs to be treated as a project. This involves defining clear outcomes, measures of success, documenting high level scope usually as user stories, estimating work effort and a justification aligned to the organisation’s process.</p><p>Using the technical uplift example of implementing an automated build pipeline, the justification may look like:</p><p><strong>The current cost to the organisation of not implementing</strong></p><ul><li>Four teams each undertake one release every week.</li><li>Each release costs four hours of work effort.</li><li>As each release is after hours it also incurs three hours Time in Lieu (TIL).</li><li>This sums up to <strong>1,456</strong> hours a year releasing features.</li><li>This equates to <strong>$72,800</strong>.</li></ul><p><strong>Total cost in work effort to implement is 988 hours</strong></p><ul><li>This equates to two software engineers for three months.</li><li>The total work effort costs <strong>$49,400</strong>.</li></ul><p>By entirely automating the platforms build pipelines, after the first year there will be a cost saving of <strong>$23,400</strong> and then <strong>$72,800</strong> for subsequent years.</p><p>That is a very persuasive justification as this initiative pays for itself after just 9 months. Taking it one step further, as the teams are now able to release new product features with zero manual effort it enables smaller feature releases more frequently. This reduces production deployment risks while delivering value add features to customers faster than ever before. I would challenge you to find any product role who wouldn’t support this initiative within the organisation.</p><h4>Ensuring a combined organisational roadmap</h4><p>In many organisations it can make sense to have a separate technical uplift roadmap and product initiative roadmap. This can support a more efficient planning process within specific teams as these roadmaps are often owned by different leaders. However, at an organisational level when organisational priorities are published, it is imperative to have a single and aligned roadmap that includes both technical uplift and product initiatives prioritised side by side.</p><h4>Breaking down larger technical uplift initiatives</h4><p>The larger the work effort is for an initiative, the harder it is going to be to justify and prioritise. Organisational leaders usually struggle to support large initiatives as their value won’t be realised for a considerable time into the future. Prioritising a six month work effort product initiative is challenging enough, let alone a six month technical uplift initiative. As a software leader, you need to be continuously looking for ways to break larger initiatives into smaller work items that can be delivered into production sooner.</p><p>For example, take the technical initiative to implement a new organisational wide logging and monitoring platform that has a total work effort of seven months. By breaking it down into smaller work items as seen below, the effort required to justify is greatly decreased, risks are reduced and work items that can run in parallel may become more clear.</p><ol><li>One sprint to research and document a solution.</li><li>Three sprints to build out the base logging and monitoring platform.</li><li>Two sprints to upskill and train all software teams.</li><li>Three sprints to integrate logging into the first platform.</li><li>One sprint to implement monitoring into the first platform.</li><li>… Repeat steps 4 and 5 for each additional platform.</li></ol><p>Always focus on finding the smallest item of work that can be released into production that still adds value to the organisation.</p><h4>Definition of Done</h4><p>A Definition of Done (DoD) is essential for every software team to define and proactively adhere to. The purpose of a Definition of Done is to improve the overall quality of the capabilities that the team deploys into production. Quite often a Definition of Done includes specific non-functional requirements (NFRs) that a team values, which may include:</p><ul><li>Automated testing exists for each feature.</li><li>Solution and code has been peer reviewed.</li><li>Documentation has been created or updated.</li><li>Monitoring has been implemented to ensure observability of all critical paths.</li><li>Product owner has reviewed and signed off on the new capability.</li></ul><p>A Definition of Done does not reduce existing technical debt, however, it plays a part in reducing new technical debt being created which would otherwise need to be paid back in the future.</p><h3>TL;DR</h3><ul><li>The decision between technical uplift and product initiatives shouldn’t be a binary decision. An organisation needs to find a healthy balance between the two to ensure platform scalability, new product development and retention of team members.</li><li>Technical uplift initiatives need to be documented, prioritised and made visible to the wider organisation otherwise the team will always struggle to make progress.</li><li>Continually look for opportunities to couple technical uplift with new product initiatives. This will encourage efficiencies by reducing testing time and reducing context shifting the software team may face by tackling both initiatives separately.</li></ul><p>Balancing technical uplift with product development is not a trivial task. But it will improve the effectiveness and harmony of your team if you can accomplish it. If you found this post useful, check out my book “<a href="https://leadingsoftwareteamswithcontextnotcontrol.com/"><strong>Leading software teams with context, not control</strong></a>” on <a href="https://leanpub.com/leading-software-teams-with-context-not-control">Leanpub</a> as well as <a href="https://leadingsoftwareteamswithcontextnotcontrol.com/">other online stores</a> like Amazon in eBook or print format.</p><p><em>Originally published at </em><a href="https://blog.dionbeetson.com/2020/07/balancing-technical-uplift-with-product.html"><em>https://blog.dionbeetson.com</em></a><em> on July 30, 2020.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61fa6391f8a7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I’ve written a book about leading software engineering teams]]></title>
            <link>https://medium.com/@dionbeetson/ive-written-a-book-about-leading-software-engineering-teams-b81aaba8910f?source=rss-879af07f2512------2</link>
            <guid isPermaLink="false">https://medium.com/p/b81aaba8910f</guid>
            <dc:creator><![CDATA[Dion Beetson]]></dc:creator>
            <pubDate>Tue, 28 Jul 2020 13:00:00 GMT</pubDate>
            <atom:updated>2022-01-08T01:59:04.767Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*HoVkNRyF6JgFBlOdRFCfmQ.png" /></figure><p>Ten months ago I set myself a one year goal to write a book on leading software engineering teams. This books purpose was to document the practices and initiatives I followed while leading software teams. It was to provide myself with a reference model to refer back to and reduce the cognitive load required within my day to day role. What started off as a casual few hours a week planning and firming up the books chapter, rapidly turned into 3+ hours every night researching, writing, rewriting, deleting and writing some more in an effort to complete this side project before our first baby was born. I didn’t exactly make the 9 month deadline, but published v1.0.0 three days later. The book is titled <strong>“Leading Software Teams with Context, Not Control”</strong> and can be purchased from <a href="https://leanpub.com/leading-software-teams-with-context-not-control">Leanpub</a> as well as <a href="https://leadingsoftwareteamswithcontextnotcontrol.com/">other online stores</a> like Amazon in eBook or print format.</p><h3>Why this book was written</h3><p>As a software engineering leader, the scope of your role is extensive. You have many competing responsibilities and priorities that need to be balanced to ensure you and your team are as effective as possible. These can include providing architectural direction, driving peer to peer collaboration, ensuring cross-team alignment, motivating teams with purpose, supporting team members’ career progression, or perhaps helping remove blockers and impediments. All of these efforts work to create a specific culture within a software team that aims to improve effectiveness, engagement, and retention.</p><p>I wrote this book for software leaders who are responsible for leading teams. More specifically it focuses on approaches for leading multiple software teams whether that is directly or indirectly through leadership roles reporting into your role. There is a level of unique complexity that comes with leading, aligning and supporting multiple software development teams. This book aspires to provide you with helpful and reusable approaches that can be leveraged to bring about a greater level of efficiency into your role as a leader. There are many books written around leading teams or leading people, this book takes a lens of what specific practices and initiatives you should be investing your time into when leading technical software teams.</p><p>Regardless of the size of your software team, if you find yourself needing to better balance both the technical and people aspects of leading teams, or guidance on initiatives you could be running to improve team alignment, effectiveness and engagement then this book is written for you.</p><h3>What’s in the book?</h3><p>The book comprises of 23 chapters that discuss a broad range of initiatives you can run when leading software engineering teams. These range from baselining a software team to effective software engineering metrics to crafting an experimentation culture. The book is broken into 3 parts:</p><ul><li>Part 1: Creating alignment</li><li>Part 2: Leading teams</li><li>Part 3: Uplifting team culture</li></ul><p>Each chapter has a loose structure of explaining the topic, talking to why it is important within a software engineering team and different approaches you can use to implement within your own team. Most chapters include multiple exercises that you can adopt into your organisation, as well as the occasional story around specific experiences I have had while leading teams in my previous and current roles.</p><p>Interested in more insight to exactly what is in each chapter just incase it tempts you to pick yourself up a copy? Here it is…</p><h3>Part 1: Creating alignment</h3><ol><li><strong>Baselining a software team<br></strong>All the thing you need to do to understand the current state of your software engineering team. It talks to technical and team cultural measures and techniques to determine a teams baseline.</li><li><strong>Defining a software team target state<br></strong>A software target state is the technical and non-technical aspirations of the team that are flags on the top of the hill for you and your team to continuously climb towards. Learn how to define a software target state for your team.</li><li><strong>The software engineering roadmap<br></strong>A software engineering roadmap is a visual representation that defines a team’s pathway to achieving their target state. This chapter explains the importance and how to implement one.</li><li><strong>Effective software team metrics<br></strong>Metrics within software are measurements that are put in place to keep you and your teams honest, accountable and continuously improving. Learn about what makes good team metrics, as well as metrics you need to avoid.</li><li><strong>Importance of collaborating on team goals<br></strong>Setting goals for your team or team members does not need to be an overly time consuming exercise, although it does need to align to your team target state, roadmap and team metrics.</li><li><strong>Balancing reactive versus strategic work<br></strong>Getting stuck in the weeds is all too common for software engineering teams, learn about strategies to better balance the time you spend on strategic based work items.</li><li><strong>Balancing technical uplift with product development<br></strong>The decision between technical uplift and product initiatives shouldn’t be a binary decision. Discover approaches to find a healthy balance between the two to ensure platform scalability, new product development and retention of team members.</li><li><strong>Introducing a new technology<br></strong>As a software leader, you are accountable for ensuring relevant new technology is being adopted within your team at a healthy and manageable pace. This is compared to implementing too many technologies too quickly and running the risk of losing great software developers from technology change fatigue or cognitive overload due to overly complex platforms.</li><li><strong>Platform SLAs<br></strong>Discover SLAs that add value to a platform, as well as important items to consider when implementing SLAs for your software teams platforms.</li></ol><h3>Part 2: Leading teams</h3><ol><li><strong>Effective 1:on:1s<br></strong>1:on:1s are weekly or fortnightly catch-ups with each of your direct reports, that provide an opportunity for you to listen, provide guidance, coach, listen more and support them within their role and future career aspirations. Find out about approaches to make the most out of 1:on:1s within your team.</li><li><strong>Continuous performance feedback<br></strong>Performance feedback within software teams should more than a once yearly exercise that is orchestrated through the organisation’s HR department. There are many opportunities to provide constructive feedback to your team every day of the week.</li><li><strong>Impactful position descriptions<br></strong>Position descriptions are short (no more than 3 pages), well formed documents that clearly articulate the impact a role has, where it sits within the organisation and breaks down the key responsibility areas of that role. Discover how to craft position descriptions that create a sense of excitement and motivation within a role that is genuinely valued within the organisation.</li><li><strong>Candidate centric interviews<br></strong>Understand what candidate centric interviews are and how they build trust between the interviewer and the candidate which results in them being more genuine about their experiences, concerns in the role and their career aspirations. While at the same time becoming more invested in the role within the team.</li><li><strong>Onboarding effectively<br></strong>Effective onboarding should include a combination of discussions, introductions, workshops, documentation sharing and mentoring to support new starters in becoming a motivated and effective team member. This chapter explains the different phases of onboarding to focus on within your software teams.</li><li><strong>Software team structures<br></strong>Learn how to implement ‘just enough hierarchy’ while coupling it with small team sizes of seven or less, to dramatically reduce the blast radius and impact when an individual chooses to move on from the organisation.</li><li><strong>Career pathway framework for software teams<br></strong>A software career pathway framework links together roles to represent different pathways of progression an individual can follow to advance their career within the team that aligns with their skills, experience and motivation. It is not a trivial task, however this chapter aims to provide some key learnings to fast track your own implementation.</li></ol><h3>Part 3: Uplifting team culture</h3><ol><li><strong>Context over control<br></strong>It is in the title of this book, and this chapter explains how a context over control approach allows software leaders to lead vastly larger teams and projects when compared to a micromanagement approach.</li><li><strong>Engaging team meetings<br></strong>As a software leader you are accountable for ensuring weekly team meetings are set up, are engaging, have the right amount of energy and bring value to as many individuals within that session as possible.</li><li><strong>Team health checks<br></strong>Team health checks usually consist of six to ten questions that focus on technical, team and communication practices that each team discusses and rates every six to eight weeks. Understand what makes health checks valuable and approaches on running health checks within your teams.</li><li><strong>Building a culture of learning<br></strong>A software team that is built around a culture of learning allows its members to learn in all aspects of their role. Learn about how building a culture of learning can be achieved at no financial cost to the organisation.</li><li><strong>Crafting an experimentation culture<br></strong>Software teams that embrace an experimentation culture have a more maintainable technology stack, incur less technical debt, and are thus able to iterate faster on developing and releasing new capabilities. Discover approaches to encourage experimentation in every aspect of your teams day to day.</li><li><strong>Software engineering working groups<br></strong>Software engineering working groups involve a set of individuals working together to build a center of excellence around a specific topic. This chapter explains different approaches of implementing working groups within your organisation.</li><li><strong>Running a software team hack day<br></strong>Running a hack day allows your teams to take a break from the day to day and collaborate together on solving real software engineering problems being faced. Learn about a high level structure and run sheet you can use to run your very own hack day.</li></ol><p>Although this goal can now be ticked off my list, I’m looking forward to many future iterations as I evolve my ways of running software engineering teams. If you would like to check out a free sample, you can download it over at <a href="https://leanpub.com/leading-software-teams-with-context-not-control">Leanpub</a>. eBook formats are available on <a href="https://leanpub.com/leading-software-teams-with-context-not-control">Leanpub</a>, <a href="https://www.amazon.com/Leading-software-teams-context-control-ebook/dp/B08BBSBPL9/">Amazon</a>, <a href="https://play.google.com/store/books/details?id=CcjrDwAAQBAJ">Google Play</a> and <a href="https://books.apple.com/us/book/id1519578649">iBooks</a>. Print copies can also be purchased from <a href="https://www.amazon.com/Leading-software-teams-context-control/dp/B08C475VMR/">Amazon</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b81aaba8910f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to bulk reset your Reddit upvote history]]></title>
            <link>https://medium.com/@dionbeetson/how-to-bulk-reset-your-reddit-upvote-history-e2830b6203df?source=rss-879af07f2512------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2830b6203df</guid>
            <dc:creator><![CDATA[Dion Beetson]]></dc:creator>
            <pubDate>Fri, 03 Apr 2020 13:00:00 GMT</pubDate>
            <atom:updated>2022-01-10T03:55:43.317Z</atom:updated>
            <content:encoded><![CDATA[<p>Ever needed to bulk reset your upvote history for a subreddit? Yes, it is a bit of a random topic. However, I found myself in this situation recently and there wasn’t a way to do so using Reddit’s built in capabilities. A simple piece of Javascript came to the rescue!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*rayOMkWqwjqiwB9X.png" /></figure><p><strong><em>Disclaimer:</em></strong><em> This solution is coupled to the markup of the current Reddit website. So will only be functional until Reddit changes their markup in the future.</em></p><p>Start by navigating your browser to:<br> <a href="https://www.reddit.com/user/dionbeetson/upvoted/">https://www.reddit.com/user/dionbeetson/upvoted/</a></p><p>Then open up your developer console (F12). Click on the console tab.</p><pre>/**<br> * Function to clear votes for a specific subreddit<br> */<br>let clearVote = (subReddit) =&gt; {<br>    let clearedCounter = 0;<br>    let anchorsFound = document.querySelectorAll(&quot;a[href$=&#39;/r/&quot; + subReddit + &quot;/&#39;]&quot;);<br><br>    for (var i = 0, l = anchorsFound.length; i &lt; l; i++) {<br>	let anchor = anchorsFound[i];<br>	let post = anchor.closest(&#39;.Post&#39;);<br>	let button = post.querySelector(&quot;button[data-click-id=&#39;upvote&#39;]&quot;);<br><br>	button.click();<br>	clearedCounter++;<br>    }<br><br>    console.log(&#39;Cleared &#39; + clearedCounter + &#39; upvotes from &#39; + subReddit);<br>};<br><br>// We need to continually keep scrolling to the bottom to load more history<br>let scrollToBottomCounter = 0;<br>let interval = setInterval(() =&gt; {<br>if( scrollToBottomCounter &gt; 300 ) {<br>        clearInterval(interval);<br>        clearVote(&#39;Bitcoin&#39;);<br>    }<br><br>    window.scrollTo(0,document.body.scrollHeight);<br><br>    scrollToBottomCounter++;<br>}, 1500);<br><br>// Call clear vote for the subreddits you want to clear your upvotes for<br>// EG: this is the name of the subreddit that you can see within the UI that looks like r/CryptoMarkets/<br>// However you simply include the subreddit name and not the starting &#39;r/&#39; and trailing &#39;/&#39;<br>clearVote(&#39;InstantRegret&#39;);<br>clearVote(&#39;RoastMe&#39;);<br><br>// You can call clearVote as many times as you require.</pre><h3>How to run the script</h3><p>Now, after adding a &#39;clearVote&#39; function call for each subreddit you want to clear upvotes for you can paste this script into the &#39;console&#39; within your browser and the script will simulate a user clicking the upvote button for all of your upvote history.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2830b6203df" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NeuroEvolution using TensorFlow JS — Part 3]]></title>
            <link>https://medium.com/@dionbeetson/neuroevolution-using-tensorflow-js-part-3-c266b47d3ebc?source=rss-879af07f2512------2</link>
            <guid isPermaLink="false">https://medium.com/p/c266b47d3ebc</guid>
            <dc:creator><![CDATA[Dion Beetson]]></dc:creator>
            <pubDate>Fri, 19 Jul 2019 13:00:00 GMT</pubDate>
            <atom:updated>2022-01-08T09:20:28.754Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*vgIMi3HQjjBk7rxC.gif" /></figure><p>This tutorial is part 3, if you have not completed <a href="https://medium.com/@dionbeetson/neuroevolution-using-tensorflowjs-part-1-7e66ed42ccc0">NeuroEvolution using TensorFlowJS — Part 1</a> and <a href="https://medium.com/@dionbeetson/neuroevolution-using-tensorflow-js-part-2-b13d2861f4a9">NeuroEvolution using TensorFlowJS — Part 2</a>, I highly recommend you do that first. As it will:</p><ul><li>Explain how to setup the codebase</li><li>Teach you how to code a basic NeuroEvolution implementation</li><li>Improve the performance of the NeuroEvolution</li><li>Implement the ability to save/load models</li></ul><p>In this last tutorial we will:</p><ul><li>Learn how to use simple shape/object/color detection to gather the inputs required to feed into TensorFlow JS (instead of interacting directly with the GameAPI object)</li></ul><p>This tutorial is also based on the source code found on my GitHub account here <a href="https://github.com/dionbeetson/neuroevolution-experiment">https://github.com/dionbeetson/neuroevolution-experiment</a>.</p><h3>Create base class</h3><p>We need to create a class that will be used instead of GameAPI to gather all of the required inputs from the game every 10ms and then pass them to TensorFlow JS to make the same prediction to jump or not.</p><p>Disclaimer:</p><ul><li>There is already logic in js/ai/Ai.js to initialise this class if you select the checkbox ‘Use Object Recognition’ in the UI</li><li>There are a few inputs small items we are not decoupling from the original GameAPI (eg: getScore()), as it was outside the scope of my experiment. But it could be implemented if we really wanted to.</li></ul><h4>How it works</h4><p>Every 10ms the game will execute a method to:</p><ol><li>Extract an image from the game canvas</li><li>Convert the image to greyscale</li><li>Using a 10x10 pixel grid create a layout of the level in regards to:</li><li>What is whitespace (color=white)</li><li>What are obstacles to jump over (color=grey)</li><li>Where the player is (color=black)</li><li>Look ahead 4 blocks (40 pixels) and determine if there is a block or dip to jump over</li><li>Determine the x/y coordinates that can feed into TensorFlowJS</li><li>Ai.js will then use the same logic as the previous tutorials to determine to jump or not</li></ol><p>Create a file called js/ai/GameImageRecognition.js and then paste in the below code.</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>class GameImageRecognition { }</pre><p>Lets now create the start() method which is called by default in Ai.js which will start the game, setup the canvas tracker (which is essentially a new hidden canvas DOM element that we paint an image of the game canvas into every 10ms) and use it for detecting shapes/objects/player position etc...</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>start() {<br>  const self = this;<br><br>  this.#enableVision = document.querySelector(&quot;#ml-enable-vision&quot;).checked;<br><br>  // @todo - remove dependency on gameAPI object - although outside of scope of this example<br>  this.#gameApi.start();<br>  this.setupCanvasTracker();<br><br>  // Simulate what happens in the game<br>  setTimeout(() =&gt; {<br>    self.#isSetup = true;<br>  }, 100);<br>}</pre><p>We will also add in a few other helper functions to get things moving.</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>setupCanvasTracker(){<br>  this.#visualTrackingCanvas = document.createElement(&quot;canvas&quot;);<br>  this.#visualTrackingCanvas.setAttribute(&quot;width&quot;, this.#gameApi.getWidth());<br>  this.#visualTrackingCanvas.setAttribute(&quot;height&quot;, this.#gameApi.getHeight());<br>  this.#visualTrackingCanvas.setAttribute(&quot;class&quot;, &quot;snapshot-canvas&quot;);<br><br>  this.#gameApi.getContainer().appendChild(this.#visualTrackingCanvas);<br>  this.#gameApiCanvas = this.#gameApi.getCanvas();<br>}<br><br>setHighlightSectionAhead(index) {<br>  // Not required for this demo<br>  return;<br>}<br><br>isOver() {<br>  return this.#gameApi.isOver();<br>}<br><br>isSetup() {<br>  return this.#isSetup;<br>}</pre><p>As well as some required class variables</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>#gameApi = new GameApi();<br>#gameApiCanvas;<br>#isSetup = false;<br>#visualTrackingCanvas;<br>#enableVision = false;</pre><p>Now wire up the UI event handler</p><p><strong>js/ai/ui.js</strong></p><pre>document.querySelector(&quot;#ml-use-object-recognition&quot;).addEventListener(&quot;change&quot;, function() {<br>  if ( this.checked ) {<br>    neuroEvolution.useImageRecognition = true;<br>  } else {<br>    neuroEvolution.useImageRecognition = false;<br>  }<br>});</pre><p>And add in the required setters for useImageRecognition</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>set useImageRecognition( useImageRecognition ) {<br>  this.#useImageRecognition = useImageRecognition;<br>}</pre><p>Reload your browser ( <a href="http://127.0.0.1:8080/">http://127.0.0.1:8080/</a>), check ‘Use Object Recognition’ and click ‘Start evolution’ button. You should see the games begin, but get a lot of game.gameApi.getPlayerY is not a function errors. This is because we need to implement a range of functions to gather input.<br> Before we do that though, we will add in the logic to extract information from the game canvas every 10ms.</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>// Method to extract data from canvas/image and convert it into a readable format for this class to use<br>extractVisualTrackingData(){<br>  let data = this.#gameApiCanvas.getContext(&#39;2d&#39;).getImageData(0, 0, this.#visualTrackingCanvas.width, this.#visualTrackingCanvas.height);<br>  let dataGrey = this.convertImageToGreyScale(data);<br><br>  this.#visualTrackingMap = this.generateVisualTrackingMap(dataGrey, this.#visualTrackingCanvas.width, this.#visualTrackingCanvas.height, this.#visualTrackingMapSize, this.#colors);<br><br>  this.updatePlayerPositionFromVisualTrackingMap(this.#visualTrackingMap, this.#colors);<br><br>  this.#sectionAhead = this.getSectionAhead(this.#playerX, this.#playerY, 4, this.#visualTrackingMapSize, this.#playerGroundY);<br>}<br><br>// Method to create an object indexed by xposition and yposition with the color as the value, eg: 10x40 = grey<br>generateVisualTrackingMap(data, width, height, visualTrackingMapSize, colors) {<br>  let visualTrackingMap = {};<br>  for( let y = 0; y &lt; height; y+=visualTrackingMapSize ) {<br>    for( let x = 0; x &lt; width; x+=visualTrackingMapSize ) {<br>      let col = this.getRGBAFromImageByXY(data, x+5, y+5)<br>      let key = x+&#39;x&#39;+y;<br>      visualTrackingMap[key] = colors.background;<br><br>      if ( 0 == col[0] ) {<br>        visualTrackingMap[key] = colors.player;<br>      }<br><br>      if ( col[0] &gt; 210 &amp;&amp; col[0] &lt; 240 ) {<br>        visualTrackingMap[key] = colors.block;<br>      }<br>    }<br>  }<br><br>  return visualTrackingMap;<br>}</pre><p>These above functions have extra dependencies. Let’s add in functionality to convert an image into greyscale, as well as get the RGBA from a specific pixel on that image.</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>convertImageToGreyScale(image) {<br>  let greyImage = new ImageData(image.width, image.height);<br>  const channels = image.data.length / 4;<br>  for( let i=0; i &lt; channels; i++ ){<br>    let i4 = i*4;<br>    let r = image.data[i4 + 0];<br>    let g = image.data[i4 + 1];<br>    let b = image.data[i4 + 2];<br><br>    greyImage.data[i4 + 0] = Math.round(0.21*r + 0.72*g + 0.07*b);<br>    greyImage.data[i4 + 1] = g;<br>    greyImage.data[i4 + 2] = b;<br>    greyImage.data[i4 + 3] = 255;<br>  }<br><br>  return greyImage;<br>}<br><br>getRGBAFromImageByXY(imageData, x, y) {<br>  let rowStart = y * imageData.width * 4;<br>  let pixelIndex = rowStart + x * 4;<br><br>  return [<br>    imageData.data[pixelIndex],<br>    imageData.data[pixelIndex+1],<br>    imageData.data[pixelIndex+2],<br>    imageData.data[pixelIndex+3],<br>  ]<br>}</pre><p>Add these class variables as well</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>#visualTrackingMap = {};<br>#visualTrackingMapSize = 10;<br>#sectionAhead = [];<br>#playerX = 0;<br>#playerY = 0;<br>#playerGroundY = 0;<br>#colors = {<br>  block: &#39;grey&#39;,<br>  visionOutline: &#39;red&#39;,<br>  player: &#39;black&#39;,<br>  background: &#39;white&#39;<br>};</pre><p>Now we want to add in 3 methods that will be called from Ai.js to detect some of the inputs from the previous tutorials.</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>getHeight() {<br>  return this.#visualTrackingCanvas.height;<br>}<br><br>getWidth() {<br>  return this.#visualTrackingCanvas.width;<br>}<br><br>getPlayerY() {<br>  return this.#playerY;<br>}</pre><p>Reload your browser ( <a href="http://127.0.0.1:8080/">http://127.0.0.1:8080/</a>), check ‘Use Object Recognition’ and click ‘Start evolution’ button. Again, you should see the games begin, but now get a lot of game.gameApi.getPlayerX is not a function errors. Ok, we are making progress, let&#39;s implement this method.<br> This method is actually the method we hook into to do all of the processing of the games canvas. Realistically we could have pulled this out into it&#39;s own setInterval(), but for the purpose of this demo let&#39;s couple it in with getPlayerX() which is called within every think() invocation.</p><h3>js/ai/GameImageRecognition.js</h3><pre>getPlayerX() {<br>  this.extractVisualTrackingData();<br> <br>  return this.#playerX;<br>}</pre><p>Now add in a method to determine the players x/y position on the canvas (we do this by finding the 10x10 pixel that is color #000000 (black)). Simple, yet effective.</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>updatePlayerPositionFromVisualTrackingMap(visualTrackingMap, colors) {<br>  for (const xy in visualTrackingMap) {<br>    let value = visualTrackingMap[xy];<br><br>    if ( colors.player == value) {<br>      let position = xy.split(&#39;x&#39;);<br>      this.#playerX = parseInt(position[0]);<br>      this.#playerY = parseInt(position[1]);<br><br>      // If we dont have a ground, then set it<br>      if( 0 == this.#playerGroundY ) {<br>        this.#playerGroundY = this.#playerY;<br>      }<br>    }<br>  }<br>}</pre><p>Next up is a lot of logic to look through visualTrackingMap which stores all the colors of each 10x10 pixel section and determine what lies ahead in relation to the player.</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>getSectionAhead(playerX, playerY, aheadIndex, pixelMapSize, playerGroundY){<br>  let x;<br>  let y;<br>  let section;<br>  let aheadWidth = aheadIndex*10;<br><br>  x = Math.ceil(playerX/pixelMapSize) * pixelMapSize + aheadWidth;<br>  y = Math.ceil(playerY/pixelMapSize) * pixelMapSize;<br><br>  section = this.getCollisionSectionAhead(x, y);<br><br>  if( false == section ) {<br>    section = [x, playerGroundY+pixelMapSize, pixelMapSize, pixelMapSize];<br>  }<br><br>  return {<br>    x: section[0],<br>    y: section[1],<br>    width: section[2],<br>    height: section[3],<br>  };<br>}<br><br>// Logic to get the xy and width/height of the section ahead that we need to use to determine if we jump over or not<br>getCollisionSectionAhead(x, y) {<br><br>  // Look for drop/dip section ahead we need to jump over<br>  y = this.#playerGroundY;<br><br>  if ( this.isSectionSolid(x, y) ) {<br>    // Look for taller section ahead we need to jump over<br>    let xyStart = this.findTopLeftBoundsOfSolidSection(x, y-this.#visualTrackingMapSize);<br>    let xyEnd = this.findTopRightBoundsOfSolidSection(xyStart[0], xyStart[1], 1);<br>  <br>    return [xyStart[0], xyStart[1], xyEnd[0] - x, y - xyEnd[1] + this.#visualTrackingMapSize];<br>  } else {<br><br>    if (  false === this.isSectionSolid(x, y+this.#visualTrackingMapSize) ) {<br>      let xyStart = this.findBottomLeftBoundsOfSolidSection(x, y);<br>      let xyEnd = this.findBottomRightBoundsOfSolidSection(xyStart[0], xyStart[1], 1);<br><br>      return [xyStart[0], xyEnd[1]+this.#visualTrackingMapSize, xyEnd[0] - x, this.#visualTrackingMapSize];<br>    }<br>  }<br><br>  return false;<br>}<br><br>isSectionSolid(x, y){<br>  let section = this.#visualTrackingMap[x + &#39;x&#39;  +y];<br>  if ( this.#colors.block == section ) {<br>    return true;<br>  }<br><br>  return false;<br>}<br><br>findTopLeftBoundsOfSolidSection(x, y) {<br>  if ( this.isSectionSolid(x, y) ) {<br>    return this.findTopLeftBoundsOfSolidSection(x, y-this.#visualTrackingMapSize)<br>  }<br><br>  return [x,y+this.#visualTrackingMapSize];<br>}<br><br>findTopRightBoundsOfSolidSection(x, y, counter) {<br>  if ( counter &lt; 5 &amp;&amp; this.isSectionSolid(x, y) ) {<br>    counter++<br>    return this.findTopRightBoundsOfSolidSection(x+this.#visualTrackingMapSize, y, counter)<br>  }<br><br>  return [x,y];<br>}<br><br>findBottomLeftBoundsOfSolidSection(x, y) {<br>  if ( false === this.isSectionSolid(x, y) &amp;&amp; y &lt; this.#visualTrackingCanvas.height) {<br>    return this.findBottomLeftBoundsOfSolidSection(x, y+this.#visualTrackingMapSize)<br>  }<br><br>  return [x,y-this.#visualTrackingMapSize];<br>}<br><br>findBottomRightBoundsOfSolidSection(x, y, counter) {<br>  if ( counter &lt; 5 &amp;&amp; false === this.isSectionSolid(x, y) ) {<br>    counter++<br>    return this.findBottomRightBoundsOfSolidSection(x+this.#visualTrackingMapSize, y, counter)<br>  }<br><br>  return [x,y];<br>}<br><br>getSectionFromPlayer(index) {<br>  return {<br>    x: this.#sectionAhead.x,<br>    y: this.#sectionAhead.y,<br>    width: this.#visualTrackingMapSize,<br>    height: this.#playerY-this.#sectionAhead.y<br>  };<br>}</pre><p>I will be the first to admit the above logic is not clean/performant and can really be improved. But the purpose of this demo was to prove what is possible — feel free to submit a PR if you want to improve :-) <br> Getting closer… Reload your browser ( <a href="http://127.0.0.1:8080/">http://127.0.0.1:8080/</a>), check ‘Use Object Recognition’ and click ‘Start evolution’ button. You should now see a lot of game.gameApi.isPlayerJumping is not a function errors. Let&#39;s implement that and a few other methods that are needed regarding the player.</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>isPlayerJumping() {<br>  if( this.#playerY &lt; this.#playerGroundY ) {<br>    return true;<br>  }<br><br>  return false;<br>}<br><br>getPlayerVelocity() {<br>  return 0;<br>}<br><br>canPlayerJump() {<br>  if( this.isPlayerJumping() ) {<br>    return false;<br>  }<br><br>  return true;<br>}</pre><p>Reload your browser ( <a href="http://127.0.0.1:8080/">http://127.0.0.1:8080/</a>), check ‘Use Object Recognition’ and click ‘Start evolution’ button. You should now see a lot of game.gameApi.setDebugPoints is not a function errors. Let&#39;s implement.</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>setDebugPoints(debugPoints) {<br>  this.#gameApi.setDebugPoints(debugPoints);<br>}</pre><p>We are actually missing a key method jump(). For the sake of this demo, we are just going to revert to calling the GameAPI. We could simulate this with a bit of trickery by focusing in on the canvas and triggering the &#39;spacebar&#39; button. But a little too much for this demo.</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>jump(){<br>  // The only way to simulate this is by pressing the spacebar key, but because we have multiple games at once it isn&#39;t easily possible.<br>  this.#gameApi.jump();<br>}</pre><p>Reload your browser ( <a href="http://127.0.0.1:8080/">http://127.0.0.1:8080/</a>), check ‘Use Object Recognition’ and click ‘Start evolution’ button. You should now see the game mostly work, although a few errors will pop up. Add in the below.</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>getProgress() {<br>  return this.#gameApi.getProgress();<br>}<br><br>getScore() {<br>  return this.#gameApi.getScore();<br>}<br><br>isLevelPassed() {<br>  return this.#gameApi.isLevelPassed();<br>}<br><br>remove() {<br>  if( null !== this.#visualTrackingCanvas.parentNode ) {<br>    this.#visualTrackingCanvas.parentNode.remove();<br>  }<br>}<br><br>show() {<br>  if( null !== this.#visualTrackingCanvas.parentNode ) {<br>    this.#visualTrackingCanvas.parentNode.classList.remove(&#39;game-container-hide&#39;);<br>  }<br>}</pre><p>Reload your browser ( <a href="http://127.0.0.1:8080/">http://127.0.0.1:8080/</a>), check ‘Use Object Recognition’ and click ‘Start evolution’ button. Everything should work now, if you let it go it will eventually solve all of the levels.<br> However… Wouldn’t it be nice to see what the ML is actually seeing on each game? Let’s add in some debugging info</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>drawRectOnCanvas(rect, color) {<br>  let context = this.#visualTrackingCanvas.getContext(&#39;2d&#39;);<br>  context.beginPath();<br>  context.strokeStyle = color;<br>  context.lineWidth = &quot;1&quot;;<br>  context.rect(rect.x, rect.y, rect.width, rect.height);<br>  context.stroke();<br>}<br><br>// Function responsible for drawing what the computer sees, we then use this to get the inputs for tensorflow<br>drawMachineVision() {<br>  if( this.#enableVision ) {<br>    // Clear everything first<br>    this.#visualTrackingCanvas.getContext(&#39;2d&#39;).clearRect(0, 0, this.#visualTrackingCanvas.width, this.#visualTrackingCanvas.height);<br><br>    // Draw player<br>    this.drawRectOnCanvas({<br>      x: this.#playerX,<br>      y: this.#playerY,<br>      width: this.#visualTrackingMapSize,<br>      height: this.#visualTrackingMapSize,<br>    }, this.#colors.visionOutline);<br><br>    // Draw map sections<br>    for (const xy in this.#visualTrackingMap) {<br>      let value = this.#visualTrackingMap[xy];<br><br>      if ( this.#colors.block == value) {<br>        let position = xy.split(&#39;x&#39;);<br>          this.drawRectOnCanvas({<br>          x: parseInt(position[0]),<br>          y: parseInt(position[1]),<br>          width: this.#visualTrackingMapSize,<br>          height: this.#visualTrackingMapSize<br>        }, this.#colors.visionOutline)<br>      }<br>    }<br><br><br>    this.drawRectOnCanvas({<br>      x: this.#sectionAhead.x,<br>      y: this.#sectionAhead.y,<br>      width: this.#sectionAhead.width,<br>      height: this.#sectionAhead.height,<br>    }, &#39;blue&#39;);<br>  }<br>}</pre><p>Then change the method getPlayerX() to look like this.</p><p><strong>js/ai/GameImageRecognition.js</strong></p><pre>getPlayerX() {<br>  this.extractVisualTrackingData();<br>  this.drawMachineVision();<br>  return this.#playerX;<br>}</pre><p>Reload your browser ( <a href="http://127.0.0.1:8080/">http://127.0.0.1:8080/</a>), check ‘Use Object Recognition’, check ‘Enable ML vision’ and click ‘Start evolution’ button. You should now see lots of red boxes that highlight what the ML is actually using as inputs. You’re browser will most likely struggle, however it will work.</p><p>I get consistent results along the lines of:</p><ul><li><strong>Level 1:</strong> Takes 10–25 generations</li><li><strong>Level 2:</strong> Takes 15–40 generations</li><li><strong>Level 3:</strong> Takes 40–400 generations (as it has to learn to jump blocks and gaps).</li></ul><h3>Congratulations!</h3><p>I hope this tutorial was useful for you to learn how to use TensorFlowJS to build a NeuroEvolution implementation. Any questions leave them in the comments below, or tweet me on twitter at <a href="https://twitter.com/dionbeetson">@dionbeetson</a></p><h3>Source code</h3><p>All source code for this tutorial can be found here <a href="https://github.com/dionbeetson/neuroevolution-experiment">https://github.com/dionbeetson/neuroevolution-experiment</a>.</p><p><em>Originally published at </em><a href="https://blog.dionbeetson.com/2019/07/neuroevolution-using-tensorflow-js-part.html"><em>https://blog.dionbeetson.com</em></a><em> on July 19, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c266b47d3ebc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NeuroEvolution using TensorFlow JS — Part 2]]></title>
            <link>https://medium.com/@dionbeetson/neuroevolution-using-tensorflow-js-part-2-b13d2861f4a9?source=rss-879af07f2512------2</link>
            <guid isPermaLink="false">https://medium.com/p/b13d2861f4a9</guid>
            <dc:creator><![CDATA[Dion Beetson]]></dc:creator>
            <pubDate>Mon, 27 May 2019 13:00:00 GMT</pubDate>
            <atom:updated>2022-01-08T09:11:20.535Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*z0IFs9HOQyIFAmj3.gif" /></figure><p>This tutorial is part 2, if you have not completed <a href="https://medium.com/@dionbeetson/neuroevolution-using-tensorflowjs-part-1-7e66ed42ccc0">NeuroEvolution using TensorFlowJS — Part 1</a>, I highly recommend you do that first. As it will:</p><ul><li>Explain how to setup the codebase</li><li>How to run the local dev environment</li><li>How to setup the basic NeruoEvolution implementation</li></ul><p>In this tutorial we will be:</p><ul><li>Adding visual debugging information</li><li>Adding more inputs for TensorFlow to use to predict jumping</li><li>Implement crossover within TensorFlowJS</li><li>Use the best progressing NeuralNetwork to ensure faster NeuroEvolution</li><li>Add saving and loading of NeuralNetwork models</li></ul><p>This tutorial is also based on the source code found on my GitHub account here <a href="https://github.com/dionbeetson/neuroevolution-experiment">https://github.com/dionbeetson/neuroevolution-experiment</a>.</p><h3>Add in visual/debugging</h3><p>We will be adding in useful debugging information to the right hand column and to the game itself.</p><h4>Chart the progress of each evolution</h4><p>We are going to use <a href="https://www.chartjs.org/">ChartJS</a> to track the progress (as a percentage) and the score for the best game of each evolution. Create a file called js/ai/NeuroEvolutionChart.js and copy the below logic into it. It is a basic ChartJS implementation that I won&#39;t go into detail in this tutorial.</p><p><strong>js/ai/NeuroEvolutionChart.js</strong></p><pre>class NeuroEvolutionChart {<br>  constructor() {<br>    this.chart = null;<br>    this.config = {<br>    type: &#39;line&#39;,<br>    data: {<br>     labels: [],<br>     datasets: [{<br>      label: &#39;&#39;,<br>      backgroundColor: &#39;#999999&#39;,<br>      borderColor: &#39;#EEEEEE&#39;,<br>          borderWidth: 3,<br>      data: [],<br>      fill: false,<br>     },{<br>      label: &#39;&#39;,<br>      backgroundColor: &#39;#91C7B1&#39;,<br>      borderColor: &#39;#A1CFBC&#39;,<br>          borderWidth: 2,<br>      data: [],<br>      fill: false,<br>     }]<br>    },<br>    options: {<br>     responsive: true,<br>     title: {<br>      display: false<br>     },<br>     tooltips: {<br>      mode: &#39;index&#39;,<br>      intersect: false,<br>     },<br>        legend: {<br>          display: false<br>        },<br>     hover: {<br>      mode: &#39;nearest&#39;,<br>      intersect: true<br>     },<br>     scales: {<br>      xAxes: [{<br>       display: false,<br>       scaleLabel: {<br>        display: true,<br>        labelString: &#39;Round&#39;<br>       }<br>      }],<br>      yAxes: [{<br>       display: true,<br>            ticks: {<br>              beginAtZero: true,<br>              min: 0,<br>              max: 100<br>            },<br>       scaleLabel: {<br>        display: false,<br>        labelString: &#39;Progress&#39;<br>       }<br>      },{<br>       display: true,<br>            position: &#39;right&#39;,<br>            ticks: {<br>              beginAtZero: true,<br>              min: 0,<br>              max: 200<br>            },<br>       scaleLabel: {<br>        display: false,<br>        labelString: &#39;Score&#39;<br>       }<br>      }]<br>     }<br>    }<br>   };<br><br>    this.setup();<br>  }<br><br>  setup(){<br>    var ctx = document.getElementById(&#39;trend&#39;).getContext(&#39;2d&#39;);<br>    this.chart = new Chart(ctx, this.config);<br>  }<br>}</pre><p>Now, add an update() method that will be responsible for pushing the data points onto the chart. It will also only keep 12 data points (the very first evolution, and the latest 11 evolutions). This is to avoid clutter on the page.</p><p><strong>js/ai/NeuroEvolutionChart.js</strong></p><pre>update(progress, score) {<br>  // Purge middle data (to avoid clutter)<br>  if( this.config.data.labels.length &gt; 12 ) {<br>    this.config.data.labels.splice(1, 1);<br>    this.config.data.datasets[0].data.splice(1, 1);<br>    this.config.data.datasets[1].data.splice(1, 1);<br>  }<br><br>  this.config.data.labels.push((this.config.data.labels.length+1))<br>  this.config.data.datasets[0].data.push(progress);<br>  this.config.data.datasets[1].data.push(score);<br><br>  this.chart.update();<br>}</pre><p>Back in the NeuroEvolution class add the following variable declaration to the top</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>#neuroEvolutionChart = new NeuroEvolutionChart();</pre><p>Then in the same file in the finishGeneration(games, timeTaken) method, right after the line this.#bestGames.sort(this.sortByFitness); add in.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>// Update UI - Chart this.#neuroEvolutionChart.update(bestPlayerByFitness.progress, bestPlayerByFitness.score);</pre><p>Reload your browser, and click the ‘start evolution’ button. After each evolution you will start to see the chart in the top right update.</p><h4>Additional debugging visuals</h4><p>Let’s quickly add in some more useful debug information which includes:</p><ul><li>The best game progress to date</li><li>Track the best score from each evolution</li><li>A progress bar in the header to track round and generation progress</li></ul><p>In the NeuroEvolution class add the below methods</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>updateUIaddBestGenerationToBestScore( pickBestPlayerByFitness, timeTaken ) {<br>  let bestScore = document.createElement(&quot;li&quot;);<br>  bestScore.innerHTML = pickBestPlayerByFitness.score + &#39; (&#39; + pickBestPlayerByFitness.progress.toFixed(1) + &#39;%) (&#39; + pickBestPlayerByFitness.fitness.toFixed(3) + &#39;) (&#39; + timeTaken + &#39;s)&#39;;<br>  this.#bestScores.insertBefore(bestScore, document.querySelector(&quot;li:first-child&quot;));<br>}<br><br>updateUIRoundInformation() {<br>  document.querySelector(&quot;#round-current&quot;).innerHTML = this.#generation;<br>  document.querySelector(&quot;#round-total&quot;).innerHTML = this.#maxGenerations;<br>  document.querySelector(&quot;#round-progress&quot;).style.width = &#39;0%&#39;;<br>  document.querySelector(&quot;#generation-progress&quot;).style.width = (this.#generation/this.#maxGenerations)*100 + &#39;%&#39;;<br>}<br><br>updateUIBestPlayerScore( bestGame ) {<br>  document.querySelector(&quot;#best-player-score&quot;).innerHTML = bestGame.score + &quot; points (&quot; + bestGame.progress.toFixed(1) + &quot;%)&quot;;<br>}</pre><p>In the first line of the start() method add:</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>processGeneration(games, bestPlayerBrainsByFitness) {<br>  this.updateUIRoundInformation();<br>  ...</pre><p>Then right after where you added in this.#neuroEvolutionChart.update... add in:</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>// Update UI<br>this.updateUIaddBestGenerationToBestScore(bestPlayerByFitness, timeTaken);<br>this.updateUIBestPlayerScore(this.#bestGames[0]);<br>this.updateUIRoundInformation();</pre><p>Lastly, add the following variable declaration to the top</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>#bestScores = null;</pre><p>and the following constructor</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>constructor() {<br>  this.#bestScores = document.querySelector(&quot;#best-scores&quot;);<br>}</pre><p>Reload your browser, and click the ‘start evolution’ button. You should start seeing debug information in your right hand column.</p><p>Another useful debug is to visualise the inputs that are being used to predict the jump() action.</p><p>Back in the Ai class within the think() method, just before the game.brain.predict() line, add in this code block to push debug points into the game canvas.</p><p><strong>js/ai/Ai.js</strong></p><pre>game.gameApi.setDebugPoints([<br>  {<br>    x: 0,<br>    y: inputs[0]<br>  },<br>  {<br>    x: inputs[2],<br>    y: 0<br>  },<br>  {<br>    x: 0,<br>    y: inputs[3]<br>  },<br>  {<br>   x: 0,<br>   y: inputs[4]<br>  }<br>]);</pre><p>Reload your browser, and click the ‘start evolution’ button. You should start seeing crosshairs for the inputs being used by TensorFlow JS on every prediction call.</p><p>Sometimes it is useful to be able to ‘ <strong>pause</strong> the game before the next evolution begins. You will see a checkbox in the left column labelled ‘Pause after generation’. Let’s wire it up, as it currently is not.</p><p>In ui.js add in the below change handler to capture user interaction.</p><p><strong>js/ai/ui.js</strong></p><pre>document.querySelector(&quot;#ml-pause-before-next-generation&quot;).addEventListener(&quot;change&quot;, function() {<br>  neuroEvolution.pauseBeforeNextGeneration = this.checked;<br><br>  if( false == neuroEvolution.pauseBeforeNextGeneration &amp;&amp; neuroEvolution.pausedGames.length &gt; 0 &amp;&amp; neuroEvolution.pausedBestNeuralNetworksByFitness.length &gt; 0 ) {<br>    neuroEvolution.start(neuroEvolution.pausedGames, neuroEvolution.pausedBestNeuralNetworksByFitness);<br>  }<br>});<br><br>neuroEvolution.pauseBeforeNextGeneration = document.querySelector(&quot;#ml-pause-before-next-generation&quot;).checked;</pre><p>This isn’t enough though, we need to build in the logic to actually pause the game. We are basically going to wrap the ai.start(bestPlayerBrainsByFitness); call within the start() method in NeuroEvolution.js in an IF statement based on if the pause checkbox is checked. If it is checked, we will then store the current games within a local class variable so we can retrieve it once the checkbox is unchecked.</p><p>The completed method looks like this now.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>start(games, bestPlayerBrainsByFitness) {<br>  this.updateUIRoundInformation();<br><br>  if ( this.#generation &lt; this.#maxGenerations ) {<br><br>    if( false == this.#pauseBeforeNextGeneration ){<br>      for ( let i = 0; i &lt; games.length; i++ ) {<br>        games[i].gameApi.remove();<br>      }<br><br>      games = undefined;<br><br>      this.#pausedGames = [];<br>      this.#pausedBestNeuralNetworksByFitness = [];<br><br>      this.#generation++;<br><br>      const ai = new Ai(this.finishGeneration.bind(this));<br>      ai.start(this.#useImageRecognition, bestPlayerBrainsByFitness);<br><br>    } else {<br>      this.#pausedGames = games;<br>      this.#pausedBestNeuralNetworksByFitness = bestPlayerBrainsByFitness;<br><br>      for ( let i = 0; i &lt; games.length; i++ ) {<br>        games[i].gameApi.show();<br>      }<br>    }<br>  } else {<br>    this.enableSpeedInput();<br>  }<br>}</pre><p>Then add in the following variable declarations in the NeuroEvolution class.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>#pausedGames = [];<br>#pausedBestNeuralNetworksByFitness = [];<br>#pauseBeforeNextGeneration = false;</pre><p>And then add in the required getters/setters in the NeuroEvolution class.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>get pauseBeforeNextGeneration() {<br>  return this.#pauseBeforeNextGeneration;<br>}<br><br>set pauseBeforeNextGeneration( pauseBeforeNextGeneration ) {<br>  this.#pauseBeforeNextGeneration = pauseBeforeNextGeneration;<br>}<br><br>get pausedGames() {<br>  return this.#pausedGames;<br>}<br><br>get pausedBestNeuralNetworksByFitness() {<br>  return this.#pausedBestNeuralNetworksByFitness;<br>}<br><br>get bestGames() {<br>  return this.#bestGames;<br>}<br><br>set pauseBeforeNextGeneration( pauseBeforeNextGeneration ) {<br>  this.#pauseBeforeNextGeneration = pauseBeforeNextGeneration;<br>}</pre><h3>Speeding up NeuroEvolution</h3><p>Ok that is debugging done for now. Let’s move onto improving our NeuroEvolution to solve this game a little faster. This involves 3 things:</p><ol><li>Adding more inputs for TensorFlow to use to predict jumping</li><li>After each evolution randomly choosing to use the ‘best game to date’, as this can keep things on track if we have a single bad evolution randomly (trust me it happens)</li><li>Implementing a crossover method so instead of mutating <strong>only 1</strong> of the best games, we take 2 of the best games and breed a child NeuralNetwork from their 2 results (which is called a crossover)</li><li>If a game passes the level, don’t mutate or crossover, as we already have a successful NeuralNetwork</li></ol><h3>Adding more inputs for TensorFlow JS</h3><p>In the Ai class we are going to add in 3 more inputs for (isPlayerJumping, playerVelocity &amp; canPlayerJump). Within the think() method after the existing 5 inputs, add in these additional inputs.</p><p><strong>js/ai/Ai.js</strong></p><pre>// Is player jumping<br>inputs[5] = (game.gameApi.isPlayerJumping() ? 1 : 0);<br>inputsNormalised[5] = map(inputs[5], 0, 1, 0, 1);<br><br>// Player velocity<br>inputs[6] = (game.gameApi.getPlayerVelocity() ? 1 : 0);<br>inputsNormalised[6] = map(inputs[6], -1.1, 1.1, 0, 1);<br><br>// Can play jump?<br>inputs[7] = (game.gameApi.canPlayerJump() ? 1 : 0);<br>inputsNormalised[7] = map(inputs[7], 0, 1, 0, 1);</pre><p>Then change the variable declaration for inputs in Ai class to 8</p><p><strong>js/ai/Ai.js</strong></p><pre>#inputs = 8;</pre><h3>Implement crossover within TensorFlowJS</h3><p>In the NeuroEvolution class within the completeGeneration() method, we are going to modify the <strong>breeding</strong> section to make it a little smarter. Make the for loop look like this.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>// Breeding<br>for (let i = 0; i &lt; games.length; i++) {<br>  let bestPlayerA = this.pickBestGameFromFitnessPool(games);<br>  let bestPlayerB = this.pickBestGameFromFitnessPool(games);<br>  let child;<br><br>  child = this.mutateNeuralNetwork(this.crossoverNeuralNetwork(bestPlayerA.neuralNetwork.clone(), bestPlayerB.neuralNetwork.clone()));<br> <br>  bestPlayerBrainsByFitness.push(child);<br>}</pre><p>Now we need to add in the crossover method to the same file.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>crossoverNeuralNetwork(neuralNetworkOne, neuralNetworkTwo) {<br>  let parentA_in_dna = neuralNetworkOne.input_weights.dataSync();<br>  let parentA_out_dna = neuralNetworkOne.output_weights.dataSync();<br>  let parentB_in_dna = neuralNetworkTwo.input_weights.dataSync();<br>  let parentB_out_dna = neuralNetworkTwo.output_weights.dataSync();<br><br>  let mid = Math.floor(Math.random() * parentA_in_dna.length);<br>  let child_in_dna = [...parentA_in_dna.slice(0, mid), ...parentB_in_dna.slice(mid, parentB_in_dna.length)];<br>  let child_out_dna = [...parentA_out_dna.slice(0, mid), ...parentB_out_dna.slice(mid, parentB_out_dna.length)];<br><br>  let child = neuralNetworkOne.clone();<br>  let input_shape = neuralNetworkOne.input_weights.shape;<br>  let output_shape = neuralNetworkOne.output_weights.shape;<br><br>  child.dispose();<br><br>  child.input_weights = tf.tensor(child_in_dna, input_shape);<br>  child.output_weights = tf.tensor(child_out_dna, output_shape);<br><br>  return child;<br>}</pre><p>Reload your browser, and click the ‘start evolution’ button. You should start seeing games solve themselves a bit faster in fewer generations (normally around 20).</p><h3>10% chance to crossover best game</h3><p>Back in the NeuroEvolution class, within breeding section, let&#39;s add one final update so that on random occasions (10% chance), we do a crossover with the best game to date. Make the breeding section look like this</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>// Breeding<br>for (let i = 0; i &lt; games.length; i++) {<br>  let bestPlayerA = this.pickBestGameFromFitnessPool(games);<br>  let bestPlayerB = this.pickBestGameFromFitnessPool(games);<br>  let bestPlayerC = this.#bestGames[0];<br>  let child;<br><br>  if ( random(1) &lt; 0.1) {<br>    const ai = new Ai();<br>    let bestPlayerD = new NeuralNetwork(ai.inputs, ai.neurons, ai.outputs);<br>    child = this.mutateNeuralNetwork(this.crossoverNeuralNetwork(bestPlayerC.neuralNetwork.clone(), bestPlayerD));<br>  } else {<br>    child = this.mutateNeuralNetwork(this.crossoverNeuralNetwork(bestPlayerA.neuralNetwork.clone(), bestPlayerB.neuralNetwork.clone()));<br>  }<br><br>  bestPlayerBrainsByFitness.push(child);<br>}</pre><p>We also need to expose getters in the Ai class for the input, neuron &amp; output variables. Add the following methods:</p><p><strong>js/ai/Ai.js</strong></p><pre>get inputs(){<br>  return this.#inputs;<br>}<br><br>get neurons(){<br>  return this.#neurons;<br>}<br><br>get outputs(){<br>  return this.#outputs;<br>}</pre><p>Reload your browser, and click the ‘start evolution’ button. You should start seeing games solve themselves a bit faster in fewer generations (normally around 10–15).</p><h3>Don’t mutate on success</h3><p>Now wrap the <strong>Breed</strong> functionality in an IF statement to ensure that if a game passes the level, we don&#39;t mutate or crossover, we just continue using the successful NeuralNetwork.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>if ( false != gamePassedLevel ) {<br>  for ( let i = 0; i &lt; games.length; i++ ) {<br>    if (games[i].gameApi.isLevelPassed() ) {<br>      games[i].neuralNetwork.save(&#39;neuralNetwork&#39;);<br>      for (let ii = 0; ii &lt; games.length; ii++) {<br>          bestPlayerBrainsByFitness.push(games[i].neuralNetwork.clone());<br>        }<br>      }<br>    }<br><br>    console.log(&#39;Level Passed:&#39;, this.#bestGames[0], this.#bestGames.length, this.#bestGames);<br>    this.start(games, bestPlayerBrainsByFitness);<br>  } else {<br>    // Breeding<br>    for (let i = 0; i &lt; games.length; i++) {<br>      let bestPlayerA = this.pickBestGameFromFitnessPool(games);<br>      let bestPlayerB = this.pickBestGameFromFitnessPool(games);<br>      let bestPlayerC = this.#bestGames[0];<br>      let child;<br><br>      if ( random(1) &lt; 0.1) {<br>        const ai = new Ai();<br>        let bestPlayerD = new NeuralNetwork(ai.inputs, ai.neurons, ai.outputs);<br>        child = this.mutateNeuralNetwork(this.crossoverNeuralNetwork(bestPlayerC.neuralNetwork.clone(), bestPlayerD));<br>      } else {<br>        child = this.mutateNeuralNetwork(this.crossoverNeuralNetwork(bestPlayerA.neuralNetwork.clone(), bestPlayerB.neuralNetwork.clone()));<br>      }<br><br>      bestPlayerBrainsByFitness.push(child);<br>    }<br> <br>   this.start(games, bestPlayerBrainsByFitness);<br>}</pre><p>Reload your browser, and click the ‘start evolution’ button. Once enough evolutions process to pass the level, you will notice that all future games in evolutions will start passing consistently.</p><p>This is a far as we will take this tutorial in regards to evolving and speeding up our NeuroEvolution logic. Next we will move onto saving/loading our NeuralNetwork models.</p><h3>Saving &amp; Loading our NeuralNetwork models</h3><p>The last piece to implement is the saving and loading functionality of our NeuralNetwork. This is so once you have a NeuralNetwork that works you can save it and load it back when needed. We will be implementing the:</p><ol><li>Ability to automatically save a NeuralNetwork that passes to localStorage</li><li>Ability to manually save best NeuralNetwork to localStorage</li><li>Ability to manually save best NeuralNetwork to disk</li><li>Ability to load from local storage</li><li>Ability to load from disk</li></ol><p>First we need to add the ability to save within the NeuralNetwork.js file. Add these 2 methods</p><p><strong>js/ai/NeuralNetwork.js</strong></p><pre>stringify() {<br>  let neuralNetworkToSave = {<br>    input_weights: this.input_weights.arraySync(),<br>    output_weights: this.output_weights.arraySync()<br>  };<br><br>  return JSON.stringify(neuralNetworkToSave);<br>}<br><br>save( key ) {<br>  localStorage.setItem(key, this.stringify());<br>}</pre><h4>Automatically save a NeuralNetwork that passes to localStorage</h4><p>In the NeuroEvolution. class within the method finishGeneration(). Add in the below to the IF statement when a game level is passed.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>games[i].neuralNetwork.save(&#39;neuralNetwork&#39;);</pre><p>So it should look like this:</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>for ( let i = 0; i &lt; games.length; i++ ) {<br>  if (games[i].gameApi.isLevelPassed() ) {<br>    games[i].neuralNetwork.save(&#39;neuralNetwork&#39;);<br>    for (let ii = 0; ii &lt; games.length; ii++) {<br>      bestPlayerBrainsByFitness.push(games[i].neuralNetwork.clone());<br>    }<br>  }<br>}<br><br>console.log(&#39;Level Passed:&#39;, this.#bestGames[0], this.#bestGames.length, this.#bestGames);<br>this.start(games, bestPlayerBrainsByFitness);</pre><p>Your game will now automatically save the NeuralNetwork model to localStorage key neuralNetwork if it passes the level.</p><h4>Manually save best NeuralNetwork to localStorage</h4><p>To manually save to local storage add the below code to ui.js</p><p><strong>js/ai/ui.js</strong></p><pre>document.querySelector(&quot;#btn-ml-save-neuralnetwork-localstorage&quot;).addEventListener(&#39;click&#39;, function (event) {<br>  if( neuroEvolution.bestGames.length &gt; 0 ) {<br>    neuroEvolution.bestGames[0].neuralNetwork.save(&#39;neuralNetwork&#39;)<br>  }<br>});</pre><h4><strong>Manually save best NeuralNetwork to disk</strong></h4><p>To manually save to disk you can add the below code to ui.js</p><p><strong>js/ai/ui.js</strong></p><pre>document.querySelector(&quot;#btn-ml-save-neuralnetwork-disk&quot;).addEventListener(&#39;click&#39;, function (event) {<br>  if( neuroEvolution.bestGames.length &gt; 0 ) {<br>    let neuralNetworkJSON = neuroEvolution.bestGames[0].neuralNetwork.stringify();<br>    let a = document.createElement(&quot;a&quot;);<br>    let file = new Blob([neuralNetworkJSON], {type: &#39;application/json&#39;});<br>    a.href = URL.createObjectURL(file);<br>    a.download = &#39;neuralNetwork.json&#39;;<br>    a.click();<br>  }<br>});</pre><h4>Loading NeuralNetwork Models</h4><p>To load from local storage or disk we need to create a method to handle loading a NeuralNetwork into memory. Create the below method in ui.js (there is probably a better file location for this method now that I think about it).</p><p><strong>js/ai/ui.js</strong></p><pre>const loadNeuralNetwork = (neuralNetworkData) =&gt; {<br>  const ai = new Ai();<br>  let neuralNetwork = new NeuralNetwork(ai.inputs, ai.neurons, ai.outputs);<br>  neuralNetwork.dispose();<br><br>  neuralNetwork.input_weights = tf.tensor(neuralNetworkData.input_weights);<br>  neuralNetwork.output_weights = tf.tensor(neuralNetworkData.output_weights);<br><br>  let neuralNetworks = [];<br>  for ( let i = 0; i &lt; ai.totalGames; i++ ) {<br>    neuralNetworks.push(neuralNetwork);<br>  }<br><br>  let games = neuroEvolution.pausedGames;<br>  neuroEvolution.pausedGames;<br>  neuroEvolution.reset();<br><br>  neuroEvolution.start(games, neuralNetworks);<br>};</pre><p>We also need to add the below method into the Ai class.</p><p><strong>js/ai/Ai.js</strong></p><pre>get totalGames(){<br>  return this.#totalGames;<br>}</pre><h4>Load from local storage</h4><p>Now to load from localStorage into memory, add the below into ui.js</p><p><strong>js/ai/ui.js</strong></p><pre>document.querySelector(&quot;#btn-ml-load-neuralnetwork-localstorage&quot;).addEventListener(&#39;click&#39;, function (event) {<br>  cachedLevelSections = [];<br>  let games = neuroEvolution.pausedGames;<br>  neuroEvolution.pausedGames;<br>  neuroEvolution.reset();<br><br>  let neuralNetworkData = localStorage.getItem(&#39;neuralNetwork&#39;);<br><br>  loadNeuralNetwork(JSON.parse(neuralNetworkData));<br>});</pre><p>We also need to add the reset method into the NeuroEvolution class.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>reset() {<br>  this.#bestGames = [];<br>  this.#generation = 1;<br>  this.#pausedGames = [];<br>  this.#pausedBestNeuralNetworksByFitness = [];<br>  this.#pauseBeforeNextGeneration = false;<br>}</pre><h4>Load from disk</h4><p>Now to load from disk into memory, add the below into ui.js</p><p><strong>js/ai/ui.js</strong></p><pre>document.querySelector(&quot;#btn-ml-load-neuralnetwork-disk&quot;).addEventListener(&#39;change&#39;, function (event) {<br>  let file = this.files[0];<br>  let reader = new FileReader()<br>  let textFile = /application\/json/;<br>  let fileText = &#39;&#39;;<br><br>  if (file.type.match(textFile)) {<br>    reader.onload = function (event) {<br>       let importedNeuralNetwork = JSON.parse(event.target.result);<br><br>       loadNeuralNetwork(importedNeuralNetwork);<br>    }<br>  }<br><br>  reader.readAsText(file);<br>});</pre><p>Reload your browser, and click the ‘start evolution’ button. You will notice if you inspect Chrome DevTools you will see a localStorage key called ‘brain’ once a level is passed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*-uSRb1yj25rcWTdX.png" /></figure><p>You can also now also use the buttons in the left hand menu to <strong>save</strong> and <strong>load</strong> the best NeuralNetwork to disk.</p><p>Within the GitHub codebase within the models/ directory you will see 3 existing models that can be loaded up to pass levels 1, 2 &amp; 3.</p><p>Try it yourself and get your NeuroEvolution implementation to pass Level 1, then Level 2, then finally Level 3.</p><p>I get consistent results along the lines of:</p><ul><li><strong>Level 1:</strong> Takes 5–20 generations</li><li><strong>Level 2:</strong> Takes 5–20 generations</li><li><strong>Level 3:</strong> Takes 40–700 generations (as it has to learn to jump blocks and gaps).</li></ul><h3>Congratulations!</h3><p>I hope this tutorial was useful for you to learn how to use TensorFlowJS to build a NeuroEvolution implementation. Any questions leave them in the comments below, or tweet me on twitter at <a href="https://twitter.com/dionbeetson">@dionbeetson</a></p><h3>Source code</h3><p>All source code for this tutorial can be found here <a href="https://github.com/dionbeetson/neuroevolution-experiment">https://github.com/dionbeetson/neuroevolution-experiment</a>.</p><h3>Part 3 — coming soon…</h3><p>Coming soon and will be using image recognition to solve the same browser based game.</p><p><em>Originally published at </em><a href="https://blog.dionbeetson.com/2019/05/neuroevolution-using-tensorflow-js-part-2.html"><em>https://blog.dionbeetson.com</em></a><em> on May 27, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b13d2861f4a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NeuroEvolution using TensorFlowJS — Part 1]]></title>
            <link>https://medium.com/@dionbeetson/neuroevolution-using-tensorflowjs-part-1-7e66ed42ccc0?source=rss-879af07f2512------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e66ed42ccc0</guid>
            <dc:creator><![CDATA[Dion Beetson]]></dc:creator>
            <pubDate>Sun, 26 May 2019 13:00:00 GMT</pubDate>
            <atom:updated>2022-01-08T09:14:19.748Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*-4yGxTcyx8PbJhwv.gif" /></figure><p>This tutorial will walk you through how to implement NeuroEvolution using TensorFlow JS. This NeuralNetwork will learn how to play a simple browser based game that requires a player to jump over blocks and gaps. It is based on the source code found on my GitHub account here <a href="https://github.com/dionbeetson/neuroevolution-experiment">https://github.com/dionbeetson/neuroevolution-experiment</a>.</p><p>The difference in this solution compared to other solutions out there is that instead of building ML directly into the core game source code (like I saw in a lot of examples); the ML and Game source code are completely decoupled. This simulates a much more realistic real world example, as you would rarely have direct access to the source code when applying ML (you would only have access to specific inputs). I created an an API/SDK for the browser based game to expose some key functionality (the inputs the NeuralNetwork needs), but end state is to use image analysis so this solution is more adaptable to other applications.</p><h3>References</h3><p>Although I wrote this codebase myself I took a lot of input/influence from a huge range of great tutorials that already exist. You can see a list of the key tutorials/repos I learned from on the GitHub Readme for this project <a href="https://github.com/dionbeetson/neuroevolution-experiment">here</a>.</p><h3>Setup</h3><p>Clone this repo from GitHub:</p><pre>git clone git@github.com:dionbeetson/neuroevolution-experiment.git</pre><p>Then run in terminal the below to setup the repository for this tutorial:</p><pre>npm install<br>npm run setup-tutorial</pre><p>This basically moves the js/ai directory to js/ai-source, and leaves everything else in place. If you ever need to refer back to the original source code use js/ai-source as reference.</p><p>Now run the below to startup the app:</p><pre>npm run dev</pre><p>The browser will load up the application, but you will see 404 errors within the DevTools console. You will though however, have a working browser game. Click <strong>‘Play game’</strong>, and use the spacebar to jump to test things out. Throughout this tutorial you are going to be building a <a href="https://en.wikipedia.org/wiki/Neuroevolution">NeuroEvolution</a> implementation to learn how to automatically solve this game.</p><h3>Quick overview of the UI</h3><p>Left section: Includes base controls for Manual and AI interactions. In order from top to bottom</p><ul><li><strong>Level:</strong> You can select the level (there are currently 3 levels available in the game in order of complexity)</li><li><strong>Play game:</strong> Play an user interacted game</li><li><strong>Enable drawing: </strong>Whether you want to render the drawing on canvas</li><li><strong>Speed:</strong> How fast the game will run</li><li><strong>Start Evolution:</strong> Start the NeuroEvolution</li><li><strong>Save to localStorage:</strong> Save best current NeuralNetwork into localStorage</li><li><strong>Save to disk:</strong> Save best current NeuralNetwork to disk</li><li><strong>Load from localStorage:</strong> Load current NeuralNetwork in local storage into memory and start NeuroEvolution</li><li><strong>Load from disk:</strong> Load a NeuralNetwork from disk into memory and start NeuroEvolution</li><li><strong>Pause after generation:</strong> For debugging, pause after each evolution so you can see / debug it</li></ul><p>Middle section: The game(s) render within the large middle column</p><p>Right section: Is used to display debugging information for the NeuroEvolution implementation</p><h3>This tutorial has 4 key files</h3><p>For the entire tutorial we will be working within the js/ai/ directory.</p><ol><li><strong>ui.js:</strong> Controls the UI interactions</li><li><strong>NeuralNetwork.js:</strong> Is the base neural network</li><li><strong>Ai.js:</strong> This class instantiates (by default 9) games &amp; corresponding NeuralNetwork instances</li><li><strong>Neuroevolution.js:</strong> This class will run up to 1500 generations of Ai.js until it successfully passes the selected level</li></ol><h3>Setup base structure</h3><p>Let’s start to build out enough base logic to be able to predict if we should jump or not using TensorFlowJS.</p><p>Create a directory js/ai, and create a file called js/ai/ui.js which will handle basic UI controls. Within this file we want to instantiate the NeuroEvolution class (that we are about to create, and bind a UI button to start the evolution process).</p><p><strong>js/ai/ui.js</strong></p><pre>let neuroEvolution = new NeuroEvolution(); </pre><pre>document.querySelector(&quot;#btn-ml-start&quot;).addEventListener(&#39;click&#39;, function (event) {<br>  neuroEvolution.start([]);<br>}, false);</pre><p>Now we need to create the skeleton of NeuralNetwork.js, Ai.js &amp; Neuroevolution.js.</p><p>For the NeuralNetwork let’s just start with a constructor that allows us to send input_node/hidden_node &amp; output_node count arguments.</p><p><strong>js/ai/NeuralNetwork.js</strong></p><pre>class NeuralNetwork {<br>  constructor(input_nodes, hidden_nodes, output_nodes) {<br>    // The amount of inputs (eg: player y position, height of next block etc..) <br>    this.input_nodes = input_nodes;</pre><pre>    // Amount of hidden nodes within the Neural Network)<br>    this.hidden_nodes = hidden_nodes;</pre><pre>    // The amount of outputs, we will use 2 (will be needed for level 3)<br>    this.output_nodes = output_nodes;<br>    <br>    // Initialize random weights<br>    this.input_weights = tf.randomNormal([this.input_nodes, this.hidden_nodes]);<br>    this.output_weights = tf.randomNormal([this.hidden_nodes, this.output_nodes]);<br>  }<br>}</pre><p>Now we need to create the Ai class. This will include a method that will create 1 game for now and link it with a NeuralNetwork instance. We will start with a start() method to create instances of a NeuralNetwork, a checkGame() method that will be called every 50ms which will call the think() method that we will expand to make the actual prediction to jump or not jump.</p><p><strong>js/ai/Ai.js</strong></p><pre>class Ai {<br>  #totalGames = 1; // Will increase to 9 games later<br>  #inputs = 5;<br>  #neurons = 40;<br>  #outputs = 2;<br>  #games = [];<br>  #gamesRunning = 0;<br>  #sectionsToSeeAhead = 1;<br>  #timeTakenDateStart = null;<br><br>  start(useImageRecognition, neuralNetworks, completeCallback) {<br>    this.#timeTakenDateStart = new Date();<br><br>    for ( let i = 0; i &lt; this.#totalGames; i++ ) {<br>      let neuralNetwork;<br><br>      if ( undefined !== neuralNetworks &amp;&amp; neuralNetworks[i] instanceof NeuralNetwork ) {<br>        neuralNetwork = neuralNetworks[i];<br>      } else {<br>        neuralNetwork = new NeuralNetwork(this.#inputs, this.#neurons, this.#outputs);<br>      }<br><br>      let gameApi;<br><br>      if ( useImageRecognition ) {<br>        gameApi = new GameImageRecognition();<br>      } else {<br>        gameApi = new GameApi();<br>      }<br><br>      this.#games[i] = {<br>        gameApi: gameApi,<br>        neuralNetwork: neuralNetwork,<br>        interval: null<br>      }<br><br>      // Debug look ahead<br>      this.#games[i].gameApi.setHighlightSectionAhead(this.#sectionsToSeeAhead)<br><br>      // Start game<br>      this.#gamesRunning++;<br>      this.#games[i].gameApi.start();<br><br>      this.#games[i].interval = setInterval(this.checkGame.bind(null, this, this.#games, this.#games[i]), 50);<br>    }<br>  }<br><br>  checkGame(ai, games, game) {<br>    if( game.gameApi.isSetup() ) {<br>      ai.think(game);<br>    }<br>  }<br><br>  think(game) {<br>  }<br>}</pre><p>Now create the NeuroEvolution class that will be responsible for running multiple evolutions of Ai.js until we get an evolution that passes the level.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>class NeuroEvolution {<br>  #generation = 1;<br>  #maxGenerations = 1500;<br>  #useImageRecognition = false;<br><br>  start(games, bestPlayerBrainsByFitness) {<br><br>    if ( this.#generation &lt; this.#maxGenerations ) {<br><br>      for ( let i = 0; i &lt; games.length; i++ ) {<br>        games[i].gameApi.remove();<br>      }<br><br>      games = undefined;<br><br>      this.#generation++;<br><br>      const ai = new Ai(this.finishGeneration.bind(this));<br>      ai.start(this.#useImageRecognition, bestPlayerBrainsByFitness);<br>    } else {<br>      this.enableSpeedInput();<br>    }<br>  }<br><br>  finishGeneration(games, timeTaken) {<br>   // TODO<br>  }<br><br>  updateUIRoundInformation() {<br>    document.querySelector(&quot;#round-current&quot;).innerHTML = this.#generation;<br>    document.querySelector(&quot;#round-total&quot;).innerHTML = this.#maxGenerations;<br>    document.querySelector(&quot;#round-progress&quot;).style.width = &#39;0%&#39;;<br>    document.querySelector(&quot;#generation-progress&quot;).style.width = (this.#generation/this.#maxGenerations)*100 + &#39;%&#39;;<br>  }<br>}</pre><p>Now you have your base structure in place.</p><p>Reload your browser ( <a href="http://127.0.0.1:8080/">http://127.0.0.1:8080/</a>), and click the ‘Start evolution’ button. You should see 1 game playing, and failing (no jumping), no matter how many times you reload the page. That is because we haven’t implemented the prediction logic. Let’s implement that using TensorFlowJS in a moment.</p><p>Quickly, before that, in the Ai class, let’s create 9 games at once instead of 1, as NeuroEvolution will be incredibly slow with only 1 NeuralNetwork at a time. Depending on your computer specs you can increase this to a higher number. I did find that 9 worked quite well though.</p><p><strong>js/ai/Ai.js</strong></p><pre>#totalGames = 9</pre><p>Reload your browser, and click ‘Start evolution’ button again. You should see 9 games playing.</p><h3>Predicting whether to jump or not</h3><p>To use TensorflowJS to predict to jump, we need to modify a few files. First in Ai.js in the think() method add the below logic in. Essentially we are accessing public information that the JS game exposes in js/game/GameAPI.js (like player position, next block height etc..) and then normalising all inputs to be between 0-1 as TensorFlow JS requires this.</p><p><strong>js/ai/Ai.js</strong></p><pre>think(game) {<br>  let inputs = [];<br>  let inputsNormalised = [];<br><br>  // Player y<br>  inputs[0] = (game.gameApi.getPlayerY());<br>  inputsNormalised[0] = map(inputs[0], 0, game.gameApi.getHeight(), 0, 1);<br><br>  // Player x<br>  inputs[1] = game.gameApi.getPlayerX();<br>  inputsNormalised[1] = map(inputs[1], inputs[1], game.gameApi.getWidth(), 0, 1);<br><br>  let section = game.gameApi.getSectionFromPlayer(this.#sectionsToSeeAhead);<br><br>  // 2nd closest section x<br>  inputs[2] = section.x + section.width;<br>  inputsNormalised[2] = map(inputs[2], inputs[1], game.gameApi.getWidth(), 0, 1);<br><br>  // 2nd closest section y<br>  inputs[3] = section.y;<br>  inputsNormalised[3] = map(inputs[3], 0, game.gameApi.getHeight(), 0, 1);<br><br>  // 2nd closest section y base<br>  inputs[4] = section.y + section.height;<br>  inputsNormalised[4] = map(inputs[4], 0, game.gameApi.getHeight(), 0, 1);<br><br>  // Call the predict function in the NeuralNetwork<br>  let outputs = game.neuralNetwork.predict(inputsNormalised);<br><br>  // If input is about 0.5 then jump, if not, stay still<br>  if ( outputs[0] &gt; 0.5 || outputs[1] &gt; 0.5 ) {<br>    game.gameApi.jump();<br>  }<br>}</pre><p>We also will need to create the predict() method in the NeuralNetwork class. This method creates an input_layer, hidden_layer and output_layer and returns 2 predictions (which are both numbers between 0-1).</p><p><strong>js/ai/NeuralNetwork.js</strong></p><pre>predict(user_input) {<br>  let output;<br>  tf.tidy(() =&gt; {<br>    let input_layer = tf.tensor(user_input, [1, this.input_nodes]);<br>    let hidden_layer = input_layer.matMul(this.input_weights).sigmoid();<br>    let output_layer = hidden_layer.matMul(this.output_weights).sigmoid();<br>    output = output_layer.dataSync();<br>  });<br>  return output;<br>}</pre><p>Reload your browser, and click the ‘start evolution’ button.</p><p>You will now see 9 games playing, and some should be jumping, and some not. This is completely random. Reload a few times if you need to to ensure this is the behaviour you are seeing.</p><p>Once all of your players die you will notice the game stops. As of now, we have used TensorFlow JS to make a prediction about whether to jump or not. This is based on a set of inputs (player X, player Y &amp; section ahead height/Y).</p><p>Now we need to add in the NeuroEvolution aspect so that we pick the best progressed games and start a new evolution with those NeuralNetworks (but we will evolve/mutate them slightly before hand).</p><p>In NeuroEvolution add a method called calculateFitness(). This method iterates through all games once finished, creates a fitness rating which is just the % progress of the game. It then has logic to increase the fitness of better performing games, so that later on in our logic, there is a higher chance the better performing NeuralNetworks are chosen for the next evolution. Hope that all makes sense.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>calculateFitness(games) {<br>  for ( let i = 0; i &lt; games.length; i++ ) {<br>    let game = games[i];<br>    games[i].fitness = game.gameApi.getProgress() / 100;<br>    games[i].score = game.gameApi.getScore();<br>    games[i].progress = game.gameApi.getProgress();<br>  }<br><br>  // The below code makes the better progressed games have a higher fitness so they have a higher chance of being selected for next generation<br>  games.sort(this.sortByFitness);<br>  games.reverse();<br><br>  let prev = 0;<br>  for ( let i = 0; i &lt; games.length; i++ ) {<br>    games[i].fitness = this.#discountRate * prev + games[i].fitness;<br>    prev = games[i].fitness;<br>  }<br><br>  games.sort(this.sortByFitness);<br><br>  return games;<br>}</pre><p>In NeuroEvolution.js add a method that will check if at least 1 game has passed the level in the current evolution.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>didAtLeastOneGameCompleteLevel(games) {<br>  for ( let i = 0; i &lt; games.length; i++ ) {<br>    if (games[i].gameApi.isLevelPassed() ) {<br>      return games[i];<br>    }<br>  }<br><br>  return false;<br>}</pre><p>In NeuroEvolution add a method that will pick the <strong>exact</strong> best player with the highest fitness. We use this to display and keep a track of the best performing NeuralNetwork.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>pickBestGameByActualFitness(games){<br>  let game;<br>  let prevFitness = 0;<br>  for ( let i = 0; i &lt; games.length; i++ ) {<br>    if (games[i].fitness &gt; prevFitness) {<br>      game = games[i];<br>      prevFitness = game.fitness;<br>    }<br>  }<br><br>  return game;<br>}</pre><p>In NeuroEvolution add a method that will pick <strong>one</strong> (not always the best like the above method) of the best players with the highest fitness</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>pickBestGameFromFitnessPool(games) {<br>  let index = 0;<br>  let r = random(1);<br><br>  while (r &gt; 0 ) {<br>    if( undefined !== games[index] ) {<br>      r = r - games[index].fitness;<br>      index++;<br>    } else {<br>      r = 0;<br>    }<br>  }<br>  index--;<br><br>  let game = games[index];<br><br>  return game;<br>}</pre><p>In NeuroEvolution we can now improve the finishGeneration() method with the below logic. This code will:</p><ol><li>Calculate fitness for all 9 finished games</li><li>Store the best player in a local variable</li><li>Add the best player to the bestGames[] array</li><li>Ensure we only keep the best 5 games (for memory/performance reasons)</li><li>Iterate over all games and mutate the best performing NeuralNetworks into new NeuralNetworks for the next evolution</li><li>Start the next NeuroEvolution</li></ol><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>finishGeneration(games, timeTaken) {<br>  games = this.calculateFitness(games);<br><br>  // Did one of the games finish?<br>  let gamePassedLevel = this.didAtLeastOneGameCompleteLevel(games);<br><br>  let bestPlayerByFitness = gamePassedLevel;<br>  let bestPlayerBrainsByFitness = [];<br><br>  if( false === bestPlayerByFitness ){<br>    bestPlayerByFitness = this.pickBestGameByActualFitness(games);<br>  }<br><br>  this.#bestGames.push(bestPlayerByFitness);<br>  this.#bestGames.sort(this.sortByFitness);<br><br>  // Only keep top 5 best scores<br>  if( this.#bestGames.length &gt; 5 ) {<br>    this.#bestGames = this.#bestGames.slice(0, 5);<br>  }<br><br>  // Breeding<br>  for (let i = 0; i &lt; games.length; i++) {<br>    let bestPlayerA = this.pickBestGameFromFitnessPool(games);<br>    let child;<br><br>    child = this.mutateNeuralNetwork(bestPlayerA.neuralNetwork.clone());<br><br>    bestPlayerBrainsByFitness.push(child);<br>  }<br><br>  this.start(games, bestPlayerBrainsByFitness);<br>}</pre><p>Lastly, in NeuroEvolution you will need to add the below variable declarations.</p><p><strong>js/ai/NeuroEvolution.js</strong></p><pre>#discountRate = 0.95;<br>#bestGames = [];</pre><p>Reload your browser, and click the ‘start evolution’ button. You should again see 9 games playing, some will also be jumping like last time. However, it still wont progress past the first evolution. This is because we need to wire up Ai.js to call the callback method in the checkGame method and add some logic to check if the game is over. Let&#39;s update the checkGame() method to look like the below.</p><p><strong>js/ai/Ai.js</strong></p><pre>checkGame(ai, games, game) {<br>  if( game.gameApi.isOver() ) {<br>    clearInterval(game.interval);<br><br>    ai.#gamesRunning--;<br>    document.querySelector(&quot;#round-progress&quot;).style.width = ((games.length-ai.#gamesRunning)/games.length)*100 + &#39;%&#39;;<br><br>    if( ai.areAllGamesOver(games) &amp;&amp; 0 == ai.#gamesRunning ) {<br>      let timeTakenDateComplete = new Date();<br>      let timeTaken = (timeTakenDateComplete - ai.#timeTakenDateStart) / 1000;<br><br>      ai.#completeCallback(games, timeTaken);<br>    }<br>  } else {<br>    if( game.gameApi.isSetup() ) {<br>      ai.think(game);<br>    }<br>  }<br>}</pre><p>We also need to add the following variable declarations to Ai class.</p><p><strong>js/ai/Ai.js</strong></p><pre>#completeCallback;</pre><p>Reload your browser, and click the ‘start evolution’ button.</p><p>You should see an error as we didn’t declare the method areAllGamesOver(). Let’s quickly add the below to Ai</p><p><strong>js/ai/Ai.js</strong></p><pre>areAllGamesOver(games) {<br>  for ( let i = 0; i &lt; this.#totalGames; i++ ) {<br>    if( false == games[i].gameApi.isOver() ) {<br>      return false;<br>    }<br>  }<br><br>  return true;<br>}</pre><p>… Another error, we forgot to set the completeCallback. In Ai, create a constructor up the top after the variable declaration.</p><p><strong>js/ai/Ai.js</strong></p><pre>constructor(completeCallback) {<br>  this.#completeCallback = completeCallback;<br>}</pre><p>Reload your browser, and click the ‘start evolution’ button.</p><p>Ok, we are making progress, but we are now missing the clone() method. We need to be able to clone NeuralNetworks after each generation so we don&#39;t interfere with previous NeuralNetwork objects.</p><p>In NeuralNetwork class add the clone() method as well as the dispose() method, as that is called from within the clone() method.</p><p><strong>js/ai/NeuralNetwork.js</strong></p><pre>clone() {<br>  return tf.tidy(() =&gt; {<br>    let clonie = new NeuralNetwork(this.input_nodes, this.hidden_nodes, this.output_nodes);<br>    clonie.dispose();<br>    clonie.input_weights = tf.clone(this.input_weights);<br>    clonie.output_weights = tf.clone(this.output_weights);<br>    return clonie;<br>  });<br>}<br><br>dispose() {<br>  this.input_weights.dispose();<br>  this.output_weights.dispose();<br>}</pre><p>Reload your browser, and click the ‘start evolution’ button. You will see we are close now, we are just missing the mutateNeuralNetwork() method. This method is responsible for slightly tweaking the cloned NeuralNetwork so that you get a slightly different result in the next evolution. Let&#39;s go ahead and add the mutateNeuralNetwork() method into the NeuroEvolution class. I&#39;m not going to go into more detail on this, but you can google it and find this method in a few other examples out there. I also wasn&#39;t the original author of this, I just adjusted it to work for my implementation. See GitHub for reference to original source code.</p><p><strong>js/ai/NeuralEvolution.js</strong></p><pre>mutateNeuralNetwork(b) {<br>  function fn(x) {<br>    if (random(1) &lt; 0.05) {<br>      let offset = randomGaussian() * 0.5;<br>      let newx = x + offset;<br>      return newx;<br>    }<br>    return x;<br>  }<br><br>  let neuralNetwork = b.clone();<br>  let ih = neuralNetwork.input_weights.dataSync().map(fn);<br>  let ih_shape = neuralNetwork.input_weights.shape;<br>  neuralNetwork.input_weights.dispose();<br>  neuralNetwork.input_weights = tf.tensor(ih, ih_shape);<br><br>  let ho = neuralNetwork.output_weights.dataSync().map(fn);<br>  let ho_shape = neuralNetwork.output_weights.shape;<br>  neuralNetwork.output_weights.dispose();<br>  neuralNetwork.output_weights = tf.tensor(ho, ho_shape);<br>  return neuralNetwork;<br>}</pre><p>Reload your browser, and click the ‘start evolution’ button. Now after each of the 9 games fail the first time, the UI will start a new evolution. If you let this go for a few minutes you should see the game get solved — it normally takes around 30–50 generations with this current implementation.</p><h3>Congratulations!</h3><p>This is a very simple implementation of NeuroEvolution. In <a href="https://dionbeetson.blogspot.com/2019/05/neuroevolution-using-tensorflow-js-part-2.html">Part 2</a>, we will go through making improvements to really speed things up including:</p><ul><li>Implementing crossover within TensorFlowJS</li><li>Using the best progressing NeuralNetwork to ensure faster NeuroEvolution</li><li>Saving and loading models</li><li>Adding debugging information</li></ul><h3>Source code</h3><p>All source code for this tutorial can be found here <a href="https://github.com/dionbeetson/neuroevolution-experiment">https://github.com/dionbeetson/neuroevolution-experiment</a>.</p><h3>Part 2</h3><p>Continue on to <a href="https://medium.com/@dionbeetson/neuroevolution-using-tensorflow-js-part-2-b13d2861f4a9">NeuroEvolution using TensorFlowJS — Part 2</a></p><p><em>Originally published at </em><a href="https://blog.dionbeetson.com/2019/05/neuroevolution-using-tensorflowjs-part-1.html"><em>https://blog.dionbeetson.com</em></a><em> on May 26, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e66ed42ccc0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>